1 cs236607. 2 3 process 1 processes and threads environment stack code heap cpu state process 2...

78
1 cs236607

Upload: holly-mariah-sullivan

Post on 14-Jan-2016

225 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 1 cs236607. 2 3 Process 1 Processes and Threads Environment Stack Code Heap CPU State Process 2 Environment Stack Code Heap CPU State cs236607

1cs236607

Page 2: 1 cs236607. 2 3 Process 1 Processes and Threads Environment Stack Code Heap CPU State Process 2 Environment Stack Code Heap CPU State cs236607

cs236607 2

Page 3: 1 cs236607. 2 3 Process 1 Processes and Threads Environment Stack Code Heap CPU State Process 2 Environment Stack Code Heap CPU State cs236607

3

Process 1Process 1

Processes and Threads

Environment

Stack

CodeHeap

CPUState

Process 2Process 2

Environment

Stack

CodeHeap

CPUState

cs236607

Page 4: 1 cs236607. 2 3 Process 1 Processes and Threads Environment Stack Code Heap CPU State Process 2 Environment Stack Code Heap CPU State cs236607

4

Process 1Process 1

EnvironmentCodeHeap

Stack

CPUState

Thread 2Thread 2

Stack

CPUState

Thread 1Thread 1

Stack

CPUState

Thread 3Thread 3

Processes and Threads

cs236607

Page 5: 1 cs236607. 2 3 Process 1 Processes and Threads Environment Stack Code Heap CPU State Process 2 Environment Stack Code Heap CPU State cs236607

Tabs in BrowsersIn most browsers (e.g., Firefox, IE, Safari)

tabs are implemented by using threadsIn Chrome, tabs are implemented using

processes

Which approach is better?

cs236607 5

Page 6: 1 cs236607. 2 3 Process 1 Processes and Threads Environment Stack Code Heap CPU State Process 2 Environment Stack Code Heap CPU State cs236607

6

A thread is a single sequence of execution within a program

Multiprogramming:Running multiple processes concurrently

Multithreading:Running multiple threads within one process

concurrentlyThat is, having several code executions within a single

process

Concurrency is achieved either by using several processors or by time-slicing over one processor

Terminology

cs236607

Page 7: 1 cs236607. 2 3 Process 1 Processes and Threads Environment Stack Code Heap CPU State Process 2 Environment Stack Code Heap CPU State cs236607

7

Time Slicing vs. ParallelismCPU CPU1 CPU2

cs236607

Page 8: 1 cs236607. 2 3 Process 1 Processes and Threads Environment Stack Code Heap CPU State Process 2 Environment Stack Code Heap CPU State cs236607

8

Multiple Threads in an ApplicationEach thread has its own run-time stack and CPU

state (i.e., register content, next instruction, etc. )If two threads execute the same method, each will

have its own copy of the local variables the methods usesWhy?

However, all threads see the same dynamic memory, i.e., heap Which variables are stored on the heap?

Two different threads can act on the same object and same static fields concurrently

cs236607

Page 9: 1 cs236607. 2 3 Process 1 Processes and Threads Environment Stack Code Heap CPU State Process 2 Environment Stack Code Heap CPU State cs236607

9

Why Threads?Improve the responsiveness of applications

i.e., allows user interaction while a task is runningImprove utilization of resources

e.g., one thread can run while the other waits for I/O Provide a convenient programming technique

run resource cleaners in the backgroundgood for monitoringeasy to separate tasksgraphical applications

cs236607

Page 10: 1 cs236607. 2 3 Process 1 Processes and Threads Environment Stack Code Heap CPU State Process 2 Environment Stack Code Heap CPU State cs236607

10

API and how to use Threads

cs236607

Page 11: 1 cs236607. 2 3 Process 1 Processes and Threads Environment Stack Code Heap CPU State Process 2 Environment Stack Code Heap CPU State cs236607

11

Class ThreadWe use the class Thread in order to create

and run threadsTwo ways to create Thread objects:

Implementing Runnable and wrapping with Thread

Extending ThreadThread itself also implements Runnable, so in

both ways you implement the method run()

cs236607

