The simplest situation with multiple threads is when
each thread runs independently without interacting with each other.
NonInteract
applet shown below is a simple example of such a case. We have
three instances of IntCounter,
a subclass of Thread,
each of which prints out the values of an integer counter. These
counters do not interact with each other and run independently.
Here we use the Outputable
interface so that the print statements in IntCounter
send their strings to the JTextArea
in the applet interface.
NonInteract.java
+ Outputable.java
|
import
javax.swing.*;
import java.awt.*;
import java.awt.event.*;
/**
* This demo applet creates three instances of
the
* IntCounter thread subclass, each of which
will do
* an independent calculation. Illustrates how
threads
* can do parallel (or apparent parallel on a
single processor)
* processing via threads.
* Output of the thread goes to the text area
on the applet
* interface. The applet implements the Outputable
interface.
**/
public class NonInteract extends JApplet
implements Outputable, ActionListener {
// A Swing textarea for display of string info
JTextArea fTextArea = null;
/**
* Create a User Interface with a
textarea with sroll bars
* and a Go button to initiate processing
and a Clear button
* to clear the textarea.
**/
public void init () {
Container content_pane = getContentPane
();
JPanel panel = new JPanel (new BorderLayout
());
// Create a text area.
fTextArea = new JTextArea ();
// Make it editable
fTextArea.setEditable (false);
// Add to a scroll pane so that
a long list of
// computations can be seen.
JScrollPane area_scroll_pane = new
JScrollPane (fTextArea);
panel.add (area_scroll_pane,BorderLayout.CENTER);
JButton go_button = new JButton
("Go");
go_button.addActionListener (this);
JButton clear_button = new JButton
("Clear");
clear_button.addActionListener (this);
JPanel control_panel = new JPanel
();
control_panel.add (go_button);
control_panel.add (clear_button);
panel.add (control_panel,BorderLayout.SOUTH);
// Add text area & control panel.
content_pane.add (panel);
} // init
/** Respond to the buttons to start the threads
or to clear
* the text area.
**/
public void actionPerformed (ActionEvent e)
{
if ( e.getActionCommand ().equals
("Go"))
start ();
else
fTextArea.setText
(null);
} // actionPerformed()
/**
* Can use the start () method, which
is called after
* init () and the display has been
created.
**/
public void start () {
// Create 3 instances of each of
the Thread subclass
IntCounter ic1 = new IntCounter
(1, this);
IntCounter ic2 = new IntCounter
(2, this);
IntCounter ic3 = new IntCounter
(3, this);
// Start the threads
println ("Start:");
ic1.start ();
ic2.start ();
ic3.start ();
} // start
/** Overided Outputable println to send string
to text area.**/
public void println (String str) {
fTextArea.append (str + CR);
}
/** Overided Outputable print to send string
to text area.**/
public void print (String str) {
fTextArea.append (str);
}
} // class NonInteractApplet
/** Demo Thread class to show how threads could do
* calculations in parallel.
**/
class IntCounter extends Thread {
int fId=0;
int fIter = 0;
int fMaxIter = 0;
Outputable fOutput = null;
/** Constructor to initialize parameters. **/
IntCounter (int id, Outputable out) {
fId = id;
fMaxIter = 100000;
fOutput = out;
} // ctor
/** Simulate a calculation by just doing an
integer sum.**/
public void run () {
while (fIter < fMaxIter) fIter++;
fOutput.println (" Thread
"+ fId +": sum = " + fIter);
}
} // class IntCounter
|
You should see some overlap in the outputs of the
threads. Also, you will see that each time the program runs, this
overlapping will vary.
Latest update: Sept. 22, 2005
|