<Previous    Back to Start  Contents   Next>


Threads


Write code to define, instantiate and start new threads using both java.lang.Thread and java.lang.Runnable.

To define a class that implements Runnable, you must define a run() method in the class e.g.

class MyClass implements Runnable {
	public void run () {
		// Insert the code you want to run in a thread here
	}
}

run() must be public, with a void return type, and not take any arguments. Note that if run() is defined to take any arguments, then that is a different method and the Runnable interface hasn't been implemented!

Create an instance of the class, and construct a thread, passing the instance of the class as an argument to the constructor i.e.

MyClass mc = new Myclass();
Thread t = new Thread(mc);

To start the thread, use the start() method:

t.start();

Alternatively, you can create a subclass of the Thread class, and override the run() in the subclass:

class MyThread extends Thread {
	public void run () {
		// Insert the code you want to run in a thread here
	}
}

Then create an instance of the subclass, and start it running:

MyThread mt=new MyThread();
mt.start();

Recognize conditions that might prevent a thread from executing.

A thread can be in one of the following states:
a) Running
b) Ready - Waiting for a chance to run on the CPU. All threads enter this state before running. Threads with higher priorities may be preventing the thread from getting a chance to run (this depends on how priorities have been implemented in the particular Java Virtual machine)
c) Various waiting states (Waiting, Sleeping, Suspended & Blocked)

d) Dead - the run() method has completed (the thread can never be restarted)
Note that the suspend() and resume() methods of the thread class are deprecated in Java2.


Write code using synchronized wait, notify and notifyAll to protect against concurrent access problems and to communicate between threads.

The use of these methods, and synchronized methods or code blocks, ensure only one thread at a time can access parts of a class, and control how and when threads get this access. This is a complicated area, if it is new to you, consult a textbook.

Using the synchronized keyword to require a thread of execution to obtain an object lock prior to proceeding.

Using the synchronized keyword in the method declaration, requires a thread obtain the lock for this object before it can execute the method.

synchronized void someMethod() { }

You can also make a block of code synchronized by using the synchronized keyword followed by the object or class, for which a thread must obtain the lock before it can execute this block of code:

// .. some code before the synchronized block
synchronized (someObject) {
	// synchronized code
	}
// more code...

The wait() method.

The wait() method must be used in synchronized code. The thread releases ownership of this monitor and waits until another thread notifies threads waiting on this object's monitor to wake up (i.e. move into Ready state) either through a call to the notify() method or the notifyAll() method. The thread then waits until it can re-obtain ownership of the monitor and resumes execution.

The notify() or notifyAll() methods of an object.

notify() moves one thread, that is waiting the this objects monitor, into the Ready state. This could be any of the waiting threads; the choice of which thread is chosen is an implementation issue of the virtual machine.

notifyAll() moves all threads, waiting on this objects monitor, into the Ready state.


Define the interaction among threads and object locks when executing synchronized wait, notify or notifyAll.

Consult a textbook or the web.


<Previous    Back to Start  Contents   Next>

©1999, 2000, 2002 Dylan Walsh.
Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.1 or any later version published by the Free Software Foundation; with the Invariant Sections being the disclaimer, no Front-Cover Texts, and no Back-Cover Texts. A copy of the license is included in the section entitled "GNU Free Documentation License".