Page 12: 1 cs236607. 2 3 Process 1 Processes and Threads Environment Stack Code Heap CPU State Process 2 Environment Stack Code Heap CPU State cs236607

12

Implementing Runnablepublic class MinusPrinter implements Runnable { public void run() { for(int i=0; i<1000; ++i) { System.out.print("-"); } }} Runnable r = new

MinusPrinter();Thread t1 = new Thread(r);

When should we use this method of creating threads?

cs236607

Page 13: 1 cs236607. 2 3 Process 1 Processes and Threads Environment Stack Code Heap CPU State Process 2 Environment Stack Code Heap CPU State cs236607

13

Extending Threadpublic class PlusPrinter extends Thread { public void run() { for(int i=0; i<1000; ++i) { System.out.print("+"); } }}

Thread t2 = new PlusPrinter();

cs236607

Page 14: 1 cs236607. 2 3 Process 1 Processes and Threads Environment Stack Code Heap CPU State Process 2 Environment Stack Code Heap CPU State cs236607

14

Running ThreadsA Thread's execution starts by invoking its

method start()This method invokes its method run() in a

new threadpublic static void main(String argv[]) { Runnable r = new MinusPrinter(); Thread t1 = new Thread(r); Thread t2 = new PlusPrinter(); t1.start(); t2.start();}

cs236607

Page 15: 1 cs236607. 2 3 Process 1 Processes and Threads Environment Stack Code Heap CPU State Process 2 Environment Stack Code Heap CPU State cs236607

cs236607 15

Page 16: 1 cs236607. 2 3 Process 1 Processes and Threads Environment Stack Code Heap CPU State Process 2 Environment Stack Code Heap CPU State cs236607

16

Running ThreadsA Thread's execution starts by invoking its

method start()

public static void main(String argv[]) { Runnable r = new MinusPrinter(); Thread t1 = new Thread(r); Thread t2 = new PlusPrinter(); t1.start(); t2.start();}

cs236607

What would happen if

we’ll change these lines

tot1.run();t2.run();

How many threads do we have

here?

Page 17: 1 cs236607. 2 3 Process 1 Processes and Threads Environment Stack Code Heap CPU State Process 2 Environment Stack Code Heap CPU State cs236607

17cs236607

Page 18: 1 cs236607. 2 3 Process 1 Processes and Threads Environment Stack Code Heap CPU State Process 2 Environment Stack Code Heap CPU State cs236607

18

Thread SchedulingUsually, threads run one at a time

Unless several processors are being usedThread execution is merged in order to

provide concurrency

cs236607

Page 19: 1 cs236607. 2 3 Process 1 Processes and Threads Environment Stack Code Heap CPU State Process 2 Environment Stack Code Heap CPU State cs236607

19

Scheduling Threads

I/O operation completes

start()

Currently executedthread

Ready queue

•Waiting for I/O operation to be completed•Waiting to be notified•Sleeping•Waiting to enter a synchronized section

Newly createdthreads

Blocked queue

cs236607

What happens when a program with aServerSocket callsaccept()?

Page 20: 1 cs236607. 2 3 Process 1 Processes and Threads Environment Stack Code Heap CPU State Process 2 Environment Stack Code Heap CPU State cs236607

20

Alive

Thread State Diagram

New Dead

Running

Runnable

new PlusPrinter();

run() method returns

for (…) { … }

BlockedObject.wait()Thread.sleep()blocking IO callwaiting on a monitor

thread.start();

Don’t get confused with Runnable interface

Thread.getState()

Page 21: 1 cs236607. 2 3 Process 1 Processes and Threads Environment Stack Code Heap CPU State Process 2 Environment Stack Code Heap CPU State cs236607

21

Thread SchedulingThread scheduling is the mechanism used to

determine how Runnable threads (in the ready queue) are allocated CPU time

Java scheduling is preemptive, priority basedEach thread has a priority - an integer

numberUse thread.getPriority()/setPriority() to control

priorities In principle, the runtime system chooses the

thread that has the highest priority

cs236607

Priority has range 1-10 What is the priority of the main thread?

Page 22: 1 cs236607. 2 3 Process 1 Processes and Threads Environment Stack Code Heap CPU State Process 2 Environment Stack Code Heap CPU State cs236607

