CSE 413 Winter 2001 Threads
• Thread = Execution of one sequence of
instructions (including function/method calls,
conditionals, loops).
• Normal Java program executes in a thread created
Notes on Java Threads for main (application) or borrowed from the
browser (applets).
• Class Thread can be used to create additional
threads that execute concurrently.
• Each new thread is associated with (controlled by)
a Thread object.
2/8/2001 1 2/8/2001 2
Single Thread Example Extending Class Thread
class Foo { • Class Thread can be extended to create objects
void run( ) {
for (int i=0; i<100; i++) that run concurrently in their own thread.
[Link](“foo ”); • Execution begins in method run of the new class.
}
} class Foo extends Thread {
class Bar { void run( ) {
public static void main(char[]args) { for (int i=0; i<100; i++)
Foo foo = new Foo(); [Link](“foo ”);
[Link](); }
for (int i=0; i<100; i++) }
[Link](“bar ”);
}
• [Link] overrides a (basically) empty method
} run in class Thread.
• Prints 100 “foo”s followed by 100 “bar”s
2/8/2001 3 2/8/2001 4
Concurrent Execution Uses for Threads
• To begin concurrent execution, call method • Asynchronous or nonblocking I/O
start of a Thread object. This sets up the new – Continue execution in one thread while waiting for I/O
thread, then calls the object’s run method. to complete or time out in another.
• Timers
class Bar { – Wait for an interval to expire, then cause something to
public static void main(char[]args) {
happen (examples: animations; do something if the user
Foo foo = new Foo();
[Link](); doesn’t respond after a reasonable interval, …)
for (int i=0; i<100; i++) • Process multiple tasks simultaneously
[Link](“bar ”); – Handle GUI in one thread while doing extended
}
calculations in another.
}
• Parallel algorithms
• Prints 100 “foo”s and 100 “bar”s in some
– If the JVM supports it, run parts of the computation
unpredictable order concurrently on different processors.
2/8/2001 5 2/8/2001 6
1
Runnable Classes Using Runnable
• There are many situations where we want to • This class executes one of its methods in a
execute a computation concurrently, but in a class separate thread
that’s not a subclass of Thread.
class FooBar implements Runnable {
• We still need a Thread object to create and public void foo( ) {
control the thread. for (int i=0; i<100; i++)
[Link](“foo ”);
• A thread can begin execution in any class that }
implements Runnable and contains a run
method. public void bar( ) {
for (int i=0; i<100; i++)
public interface Runnable { [Link](“bar ”);
public abstract void run(); }
} ...
2/8/2001 7 2/8/2001 8
Using Runnable (cont.) Synchronization
public void run( ) {
foo( ); • Since threads may interleave execution in any
} order, we may need to control access to objects to
public static void main(char[]args) {
FooBar fb = new Foobar(); ensure only one thread at a time can update related
Thread t = new Thread(fb); variables.
[Link]( ); class C {
bar( ); int x,y;
public void setXY(int x, int x) {
}
} this.x = x; this.y = y;
}
• [Link]() creates a new thread, then executes public int sumXY( ) { return x+y; }
run() in that thread. }
• Meanwhile, the original thread calls bar(). • What happens if one thread executes sumXY while
• Prints 100 “foo”s and 100 “bar”s in some another thread is halfway through executing
unpredictable order setXY on the same object?
2/8/2001 9 2/8/2001 10
synchronized methods synchronized methods
class C {
• Every object has an associated lock int x,y;
• We can require threads to acquire the lock before public synchronized void setXY(int x, int x) {
executing one of the object’s methods by this.x = x; this.y = y;
}
declaring the method to be synchronized. public synchronized int sumXY( ) { return x+y;
• A synchronized method automatically }
}
acquires the object’s lock when it is called. Other
threads are blocked until the lock is released • If some thread is executing setXY or sumXY, no
automatically when the synchronized method other thread can execute either of those methods
terminates. until the first thread releases the lock.
• Methods wait and notify are available to
temporarily release the lock and regain it as
needed.
2/8/2001 11 2/8/2001 12