Java Threads Lilin Zhong. Java Threads 1. New threads 2. Threads in the running state 3. Sleeping...

37
Java Threads Lilin Zhong

description

Java Threads 5. Signaling with wait, notify, and notifyAll 6. When threads freeze: deadlock 7. Scheduling: problems and solutions 8. The end of the thread

Transcript of Java Threads Lilin Zhong. Java Threads 1. New threads 2. Threads in the running state 3. Sleeping...

Page 1: Java Threads Lilin Zhong. Java Threads 1. New threads 2. Threads in the running state 3. Sleeping threads and interruptions 4. Concurrent access problems.

Java Threads

Lilin Zhong

Page 2: Java Threads Lilin Zhong. Java Threads 1. New threads 2. Threads in the running state 3. Sleeping threads and interruptions 4. Concurrent access problems.

Java Threads1. New threads2. Threads in the running state3. Sleeping threads and

interruptions4. Concurrent access problems and

solutions

Page 3: Java Threads Lilin Zhong. Java Threads 1. New threads 2. Threads in the running state 3. Sleeping threads and interruptions 4. Concurrent access problems.

Java Threads5. Signaling with wait, notify, and

notifyAll6. When threads freeze: deadlock7. Scheduling: problems and

solutions8. The end of the thread

Page 4: Java Threads Lilin Zhong. Java Threads 1. New threads 2. Threads in the running state 3. Sleeping threads and interruptions 4. Concurrent access problems.

Why Thread? ThreadJava threadsExample: Stockbroker

Page 5: Java Threads Lilin Zhong. Java Threads 1. New threads 2. Threads in the running state 3. Sleeping threads and interruptions 4. Concurrent access problems.

StockbrokerDownload last stock pricesCheck prices for warningsAnalyze historical data for company X

Tim

e

1

2

31 2 3

Page 6: Java Threads Lilin Zhong. Java Threads 1. New threads 2. Threads in the running state 3. Sleeping threads and interruptions 4. Concurrent access problems.

New Threads

Public void run();

Process Threads

Page 7: Java Threads Lilin Zhong. Java Threads 1. New threads 2. Threads in the running state 3. Sleeping threads and interruptions 4. Concurrent access problems.

New ThreadCreating a thread by: 1. Extending the Thread class2. Implementing the Runnable

interface

Page 8: Java Threads Lilin Zhong. Java Threads 1. New threads 2. Threads in the running state 3. Sleeping threads and interruptions 4. Concurrent access problems.

Creating a Thread by Extending the Thread Class

Declaringpublic class MyThread extends Thread{

public void run(){// Your instructions here

}}Instantiating- MyThread testThread=new MyThread();

Page 9: Java Threads Lilin Zhong. Java Threads 1. New threads 2. Threads in the running state 3. Sleeping threads and interruptions 4. Concurrent access problems.

By Implementing the Runnable Interface