22

Thread Scheduling (cont)If several threads have the same priority,

an arbitrary one is chosenScheduling may violate the priority-based

policy to avoid starvation of low-priority threads

Java's scheduling also uses time slicing, when it is supported by the operating systemmainly used for sharing CPU among equal

highest-priority threadscs236607

Page 23: 1 cs236607. 2 3 Process 1 Processes and Threads Environment Stack Code Heap CPU State Process 2 Environment Stack Code Heap CPU State cs236607

23

Thread Scheduling (cont)A thread runs until one of the following occurs:The thread dies (e.g., run() completes)The thread becomes not Runnable (e.g., sleeps

or waits for an IO operation to complete)A higher-priority thread becomes RunnableOn systems that support time-slicing, its time

allotment has expiredThe thread yields (discussed later)

cs236607

Page 24: 1 cs236607. 2 3 Process 1 Processes and Threads Environment Stack Code Heap CPU State Process 2 Environment Stack Code Heap CPU State cs236607

24

Scheduling is OS Dependant!Java maps Java threads to OS threadsIn particular, Java relies on the operating

system forTime slicingPriorities

Thus, scheduling differs from one system to another

Do not count on scheduling and priorities for algorithm correctness!

cs236607

Page 25: 1 cs236607. 2 3 Process 1 Processes and Threads Environment Stack Code Heap CPU State Process 2 Environment Stack Code Heap CPU State cs236607

25

Relinquishing the CPUA running thread can explicitly relinquish

the CPU for other threads to useThe static method Thread.yield() temporarily

pauses the currently executing thread and allows other threads to execute

A thread can block for a while using the method Thread.sleep(milliseconds)

Could thread A call Thread.yield() and cause thread B to stop running ?

cs236607

Page 26: 1 cs236607. 2 3 Process 1 Processes and Threads Environment Stack Code Heap CPU State Process 2 Environment Stack Code Heap CPU State cs236607

cs236607 26

public class MinusPrinter implements Runnable { public void run() { for(int i=0; i<1000; ++i) { System.out.print("-"); Thread.yield(); } }}

public class PlusPrinter extends Thread { public void run() { for(int i=0; i<1000; ++i) { System.out.print("+"); Thread.yield(); } }}

Page 27: 1 cs236607. 2 3 Process 1 Processes and Threads Environment Stack Code Heap CPU State Process 2 Environment Stack Code Heap CPU State cs236607

cs236607 27

Page 28: 1 cs236607. 2 3 Process 1 Processes and Threads Environment Stack Code Heap CPU State Process 2 Environment Stack Code Heap CPU State cs236607

cs236607 28

public class MinusPrinter implements Runnable { public void run() { try { Thread.sleep(5); } catch(InterruptedException e) {} for(int i=0; i<1000; ++i) { System.out.print("-"); Thread.yield(); } }}

public class PlusPrinter extends Thread { public void run() { for(int i=0; i<1000; ++i) { System.out.print("+"); Thread.yield(); } }}

Page 29: 1 cs236607. 2 3 Process 1 Processes and Threads Environment Stack Code Heap CPU State Process 2 Environment Stack Code Heap CPU State cs236607

cs236607 29

Page 30: 1 cs236607. 2 3 Process 1 Processes and Threads Environment Stack Code Heap CPU State Process 2 Environment Stack Code Heap CPU State cs236607

30

Daemon ThreadsThere are two types of threads:

daemon threads (like the garbage-collection thread)non-daemon threads (like the thread running main())

JVM will let all non-daemon threads complete (i.e., complete the execution of run())

When only daemon threads stay alive, they are killed and JVM exits

Controlled by thread.isDaemon() and thread.setDeamon()

cs236607

Page 31: 1 cs236607. 2 3 Process 1 Processes and Threads Environment Stack Code Heap CPU State Process 2 Environment Stack Code Heap CPU State cs236607

31

Garbage CollectionThe garbage collector of Java runs on a

separate (daemon) threadAn object is a candidate for garbage

collection if this object can no longer be accessed by any living thread

cs236607

Page 32: 1 cs236607. 2 3 Process 1 Processes and Threads Environment Stack Code Heap CPU State Process 2 Environment Stack Code Heap CPU State cs236607

