| Class 2 | Intermediate Java 30-IT-397 | 
Thread Groups
package Threads;
import javax.swing.DefaultListModel;
/**
 *
 * @author  default
 */
public class InterruptThread extends javax.swing.JFrame {
    // These variables are globally visible.
    DefaultListModel model;
    AddRandoms addRandoms;
    ThreadGroup threadGroup;
    int threadCounter;
 
    /** Creates new form RandomNumber */
    public InterruptThread() {
        initComponents();
 
        // Instantiate the model,
add it to the list.
        model = new DefaultListModel();
        lstNumbers.setModel(model);
        threadGroup = new ThreadGroup("randoms");
        threadCounter = 0;
 
    }
    // An inner class which will run as a thread.
    class AddRandoms extends Thread {
       
public AddRandoms(ThreadGroup t, String s) {
           
super(t, s);
       
}
 
        public void run() {
           
try {
               
if ( interrupted() ) { throw new InterruptedException(); }
               
for (int j = 0; j < 500; j++) {
                   
// Generate a random number between 0 and 1, multiply it by 1000 to make
it intersting.
                   
int intRandom = (int) (Math.random() * 1000);
                   
// add the random number to the model.
                   
model.addElement(j + ": " + intRandom);
                   
// Sleep for 20 ms.
                   
Thread.sleep(20);
               
}
           
} catch (InterruptedException e) {
               
System.out.println("Interrupted, returning");
               
return;
           
}
        }
    }
 
    /** This method is called from within the constructor
to
     * initialize the form.
     * WARNING: Do NOT modify this code. The
content of this method is
     * always regenerated by the Form Editor.
     */
    private void initComponents() {
        jScrollPane1 = new javax.swing.JScrollPane();
        lstNumbers = new javax.swing.JList();
        jPanel1 = new javax.swing.JPanel();
        btnStart = new javax.swing.JButton();
        btnStop = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        addWindowListener(new
java.awt.event.WindowAdapter() {
           
public void windowClosing(java.awt.event.WindowEvent evt) {
               
exitForm(evt);
           
}
        });
jScrollPane1.setViewportView(lstNumbers);
getContentPane().add(jScrollPane1, java.awt.BorderLayout.CENTER);
        btnStart.setText("Start");
        btnStart.addActionListener(new
java.awt.event.ActionListener() {
           
public void actionPerformed(java.awt.event.ActionEvent evt) {
               
btnStartActionPerformed(evt);
           
}
        });
jPanel1.add(btnStart);
        btnStop.setText("Stop");
        btnStop.addActionListener(new
java.awt.event.ActionListener() {
           
public void actionPerformed(java.awt.event.ActionEvent evt) {
               
btnStopActionPerformed(evt);
           
}
        });
jPanel1.add(btnStop);
getContentPane().add(jPanel1, java.awt.BorderLayout.SOUTH);
        jLabel1.setText("Random
Numbers");
        getContentPane().add(jLabel1,
java.awt.BorderLayout.NORTH);
        pack();
    }
    private void btnStartActionPerformed(java.awt.event.ActionEvent
evt) {
       
// Start the AddRandoms thread.
       
threadCounter++;
       
addRandoms = new AddRandoms(threadGroup, "Thread" + threadCounter);
        addRandoms.start();
 
 
    }
    private void btnStopActionPerformed(java.awt.event.ActionEvent
evt) {
       
// Stop the group.
       
threadGroup.interrupt();
 
    }
    /** Exit the Application */
    private void exitForm(java.awt.event.WindowEvent
evt) {
        System.exit(0);
 
    }
    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        new InterruptThread().show();
    }
 
    // Variables declaration - do not modify
    private javax.swing.JButton btnStop;
    private javax.swing.JList lstNumbers;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JButton btnStart;
    // End of variables declaration
}
 
Created by: Brandan Jones December 17, 2001