Defining- public class MyRunnable implements Runnable{

public void run(){ // Your instructions here}

}Instantiating- MyRunnable firstRunnable = new MyRunnable;- Thread testThread = new Thread (firstRunnable);

Page 10: Java Threads Lilin Zhong. Java Threads 1. New threads 2. Threads in the running state 3. Sleeping threads and interruptions 4. Concurrent access problems.

Starting an Instance of a Thread

testThread.start();

Page 11: Java Threads Lilin Zhong. Java Threads 1. New threads 2. Threads in the running state 3. Sleeping threads and interruptions 4. Concurrent access problems.

Java Threads1. New threads2. Threads in the running state

Page 12: Java Threads Lilin Zhong. Java Threads 1. New threads 2. Threads in the running state 3. Sleeping threads and interruptions 4. Concurrent access problems.

Threads in the Running State

Execute run methods concurrently;Cooperate, share resources, compete;Take turns to run;Switch.

Page 13: Java Threads Lilin Zhong. Java Threads 1. New threads 2. Threads in the running state 3. Sleeping threads and interruptions 4. Concurrent access problems.

Java Threads1. New threads2. Threads in the running state3. Sleeping threads and

interruptions

Page 14: Java Threads Lilin Zhong. Java Threads 1. New threads 2. Threads in the running state 3. Sleeping threads and interruptions 4. Concurrent access problems.

Sleeping Threads and Interruptions

-Thread.sleep(long n);e.g. Thread.sleep(5*60*1000);-sleepingThread.interrupt();

Page 15: Java Threads Lilin Zhong. Java Threads 1. New threads 2. Threads in the running state 3. Sleeping threads and interruptions 4. Concurrent access problems.

Java Threads1. New threads2. Threads in the running state3. Sleeping threads and

interruptions4. Concurrent access problems

and solutions

Page 16: Java Threads Lilin Zhong. Java Threads 1. New threads 2. Threads in the running state 3. Sleeping threads and interruptions 4. Concurrent access problems.

Concurrent Access Problems and Solutions

- e.g. i++;- bring “i” to register;

- add 1 to register;- store register onto “i”.

ways to solve the problems: Using volatile to transmit single variables

between two threads. Using synchronized to transmit groups of

variables among multiple threads.

Page 17: Java Threads Lilin Zhong. Java Threads 1. New threads 2. Threads in the running state 3. Sleeping threads and interruptions 4. Concurrent access problems.

VolatileVset get

1 V(1) copy

Tim

e

V(2) 2copyVV

1 2

public class transfer{private Object value;public set(value){ this.value=value; }public Object get(){ return value; }

}

setget

Page 18: Java Threads Lilin Zhong. Java Threads 1. New threads 2. Threads in the running state 3. Sleeping threads and interruptions 4. Concurrent access problems.

Volatile

VVTi

me 1

2

setget

V1 2

public class transfer{private volatile Object value;public set(value){ this.value=value; }public Object get(){ return value; }

}

set get

Page 19: Java Threads Lilin Zhong. Java Threads 1. New threads 2. Threads in the running state 3. Sleeping threads and interruptions 4. Concurrent access problems.

SynchronizedTi

me

Thread 1 Thread 2

Current Thread Waiting Thread

L1

L1

Synchronized block

Page 20: Java Threads Lilin Zhong. Java Threads 1. New threads 2. Threads in the running state 3. Sleeping threads and interruptions 4. Concurrent access problems.

Synchronizedpublic class StatusInfo{ private float temperature, pressure, nitroConcentration; public synchronized void update (float temperature, float pressure, float nitroConcentration){ this.temperature=temperature;

this.pressure=pressure;this.nitroConcentration=nitroConcentration;

} public synchronized void analyze(){

if (isDangerousCombination(temperature, pressure, nitroConcentration);stopManufacture();

}}

Page 21: Java Threads Lilin Zhong. Java Threads 1. New threads 2. Threads in the running state 3. Sleeping threads and interruptions 4. Concurrent access problems.

Java Threads1. New threads2. Threads in the running state3. Sleeping threads and interruptions4. Concurrent access problems and

solutions5. Signaling with wait, notify, and

notifyAll

Page 22: Java Threads Lilin Zhong. Java Threads 1. New threads 2. Threads in the running state 3. Sleeping threads and interruptions 4. Concurrent access problems.

Signaling With Wait, Notify, and notifyAll

Using wait and notify by two interdependent threadsUsing notifyAll when many threads may be waiting

Page 23: Java Threads Lilin Zhong. Java Threads 1. New threads 2. Threads in the running state 3. Sleeping threads and interruptions 4. Concurrent access problems.

Signaling With Wait, Notify, and notifyAll

void wait() Waits for a condition to occur

void notify() Notifies a thread that is waiting for a

condition that the condition has occurredvoid notifyAll() Notifies all the threads waiting for a

condition that the condition has occurred

Page 24: Java Threads Lilin Zhong. Java Threads 1. New threads 2. Threads in the running state 3. Sleeping threads and interruptions 4. Concurrent access problems.

Wait, Notifysynchronized(this){ try{ wait(); }catch(InterruptedException e){} }

synchronized(this){ notify();}

L1

Tim

e

Current ThreadWaiting Thread

L1

L1

Page 25: Java Threads Lilin Zhong. Java Threads 1. New threads 2. Threads in the running state 3. Sleeping threads and interruptions 4. Concurrent access problems.

NotifyAllPublic class Multiplewriters{ private Object item; public synchronized void write(Object o) throws InterruptedException{ while (item!=null) wait(); item=o; notify(); // single writer, notifying one reader

// is sufficient}

Page 26: Java Threads Lilin Zhong. Java Threads 1. New threads 2. Threads in the running state 3. Sleeping threads and interruptions 4. Concurrent access problems.

NotifyAllPublic synchronized Object read() throwsInterruptedException { while (item==null) wait(); Object myItem=item; item=null; notifyAll(); // multiple readers,

// notifyAll ensures writer notification return myItem; }}

Page 27: Java Threads Lilin Zhong. Java Threads 1. New threads 2. Threads in the running state 3. Sleeping threads and interruptions 4. Concurrent access problems.

Java Threads6. When threads freeze:

deadlock

Page 28: Java Threads Lilin Zhong. Java Threads 1. New threads 2. Threads in the running state 3. Sleeping threads and interruptions 4. Concurrent access problems.

When Threads Freeze: Deadlock

Tim

e

Thread 1 Thread 2

L1

Current ThreadWaiting Thread

L2

L1

L2

Blocks here waiting for L1

Blocks here waiting for L2

Page 29: Java Threads Lilin Zhong. Java Threads 1. New threads 2. Threads in the running state 3. Sleeping threads and interruptions 4. Concurrent access problems.

Java Threads6. When threads freeze: deadlock7. Scheduling: problems and

solutions

Page 30: Java Threads Lilin Zhong. Java Threads 1. New threads 2. Threads in the running state 3. Sleeping threads and interruptions 4. Concurrent access problems.

Scheduling: Problem and Solution

When does the current thread lose its turn at CPU: Yield()

Thread.yield(); //will give up the turn on the CPU

Sleep, wait, or blocked by I/O Priority “time-slicing”

Page 31: Java Threads Lilin Zhong. Java Threads 1. New threads 2. Threads in the running state 3. Sleeping threads and interruptions 4. Concurrent access problems.

Scheduling: Problem and Solution

Which thread will be the next to execute? Specify the priority of threads

setPriority(int n) n=1 to 10 Main 5

Static final ints: MIN_PRIORITY 1 NORM_PRIORITY 5 MAX_PRIORITY 10

Page 32: Java Threads Lilin Zhong. Java Threads 1. New threads 2. Threads in the running state 3. Sleeping threads and interruptions 4. Concurrent access problems.

Java Threads6. When threads freeze: deadlock7. Scheduling: problems and

solutions8. The end of the thread

Page 33: Java Threads Lilin Zhong. Java Threads 1. New threads 2. Threads in the running state 3. Sleeping threads and interruptions 4. Concurrent access problems.

The End of The ThreadSystem.exit(n)Returning from run methodThrowing an unchecked exceptionBlocked by I/Oothers

Page 34: Java Threads Lilin Zhong. Java Threads 1. New threads 2. Threads in the running state 3. Sleeping threads and interruptions 4. Concurrent access problems.

SummaryHow create threads by extending Thread and by implementing Runnable, and how to start them. How to share information effectively, threads use synchronized blocks of code.

Page 35: Java Threads Lilin Zhong. Java Threads 1. New threads 2. Threads in the running state 3. Sleeping threads and interruptions 4. Concurrent access problems.

SummaryHow to coordinate activity through time, using wait/notify model. What different thread states ( new, dead) are, when threads go in and out of sleeping, waiting, blocking on I/O, and acquiring lock states.

Page 36: Java Threads Lilin Zhong. Java Threads 1. New threads 2. Threads in the running state 3. Sleeping threads and interruptions 4. Concurrent access problems.

Example: Neko

http://www.naviseek.com/book/java21/day10.shtml

Neko12.java

Neko.html

Page 37: Java Threads Lilin Zhong. Java Threads 1. New threads 2. Threads in the running state 3. Sleeping threads and interruptions 4. Concurrent access problems.

References:Java Threads, 2nd Edition, Scott Oaks & Henry WongMultithreaded programming with Java technology, Bil Lewis & Daniel J. BergTeaching yourself Java 1.2 in 21 days, Laura Lemay & Rogers CadenheadJava How to program, third edition, Deitel & DeitelJava 2 certificate