32

NotesThreads inherit their priority and daemon

properties from their creating threadsThe method thread.join() blocks and waits until

the thread completes runningA thread can have a name for identificationStopping a running thread was possible in

old versions of Java, but it is now deprecatedInstead, interruption mechanisms should be used

cs236607

Page 33: 1 cs236607. 2 3 Process 1 Processes and Threads Environment Stack Code Heap CPU State Process 2 Environment Stack Code Heap CPU State cs236607

33cs236607

Page 34: 1 cs236607. 2 3 Process 1 Processes and Threads Environment Stack Code Heap CPU State Process 2 Environment Stack Code Heap CPU State cs236607

34

Server

Client

Request Handler

Client

Request Handler …

cs236607

Page 35: 1 cs236607. 2 3 Process 1 Processes and Threads Environment Stack Code Heap CPU State Process 2 Environment Stack Code Heap CPU State cs236607

35

Multithreading Client-ServerWhen a new request arrives, it is served in a

new threadThe server thread continues to listenNext, we will show an EchoServer that can

handle concurrent requests

cs236607

Page 36: 1 cs236607. 2 3 Process 1 Processes and Threads Environment Stack Code Heap CPU State Process 2 Environment Stack Code Heap CPU State cs236607

36

Serverimport java.net.*; import java.io.*;

public class EchoServer { public static void main(String[] args) throws IOException {

ServerSocket serverSocket = new ServerSocket(8000);

while (true) { try { Socket socket = serverSocket.accept(); new EchoRequestHandler(socket).start(); } catch (Exception e) { System.err.println("Error: " + e.getMessage()); }}}}

cs236607

Page 37: 1 cs236607. 2 3 Process 1 Processes and Threads Environment Stack Code Heap CPU State Process 2 Environment Stack Code Heap CPU State cs236607

37

Request Handlerpublic class EchoRequestHandler extends Thread { Socket socket = null;

public EchoRequestHandler(Socket socket) { this.socket = socket; }

public void run() {... next slide ... }

}

cs236607

Page 38: 1 cs236607. 2 3 Process 1 Processes and Threads Environment Stack Code Heap CPU State Process 2 Environment Stack Code Heap CPU State cs236607

38

Request Handler public void run() { try { BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream())); PrintStream writer = new PrintStream(socket.getOutputStream()); String lineRead = null; while ((lineRead = reader.readLine()) != null) { writer.println("You wrote: " + lineRead); writer.flush(); } } catch (IOException exp) { System.err.println("Error: " + exp.getMessage());} finally { try { if (!socket.isClosed()) socket.close(); } }}

cs236607

Page 39: 1 cs236607. 2 3 Process 1 Processes and Threads Environment Stack Code Heap CPU State Process 2 Environment Stack Code Heap CPU State cs236607

39cs236607

Page 40: 1 cs236607. 2 3 Process 1 Processes and Threads Environment Stack Code Heap CPU State Process 2 Environment Stack Code Heap CPU State cs236607

40

Thread SynchronizationConsider the following consumer-producer

scenarioA single cookie can be placed in a jar and taken

from it later.

cs236607

Page 41: 1 cs236607. 2 3 Process 1 Processes and Threads Environment Stack Code Heap CPU State Process 2 Environment Stack Code Heap CPU State cs236607

41

The Simpson Simulationpublic class CookieJar { int contents; boolean hasCookie = false;

public void putCookie(String who, int value) { while (hasCookie) {} contents = value; hasCookie = true; System.out.println(who + " put cookie " + value); }

public int getCookie(String who) { while (!hasCookie) {} hasCookie = false; System.out.println(who + " got cookie " + contents); return contents; }}

horribly horribly inefficient!!inefficient!!

try {Thread.sleep(1000);}catch (InterruptedException e) {}

Page 42: 1 cs236607. 2 3 Process 1 Processes and Threads Environment Stack Code Heap CPU State Process 2 Environment Stack Code Heap CPU State cs236607

42

The Simpson Simulation (cont)public class Homer implements Runnable { CookieJar jar; public Homer(CookieJar jar) { this.jar = jar; } public void eat() { int cookie = jar.getCookie("Homer"); } public void run() { for (int i = 1 ; i <= 10 ; i++) eat(); } }

