■ The four states of a thread are (1) new, (2) runnable, (3) blocked, and (4) dead.
■ A new thread is a null object. It can only be run( ).
■ A dead thread cannot be restarted.
■ You can still access the methods and variables of a dead thread.
■ A blocked thread may have been:
(1) put to sleep( )
(2) suspended( ) (deprecated)
(3) suspended with a call to wait( ), waiting on a notify( ) or notifyAll( )
■ The public void run( ) method of the Thread class is real but does nothing.
Note: Thread is not
abstract. So you can compile successfully while
extending Thread without
actually providing a run( )
method.
■ Actual public void run( ) methods can be:
(1) The overriding public void run( ) method of a class which extends Thread, or
(2) The public void run( ) method of a class which implements (the) Runnable interface.
■ You stop a thread with either stop( ) or interrupt( ) or System.exit(...); or if another thread gets higher priority.
■ How to create a thread with extends Thread:
(1) Create a subclass of Thread
(2) Give it a public void run( ) method with your code
(3) Create an instance of it in main(...)
(4) Invoke start( ) on the instance
■ Creating a thread with implements Runnable:
(1) Create a class implementing the Runnable interface
(2) Give it a public void run( ) method with your code.
(3) Create an instance of it in main(...)
(4) Pass it to a new instance of Thread
(5) Invoke start( ) on the Thread object
■ Classes not
supplying a
run( ) method
must be declared
abstract like Runnable.
wait( ) method
■ The simple sequence of events for using wait( ) is:
(1) make an infinite loop in a synchronized
method, if you're working against another thread.
(2) try a wait( ) to give up your lock
(3) after catching the InterruptedException, do the thread actions
(4) notifyAll( ) to let others run
(5) back to step 2
notify( ) and
notifyAll( ) methods
■ notify( ) removes waiting threads from the wait state and returns them to the runnable state.
■ You can't tell which other thread notify( ) or notifyAll( ) will allow to run. The JVM selects which notified and runnable threads actually get run, and when.
■ notifyAll( ) removes them all from wait, and the JVM then runs just one, which it selects. The others get locked again, temporarily.
■ All calls to
wait( ), notify( ), and notifyAll( ) must be made in synchronized
code blocks or you will get an IllegalMonitorStateException.
■ All threads have a priority instance variable. You can set it with the Thread class's setPriority(...) method. i.e.
■ A new thread
inherits the priority of the thread that created it. It doesn’t automatically get the (Sun “default”) NORM.PRIORITY. (This per Brogden)
■ Priority values go from 1 to 10, with several constants:
Thread.MIN_PRIORITY = 1
Thread .NORM_PRIORITY = 5
Thread
.MAX_PRIORITY = 10
■ A thread is blocked:
(1) if sleep(...) is called
(2) if wait( ) is called
(3) if suspend( ) is called
(4) waiting for an I/O operation
(5) waiting on a lock
import java.awt.Toolkit;
public class Test{
public static void main(String[ ]
args) {
int i;
if(args.length > 0) i
= Integer.parseInt(args[0]);
else i = 5;
while(true){
try {
Thread.sleep(i
* 60* 1000);
/* Plays dat da-da-da-dat
dat-dat */
Toolkit.getDefaultToolkit().beep();
Thread.sleep(500);
Toolkit.getDefaultToolkit().beep();
Toolkit.getDefaultToolkit().beep();
Toolkit.getDefaultToolkit().beep();
Thread.sleep(200);
Toolkit.getDefaultToolkit().beep();
Thread.sleep(300);
Toolkit.getDefaultToolkit().beep();
Thread.sleep(75);
Toolkit.getDefaultToolkit().beep();
Toolkit.getDefaultToolkit().beep();
Thread.sleep(150);
Toolkit.getDefaultToolkit().beep();
}
catch
(InterruptedException e) { }
}
}
}