Threads
Threads
main
run
GC
What are Threads Good For?
• To maintain responsiveness of an
application during a long running task.
• To enable cancellation of separable tasks.
• Some problems are intrinsically parallel.
• To monitor status of some resource (DB).
• Some APIs and systems demand it: Swing.
Application Thread
void start()
– Creates a new thread and makes it runnable
– This method can be called only once
void run()
– The new thread begins its life inside this
method
void stop() (deprecated)
– The thread is being terminated
Implementing Runnable
public class RunnableExample implements Runnable {
public void run () {
for (int i = 1; i <= 100; i++) {
System.out.println (“Runnable: ” + i);
}
}
}
A Runnable Object
RESULT
Thread State Diagram
Example
public class PrintThread1 extends Thread {
String name;
public PrintThread1(String name) {
this.name = name;
}
public void run() {
for (int i=1; i<500 ; i++) {
try {
sleep((long)(Math.random() * 100));
} catch (InterruptedException ie) { }
System.out.print(name);
}}
Example (cont)
public static void main(String args[]) {
PrintThread1 a = new PrintThread1("*");
PrintThread1 b = new PrintThread1("-");
PrintThread1 c = new PrintThread1("=");
a.start();
b.start();
c.start();
}
}
RESULT
Scheduling
PrintWriter writer =
new PrintWriter(
new OutputStreamWriter(
connection.getOutputStream()));
// A client of an HelloServer
class HelloClient {
deposit()
Bank Account
Static Synchronized Methods
public MoneyTransfer(
BankAccount from, BankAccount to, float amount) {
this.from = from;
this.to = to;
this.amount = amount;
}
// At one place
Runnable transaction1 =
new MoneyTransfer(aliceAccount, bobAccount, 1200);
Thread t1 = new Thread(transaction1);
t1.start();
// At another place
Runnable transaction2 =
new MoneyTransfer(bobAccount, aliceAccount, 700);
Thread t2 = new Thread(transaction2);
t2.start();
Deadlocks
t1 t2
aliceAccount bobAccount
transfer() transfer()
?
withdraw() withdraw()
deposit() deposit()
Java Locks are Reentrant
synchronized (lock) {
while (!resourceAvailable()) {
lock.wait();
}
consumeResource();
}
Producer
produceResource();
synchronized (lock) {
lock.notifyAll();
}
Wait/Notify Sequence
Lock Object
1. synchronized(lock){ 3. produceResource()
2. lock.wait(); 4. synchronized(lock) {
5. lock.notify();
9. consumeResource();
10. } 6.}
7. Reacquire lock
8. Return from wait()
Consumer Producer
Thread Thread
Wait/Notify Sequence
Lock Object
1. synchronized(lock){ 3. produceResource()
2. lock.wait(); 4. synchronized(lock) {
5. lock.notify();
9. consumeResource();
10. } 6.}
7. Reacquire lock
8. Return from wait()
Consumer Producer
Thread Thread
Wait/Notify Sequence
Lock Object
1. synchronized(lock){ 3. produceResource()
2. lock.wait(); 4. synchronized(lock) {
5. lock.notify();
9. consumeResource();
10. } 6.}
7. Reacquire lock
8. Return from wait()
Consumer Producer
Thread Thread
Wait/Notify Sequence
Lock Object
1. synchronized(lock){ 3. produceResource()
2. lock.wait(); 4. synchronized(lock) {
5. lock.notify();
9. consumeResource();
10. } 6.}
7. Reacquire lock
8. Return from wait()
Consumer Producer
Thread Thread
Wait/Notify Sequence
Lock Object
1. synchronized(lock){ 3. produceResource()
2. lock.wait(); 4. synchronized(lock) {
5. lock.notify();
9. consumeResource();
10. } 6.}
7. Reacquire lock
8. Return from wait()
Consumer Producer
Thread Thread
Wait/Notify Sequence
Lock Object
1. synchronized(lock){ 3. produceResource()
2. lock.wait(); 4. synchronized(lock) {
5. lock.notify();
9. consumeResource();
10. } 6.}
7. Reacquire lock
8. Return from wait()
Consumer Producer
Thread Thread
Wait/Notify Sequence
Lock Object
1. synchronized(lock){ 3. produceResource()
2. lock.wait(); 4. synchronized(lock) {
5. lock.notify();
9. consumeResource();
10. } 6.}
7. Reacquire lock
8. Return from wait()
Consumer Producer
Thread Thread
Wait/Notify Sequence
Lock Object
1. synchronized(lock){ 3. produceResource()
2. lock.wait(); 4. synchronized(lock) {
5. lock.notify();
9. consumeResource();
10. } 6.}
7. Reacquire lock
8. Return from wait()
Consumer Producer
Thread Thread
Wait/Notify Sequence
Lock Object
1. synchronized(lock){ 3. produceResource()
2. lock.wait(); 4. synchronized(lock) {
5. lock.notify();
9. consumeResource();
10. } 6.}
7. Reacquire lock
8. Return from wait()
Consumer Producer
Thread Thread
Wait/Notify Sequence
Lock Object
1. synchronized(lock){ 3. produceResource()
2. lock.wait(); 4. synchronized(lock) {
5. lock.notify();
9. consumeResource();
10. } 6.}
7. Reacquire lock
8. Return from wait()
Consumer Producer
Thread Thread
Wait/Notify Sequence
Lock Object
1. synchronized(lock){ 3. produceResource()
2. lock.wait(); 4. synchronized(lock) {
5. lock.notify();
9. consumeResource();
10. } 6.}
7. Reacquire lock
8. Return from wait()
Consumer Producer
Thread Thread
The Simpsons Scenario:
SimpsonsTest
public class SimpsonsTest {
new Thread(homer).start();
new Thread(marge).start();
}
}
The Simpsons Scenario: Homer
public class Homer implements Runnable {
CookyJar jar;