cs236607

Page 43: 1 cs236607. 2 3 Process 1 Processes and Threads Environment Stack Code Heap CPU State Process 2 Environment Stack Code Heap CPU State cs236607

43

The Simpson Simulation (cont)public class Marge implements Runnable { CookieJar jar; public Marge(CookieJar jar) { this.jar = jar; } public void bake(int cookie) { jar.putCookie("Marge", cookie); } public void run() { for (int i = 0 ; i < 10 ; i++) bake(i); } }

cs236607

Page 44: 1 cs236607. 2 3 Process 1 Processes and Threads Environment Stack Code Heap CPU State Process 2 Environment Stack Code Heap CPU State cs236607

44

The Simpson Simulation (cont)public class RunSimpsons {

public static void main(String[] args) {

CookieJar jar = new CookieJar();

Homer homer = new Homer(jar); Marge marge = new Marge(jar);

new Thread(homer).start(); new Thread(marge).start(); } } cs236607

Page 45: 1 cs236607. 2 3 Process 1 Processes and Threads Environment Stack Code Heap CPU State Process 2 Environment Stack Code Heap CPU State cs236607

45

public class CookieJar { int contents; boolean hasCookie = false;

public void putCookie(String who, int value) { while (hasCookie) { sleep(); } contents = value; hasCookie = true; }

public int getCookie(String who) { while (!hasCookie) { sleep();} hasCookie = false; return contents;}}

Oops! Missed a Cookie!

HomerHomer

MargeMarge

We start at a point where there’s a cookie in the jar…

hasCookie contents

Y

N

3

4

Page 46: 1 cs236607. 2 3 Process 1 Processes and Threads Environment Stack Code Heap CPU State Process 2 Environment Stack Code Heap CPU State cs236607

46

Put green pieces Put red piecesHow can we havealternating colors?

cs236607

Page 47: 1 cs236607. 2 3 Process 1 Processes and Threads Environment Stack Code Heap CPU State Process 2 Environment Stack Code Heap CPU State cs236607

47

Race ConditionRace condition –

Two threads are simultaneously reading or modifying some shared data

The outcome of a program is affected by the order in which the program's threads are allocated CPU time

Both threads “race” for accessing shared data

When undesired, synchronization is required

cs236607

Page 48: 1 cs236607. 2 3 Process 1 Processes and Threads Environment Stack Code Heap CPU State Process 2 Environment Stack Code Heap CPU State cs236607

48

Monitors and LocksMonitors are key elements in Java's thread

synchronization Every object has a monitorAn object's monitor is used as a guardian that

watches a block of code (called a critical section) and enables only one thread to enter that code

To enter a critical section, a thread must first acquire an ownership over the corresponding monitor

cs236607

Page 49: 1 cs236607. 2 3 Process 1 Processes and Threads Environment Stack Code Heap CPU State Process 2 Environment Stack Code Heap CPU State cs236607

49

Unique Lock OwnershipsOnly one thread can own a specific monitorIf a thread A tries to enter a block under a

monitor and a different thread B has already entered that block, A will wait until B releases the monitor and (hopefully) that monitor will be passed to AHence, monitors are related to as locks

When a thread leaves the critical section, the monitor is automatically released

Threads awaiting a monitor are blocked and queued cs236607

Page 50: 1 cs236607. 2 3 Process 1 Processes and Threads Environment Stack Code Heap CPU State Process 2 Environment Stack Code Heap CPU State cs236607

50

The synchronized keywordTo monitor a block code using the monitor of

Object o, use the synchronized keyword as follows:synchronized(o) { critical-section }

synchronized method() {critical-section} is a shorthand for

method() {synchronized(this) {critical-section} }

cs236607

Page 51: 1 cs236607. 2 3 Process 1 Processes and Threads Environment Stack Code Heap CPU State Process 2 Environment Stack Code Heap CPU State cs236607

51

An Example public class BankAccount {

private float balance;

public synchronized void deposit(float amount) { balance += amount; } public synchronized void withdraw(float amount) { balance -= amount; } public synchronized void transfer (float amount, BankAccount target) { withdraw(amount); target.deposit(amount);}}

