Title: Java Threads (Outline)
1Java Threads (Outline)
Motivation The class Thread Example program
ThreadRace The Runnable interface Example Clock
applet Thread state Thread priority Thread
groups Application to animation
2Motivation
Take advantage of multiprocessors Separate
processing that might block on I/O or other
conditions. Naturally express computations that
have multiple activities going on.
3Forms of Parallel Processing
Distributed computing coarse-grained parallelism
with relatively high communication cost (cluster
computing) Fine-grained parallel processing
massive parallelism, single-instruction-stream
multiple-data-stream processing. (e.g., for
in-camera image processing). MIMD Multiprocessors
with separate memories. Parallel Random Access
Machines (shared memory) Multithreaded
architectures (shared memory)
4The Class Thread
Thread provides a constructor plus methods start,
stop, run, interrupt, suspend, set priorities,
get status, and resume threads. public Thread()
-- constructor for a new thread. This or the
constructor for a subclass is called by the
client process to create a new thread. public
void start() -- Called by client process to
request that the Java runtime system run this
thread instance. public void run() -- We
override this method to perform the work that we
want done by the thread. public void stop() --
(deprecated) Usually called by the client process
to stop the thread. public void interrupt() --
Usually called by the client process to get the
threads attention, e.g., so that it can exit its
run method and thereby stop.
5Example ThreadRace.java
// ThreadRace.java Steve Tanimoto, 9 April
1999. public class ThreadRace public static
void main( String args) CountingThread
aCounter new CountingThread("a")
CountingThread bCounter new CountingThread("b")
aCounter.start() bCounter.start()
6Class CountingThread (for ThreadRace)
public class CountingThread extends Thread
String context public CountingThread(String
context) this.context context
public void run() for (int i 0 i lt 50
i) System.out.print(context i
context " ")
7Sample Output from ThreadRace
C\WINDOWS\Desktop\Threadsgtd\jdk1.2.1\bin\java
ThreadRace a0a b0b a1a b1b a2a b2b b3b b4b b5b
b6b b7b b8b b9b b10b b11b b12b a3a b13b a4a b14b
a5a b15b b16b b17b b18b b19b b20b b21b b22b b23b
a6a b24b b25b a7a b26b a8a b27b a9a b28b a10a
b29b a11a b30b a12a b31b a13a b32b a14a b33b a15a
b34b a16a b35b a17a b36b b37b b38b b39b b40b b41b
b42b b43b b44b a18a b45b a19a b46b a20a b47b a21a
b48b a22a b49b a23a a24a a25a a26a a27a a28a a29a
a30a a31a a32a a33a a34a a35a a36a a37a a38a a39a
a40a a41a a42a a43a a44a a45a a46a a47a a48a a49a
8The Runnable Interface
Solves the problem that we might not be able to
subclass Thread because we want to subclass
something else, like Applet public class myApplet
extends Applet implements runnable
9Handling threads in an Applet
Declare your Applet subclass to implement
Runnable. public void run() ... //Add this to
do the work. What if the browser goes to another
web page or quits? Provide a way for the browser
to not only suspend the main thread, but also to
suspend and resume any extra threads public void
start() extraThread.resume() public void
stop() extraThread.timeToSleeptrue()
extraThread.interrupt() public void destroy()
extraThread.timeToQuittrue()
extraThread.interrupt()
10Example Clock Applet
// Clock.java Steve Tanimoto, 25 October
2000. import java.awt. import
java.applet. import java.util. public class
Clock extends Applet
implements Runnable class TimeThread extends
Thread boolean timeToQuit false
public TimeThread(Runnable r) super(r)
TimeThread timeThread public Clock()
public void init() timeThread
new TimeThread(this)
timeThread.start()
11Clock Applet (continued)
public void paint(Graphics g)
g.drawString("The time is "
(new Date()), 100, 50) public
void run() while (true)
try Thread.sleep(1000) catch
(InterruptedException e) if
(timeThread.timeToQuit) return
repaint() public void
start() public void stop() public
void destroy() timeThread.timeToQuit
true timeThread.interrupt()
12Appearance of the Clock Applet
This is a test of the Clock applet.
The time is Fri Apr 09 013200 PDT
1999
13Thread State(with deprecated methods)
new
create
start
running
run
ready
yield
suspend or sleep
stop
resume
stop
inactive
finished
stop
14Thread State(with new methods)
new
create
start
running
run
ready
yield, interrupt
wait, join, or sleep
interrupt
return
inactive
finished
15Thread Priority
1 MIN_PRIORITY, 10 MAX_PRIORITY int P
myThread.getPriority() myThread.setPriority(Threa
d.MAX_PRIORITY) As long as a thread of priority
P is in the ready state, no thread of priority
less than P will be moved to the runnable
state. For threads of equal priority, CPU
resources are shared, round-robin style.
16Thread Groups
When a number of threads are to be handled in the
same manner, they can be put in a thread
group. ThreadGroup tg new ThreadGroup(my
sprites) Theres a Thread constructor that
takes a group as an arg. public
Thread(ThreadGroup g, Runnable
target, String name) There are many methods for
ThreadGroup objects such as getMaxPriority,
destroy, etc. ThreadGroups can contain
ThreadGroups. Trees of ThreadGroups are
therefore possible.
17Application to Animation
Animation requires controlled timing. A thread
that uses the sleep(int) method can handle timed
updates to a display. Multiple threads can
concurrent tasks, but synchronization may become
an issue. Therefore a single animation thread
(but separate from the main thread) often works
best. The main thread handles the user interface
and other activities that are logically separate
from those belonging to the animation.