01 Javathreads
01 Javathreads
01 Javathreads
Concurrent threads are implemented in one of two ways depending on the computer hardware
available. When multiple processors are available, concurrent threads can be executed in parallel,
each on its own processor. This is called physical concurrency.
Example
Suppose thread #1 computes a * b + c * d and thread #2 computes w * x + y * z. With two
processors the computation can proceed as follows:
When only one processor is available, it executes concurrent threads by interleaving the
execution of their individual operations, a process called logically concurrency.
Example
Suppose thread #1 computes a * b + c * d and thread #2 computes w * x + y * z. One processor
interleaves the machine operations. One possible interleaving is:
Time Processor
1 Compute A = a * b
2 Compute B = w * x
3 Compute C = c * d
4 Compute D = y * z
5 Compute A + C
6 Compute B + D
The behavior of concurrent threads is equivalent regardless of whether they are implemented
physically or logically. This means that the choice of physical or logical concurrency can be left
to the compiler and operating system without you having to make special allowances for it in
your code.
Java provides two mechanisms for creating your own threads. The first is to write a class that
extends the API class java.lang.Thread.
Thread Syntax #1
public class class name extends Thread
{
public class name ( String name )
{
super( name );
thread initialization code
}
Place into the constructor any initialization code you need and into method run whatever you
want your thread to do. The thread runs in the context of the Java program that creates it.
1 import javax.swing.*;
2 import java.awt.*;
3 import javax.swing.border.*;
4
5 public class Counter implements Runnable
6 {
7 private int counter = 0; // counter to increment
8 private int delay; // sleep time
9 private JTextField display; // display for counter
10 // fancy JPanel to display, made public so another
11 // object can add it to a containment hierarchy
12 public JPanel displayPanel;
13
14 public void run( )
15 // morphed from Thread to do what I want it to.
16 {
17 try // needed for call to Thread.sleep (see below)
18 {
19 while ( true ) // do forever
20 {
21 counter++; // increment counter & display
22 display.setText( Integer.toString( counter ) );
23 Thread.sleep( delay ); // sleep
24 // sleep can throw InterruptedException
25 }
26 }
27 catch( InterruptedException e )
28 { // probably won't happen, but we handle it anyway
29 String name = Thread.currentThread().getName();
30 System.out.println( name + " halted during run." );
31 return;
Example
The following Java applet builds a Counter object and starts it running.
1 import java.applet.*;
2 import java.awt.event.*;
3 import java.awt.*;
4
5 public class CounterThread extends Applet
6 {
7 public void init( )
8 {
9 // build the counter object
10 Counter counter = new Counter( "Counter 1" );
11 // change applet's layout (default is BorderLayout)
12 setLayout( new FlowLayout( ) );
13 // add counter's display panel to applet
14 add( counter.displayPanel );
15 // start thread running
16 counter.start( );
17 }
18 }
The second mechanism for creating your own threads is to write a class that implements the
interface java.lang.Runnable, which is also available in the Java API. Runnable
contains only one method, the unimplemented run, which you implement in the new class. The
Java API specification recommends using this technique if the run method is the only Thread
method you intend to morph.
Example
To write the Counter class using the Runnable interface, the code is exactly the same as
previously listed except for line 5, which must be changed to:
Example
1 import java.applet.*;
2 import java.awt.event.*;
3 import java.awt.*;
4
5 public class CounterRunnable extends Applet
6 {
7 public void init( )
8 {
9 // build runnable object
10 Counter counter = new Counter( "Counter 1" );
11 // build the thread object from the runnable
12 Thread thread = new Thread( counter );
13 // change applet's layout (default is BorderLayout)
14 setLayout( new FlowLayout( ) );
15 // add counter's display panel to applet
16 add( counter.displayPanel );
17 // start thread running
18 thread.start( );
19 }
20 }
1. Save the classes Counter and CounterThread to files. Compile and run them using
jGRASP.