cs236607

Page 52: 1 cs236607. 2 3 Process 1 Processes and Threads Environment Stack Code Heap CPU State Process 2 Environment Stack Code Heap CPU State cs236607

52

Bank Account

deposit()

t1t2t3

cs236607

Page 53: 1 cs236607. 2 3 Process 1 Processes and Threads Environment Stack Code Heap CPU State Process 2 Environment Stack Code Heap CPU State cs236607

53

Synchronization Scopesprivate String a = "hello";private Date b = new Date();

void a() { synchronized(a) { System.out.println("In A 1"); System.out.println("In A 2"); }}void b() { synchronized(b) { System.out.println("In B 1"); System.out.println("In B 2"); }}

synchronized void a() { System.out.println("In A 1"); System.out.println("In A 2");}

synchronized void b() { System.out.println("In B 1"); System.out.println("In B 2");}

Can we get A and B prints alternately? What about here?

cs236607

Page 54: 1 cs236607. 2 3 Process 1 Processes and Threads Environment Stack Code Heap CPU State Process 2 Environment Stack Code Heap CPU State cs236607

54

Synchronization Scopesstatic String c = "world";

void a() { synchronized (c) { System.out.println("In A 1"); System.out.println("In A 2"); }}void b() { synchronized (getClass()) { System.out.println("In B 1"); System.out.println("In B 2"); }}

static synchronized void a() { System.out.println("In A 1"); System.out.println("In A 2");}

static synchronized void b() { System.out.println("In B 1"); System.out.println("In B 2");}

Uses the monitor of the Class object

Can we get A and B prints alternately? What about here?

cs236607

Page 55: 1 cs236607. 2 3 Process 1 Processes and Threads Environment Stack Code Heap CPU State Process 2 Environment Stack Code Heap CPU State cs236607

55

Synchronization ScopesWhat will happen here?

void a() { Date d = new Date(); synchronized (d) { System.out.println("In A 1"); System.out.println("In A 2"); }}

cs236607

Page 56: 1 cs236607. 2 3 Process 1 Processes and Threads Environment Stack Code Heap CPU State Process 2 Environment Stack Code Heap CPU State cs236607

ConstructorsCan we add a Synchronized keyword before a

constructor?Who should have access to an object while it

is being constructed?Who can have access to an object while it is

being constructed?That is, a constructor should not provide a

reference to itself (i.e., this) to data structures shared by other threads

cs236607 56

Page 57: 1 cs236607. 2 3 Process 1 Processes and Threads Environment Stack Code Heap CPU State Process 2 Environment Stack Code Heap CPU State cs236607

57

Back to the Simpsonspublic synchronized void putCookie(String who, int value) { while (hasCookie) { sleep(); } contents = value; hasCookie = true;}

public synchronized int getCookie(String who) { while (!hasCookie) { sleep(); } hasCookie = false; return contents;}

HomerHomer

MargeMarge

deadlock!deadlock!

cs236607

Page 58: 1 cs236607. 2 3 Process 1 Processes and Threads Environment Stack Code Heap CPU State Process 2 Environment Stack Code Heap CPU State cs236607

58

Another Deadlock Example public class BankAccount {

private float balance;

public synchronized void deposit(float amount) { balance += amount; } public synchronized void withdraw(float amount) { balance -= amount; } public synchronized void transfer (float amount, BankAccount target) { withdraw(amount); target.deposit(amount);}}

cs236607

Page 59: 1 cs236607. 2 3 Process 1 Processes and Threads Environment Stack Code Heap CPU State Process 2 Environment Stack Code Heap CPU State cs236607

59

Deadlocks

deposit()

Alice's Account Bob's Account

t1 t2

deposit()?transfer()

withdraw()

transfer()

withdraw()

AliceAccount.transfer(100, BobAccount)

BobAccount.transfer(50, AliceAccount)

cs236607

Page 60: 1 cs236607. 2 3 Process 1 Processes and Threads Environment Stack Code Heap CPU State Process 2 Environment Stack Code Heap CPU State cs236607

60

wait() and notify()Suppose that an object has some monitor, but

