Home : Course Map : Chapter 8 : Java :
Demo : Non-interacting Threads
JavaTech
Course Map
Chapter 8

Introduction
Threads Overview
  Demo 1   Demo 2
Stopping Threads
Multi-Processing
Thread Tasks
Animations
 
 Demo 3   Demo 4  
  Demo 5

Non-interacting
  Demo 6

Task Splitting
  Demo 7

Exclusivity
  Demo 8

Communicating
  Demo 9

Priority/Scheduling
More Thread

Exercises

    Supplements
Java2D Animation
  Demo 1 
Processor View
More Concurrency
Cloning
  Demo 2  Demo 3 

     About JavaTech
     Codes List
     Exercises
     Feedback
     References
     Resources
     Tips
     Topic Index
     Course Guide
     What's New

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

              Tech
Timers
  Demo 1
Hist. Adapt Range
  Demo 2
Sorting in Java
  Demo 3
Histogram Median
  Demo 4
Refactoring
  Demo 5
Error Bars
  Demo 6
Exercises

           Physics
Least Squares Fit
  Demo 1
Fit to Polynomial
  Demo 2
Fit Hist Errors
  Demo 3
Discretization
  Demo 4
Timing
  Demo 5
Exercises

  Part I Part II Part III
Java Core 1  2  3  4  5  6  7  8  9  10  11  12 13 14 15 16 17
18 19 20
21
22 23 24
Supplements

1  2  3  4  5  6  7  8  9  10  11  12

Tech 1  2  3  4  5  6  7  8  9  10  11  12
Physics 1  2  3  4  5  6  7  8  9  10  11  12

Java is a trademark of Sun Microsystems, Inc.