conditions disable it from completing the critical section

The wait/notify mechanism enables that object to release the monitor and wait until conditions are changed

cs236607

Page 61: 1 cs236607. 2 3 Process 1 Processes and Threads Environment Stack Code Heap CPU State Process 2 Environment Stack Code Heap CPU State cs236607

61

wait()The method Object.wait() requires the current

thread to own the monitor of the objectWhen called, the current thread

releases ownership on the object's monitorstops and waits until some other thread will

wake it up and the monitor will be re-obtained

cs236607

Page 62: 1 cs236607. 2 3 Process 1 Processes and Threads Environment Stack Code Heap CPU State Process 2 Environment Stack Code Heap CPU State cs236607

62

notify()Like wait, requires the object to own the monitorThe method Object.notify() wakes up an arbitrary

thread that waits on the monitor of the objectObject.notifyAll() wakes all such threadsWhen a thread is waken up, it regularly waits for

the monitor to be available (since it called Object.wait())

The thread calling notify should release the monitor for the waiting thread to continue (e.g exit the synchronized scope)

cs236607

Page 63: 1 cs236607. 2 3 Process 1 Processes and Threads Environment Stack Code Heap CPU State Process 2 Environment Stack Code Heap CPU State cs236607

63

Waiting and Notifying

synchronized (lock) { while (!resourceAvailable()) { lock.wait(); } cosumeResource();}

produceResource();synchronized (lock) { lock.notifyAll(); }

cs236607

Page 64: 1 cs236607. 2 3 Process 1 Processes and Threads Environment Stack Code Heap CPU State Process 2 Environment Stack Code Heap CPU State cs236607

64

Lock Object

ConsumerThread

ProducerThread

1. synchronized(lock){

2. lock.wait();

3. produceResource()4. synchronized(lock) {5. lock.notify();6.}

7. Reacquire lock

8.Return from wait()

9. consumeResource();10. }

cs236607

Page 65: 1 cs236607. 2 3 Process 1 Processes and Threads Environment Stack Code Heap CPU State Process 2 Environment Stack Code Heap CPU State cs236607

65

Lock Object

ConsumerThread

ProducerThread

1. synchronized(lock){

2. lock.wait();

3. produceResource()4. synchronized(lock) {5. lock.notify();6.}

7. Reacquire lock

8. Return from wait()

9. consumeResource();10. }

cs236607

Page 66: 1 cs236607. 2 3 Process 1 Processes and Threads Environment Stack Code Heap CPU State Process 2 Environment Stack Code Heap CPU State cs236607

66

Lock Object

ConsumerThread

ProducerThread

1. synchronized(lock){

2. lock.wait();

3. produceResource()4. synchronized(lock) {5. lock.notify();6.}

7. Reacquire lock

8. Return from wait()

9. consumeResource();10. }

cs236607

Page 67: 1 cs236607. 2 3 Process 1 Processes and Threads Environment Stack Code Heap CPU State Process 2 Environment Stack Code Heap CPU State cs236607

67

Lock Object

ConsumerThread

ProducerThread

1. synchronized(lock){

2. lock.wait();

3. produceResource()4. synchronized(lock) {5. lock.notify();6.}

7. Reacquire lock

8. Return from wait()

9. consumeResource();10. }

cs236607

Page 68: 1 cs236607. 2 3 Process 1 Processes and Threads Environment Stack Code Heap CPU State Process 2 Environment Stack Code Heap CPU State cs236607

68

Lock Object

ConsumerThread

ProducerThread

1. synchronized(lock){

2. lock.wait();

3. produceResource()4. synchronized(lock) {5. lock.notify();6.}

7. Reacquire lock

8. Return from wait()

9. consumeResource();10. }

cs236607

Page 69: 1 cs236607. 2 3 Process 1 Processes and Threads Environment Stack Code Heap CPU State Process 2 Environment Stack Code Heap CPU State cs236607

69

Lock Object

ConsumerThread

ProducerThread

1. synchronized(lock){

2. lock.wait();

3. produceResource()4. synchronized(lock) {5. lock.notify();6.}

7. Reacquire lock

8. Return from wait()

9. consumeResource();10. }

cs236607

Page 70: 1 cs236607. 2 3 Process 1 Processes and Threads Environment Stack Code Heap CPU State Process 2 Environment Stack Code Heap CPU State cs236607

70

Lock Object

ConsumerThread

ProducerThread

1. synchronized(lock){

2. lock.wait();

3. produceResource()4. synchronized(lock) {5. lock.notify();6.}

7. Reacquire lock

8. Return from wait()

9. consumeResource();10. }

cs236607

Page 71: 1 cs236607. 2 3 Process 1 Processes and Threads Environment Stack Code Heap CPU State Process 2 Environment Stack Code Heap CPU State cs236607

71

Lock Object

ConsumerThread

ProducerThread

1. synchronized(lock){

2. lock.wait();

3. produceResource()4. synchronized(lock) {5. lock.notify();6.}

7. Reacquire lock

8. Return from wait()

9. consumeResource();10. }

cs236607

Page 72: 1 cs236607. 2 3 Process 1 Processes and Threads Environment Stack Code Heap CPU State Process 2 Environment Stack Code Heap CPU State cs236607

72

Lock Object

ConsumerThread

ProducerThread

1. synchronized(lock){

2. lock.wait();

3. produceResource()4. synchronized(lock) {5. lock.notify();6.}

7. Reacquire lock

8. Return from wait()

9. consumeResource();10. }

cs236607

Page 73: 1 cs236607. 2 3 Process 1 Processes and Threads Environment Stack Code Heap CPU State Process 2 Environment Stack Code Heap CPU State cs236607

73

Lock Object

ConsumerThread

ProducerThread

1. synchronized(lock){

2. lock.wait();

3. produceResource()4. synchronized(lock) {5. lock.notify();6.}

7. Reacquire lock

8. Return from wait()

9. consumeResource();10. }

cs236607

Page 74: 1 cs236607. 2 3 Process 1 Processes and Threads Environment Stack Code Heap CPU State Process 2 Environment Stack Code Heap CPU State cs236607

74

Lock Object

ConsumerThread

ProducerThread

1. synchronized(lock){

2. lock.wait();

3. produceResource()4. synchronized(lock) {5. lock.notify();6.}

7. Reacquire lock

8. Return from wait()

9. consumeResource();10. }

cs236607

Page 75: 1 cs236607. 2 3 Process 1 Processes and Threads Environment Stack Code Heap CPU State Process 2 Environment Stack Code Heap CPU State cs236607

75

Fixed Simpson Examplepublic synchronized void putCookie(String who, int value) { while (hasCookie) { try { wait(); } catch(InterruptedException e){} } contents = value; hasCookie = true; System.out.println(who + " put cookie " + value); notifyAll(); }

public synchronized int getCookie(String who) { while (!hasCookie) { try{ wait(); } catch(InterruptedException e){} } hasCookie = false; System.out.println(who + " got cookie " + contents); notifyAll(); return contents; }

HomerHomer

MargeMarge

cs236607

Page 76: 1 cs236607. 2 3 Process 1 Processes and Threads Environment Stack Code Heap CPU State Process 2 Environment Stack Code Heap CPU State cs236607

Using Interruptions for Communication Between ThreadsAn interrupt is an indication to a thread that

it should stop what it is doing and do something else

A thread sends an interrupt by invoking interrupt on the Thread object for the thread to be interrupted

cs236607 76

for (int i = 0; i < inputs.length; i++) { heavyTask(inputs[i]); if (Thread.interrupted()) { //We've been interrupted return; } }

Checks if it has been

interrupted

Page 77: 1 cs236607. 2 3 Process 1 Processes and Threads Environment Stack Code Heap CPU State Process 2 Environment Stack Code Heap CPU State cs236607

More MethodsisInterupted() – can be used by a thread to

check if another thread has been interruptedisAlive() – can be used by a thread to check if

another thread is alivejoin() – allows one thread to wait for the

completion of another

cs236607 77

Page 78: 1 cs236607. 2 3 Process 1 Processes and Threads Environment Stack Code Heap CPU State Process 2 Environment Stack Code Heap CPU State cs236607

78

http://java.sun.com/docs/books/tutorial/essential/threads/index.html

http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Thread.html

cs236607