Java Threads Lilin Zhong Java Threads 1 2

  • Slides: 37
Download presentation
Java Threads Lilin Zhong

Java Threads Lilin Zhong

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

Java Threads 1. 2. 3. 4. New threads Threads in the running state Sleeping threads and interruptions Concurrent access problems and solutions

Java Threads 5. 6. 7. 8. Signaling with wait, notify, and notify. All When

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

Why Thread? Thread Java threads Example: Stockbroker

Why Thread? Thread Java threads Example: Stockbroker

Stockbroker Download last stock prices Check prices for warnings Analyze historical data for company

Stockbroker Download last stock prices Check prices for warnings Analyze historical data for company X Time 1 2 3

New Threads Process Public void run(); Threads

New Threads Process Public void run(); Threads

New Thread Creating a thread by: 1. Extending the Thread class 2. Implementing the

New Thread Creating a thread by: 1. Extending the Thread class 2. Implementing the Runnable interface

Creating a Thread by Extending the Thread Class Declaring public class My. Thread extends

Creating a Thread by Extending the Thread Class Declaring public class My. Thread extends Thread{ public void run(){ // Your instructions here } } Instantiating - My. Thread test. Thread=new My. Thread();

By Implementing the Runnable Interface Defining - public class My. Runnable implements Runnable{ public

By Implementing the Runnable Interface Defining - public class My. Runnable implements Runnable{ public void run(){ // Your instructions here} } Instantiating - My. Runnable first. Runnable = new My. Runnable; - Thread test. Thread = new Thread (first. Runnable);

Starting an Instance of a Thread test. Thread. start();

Starting an Instance of a Thread test. Thread. start();

Java Threads 1. New threads 2. Threads in the running state

Java Threads 1. New threads 2. Threads in the running state

Threads in the Running State Execute run methods concurrently; Cooperate, share resources, compete; Take

Threads in the Running State Execute run methods concurrently; Cooperate, share resources, compete; Take turns to run; Switch.

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

Java Threads 1. New threads 2. Threads in the running state 3. Sleeping threads and interruptions

Sleeping Threads and Interruptions -Thread. sleep(long n); e. g. Thread. sleep(5*60*1000); -sleeping. Thread. interrupt();

Sleeping Threads and Interruptions -Thread. sleep(long n); e. g. Thread. sleep(5*60*1000); -sleeping. Thread. interrupt();

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

Java Threads 1. New threads 2. Threads in the running state 3. Sleeping threads and interruptions 4. Concurrent access problems and solutions

Concurrent Access Problems and Solutions - e. g. i++; - bring “i” to register;

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: n n Using volatile to transmit single variables between two threads. Using synchronized to transmit groups of variables among multiple threads.

Volatile set 1 get V 2 Time public class transfer{ private Object value; public

Volatile set 1 get V 2 Time public class transfer{ private Object value; public set(value){ this. value=value; } public Object get(){ return value; } } 1 set V(1) copy V V copy V(2) get 2

Volatile 1 set V get 2 Time public class transfer{ private volatile Object value;

Volatile 1 set V get 2 Time public class transfer{ private volatile Object value; public set(value){ this. value=value; } public Object get(){ return value; } } 1 set V V get 2

Synchronized Thread 1 Thread 2 L 1 Time Synchronized block L 1 Current Thread

Synchronized Thread 1 Thread 2 L 1 Time Synchronized block L 1 Current Thread Waiting Thread

Synchronized public class Status. Info{ private float temperature, pressure, nitro. Concentration; public synchronized void

Synchronized public class Status. Info{ private float temperature, pressure, nitro. Concentration; public synchronized void update (float temperature, float pressure, float nitro. Concentration){ this. temperature=temperature; this. pressure=pressure; this. nitro. Concentration=nitro. Concentration; } public synchronized void analyze(){ if (is. Dangerous. Combination (temperature, pressure, nitro. Concentration); stop. Manufacture(); } }

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

Java Threads 1. 2. 3. 4. New threads Threads in the running state Sleeping threads and interruptions Concurrent access problems and solutions 5. Signaling with wait, notify, and notify. All

Signaling With Wait, Notify, and notify. All Using wait and notify by two interdependent

Signaling With Wait, Notify, and notify. All Using wait and notify by two interdependent threads Using notify. All when many threads may be waiting

Signaling With Wait, Notify, and notify. All void wait() n Waits for a condition

Signaling With Wait, Notify, and notify. All void wait() n Waits for a condition to occur void notify() n Notifies a thread that is waiting for a condition that the condition has occurred void notify. All() n Notifies all the threads waiting for a condition that the condition has occurred

Wait, Notify synchronized(this){ notify(); } L 1 Time synchronized(this){ try{ wait(); }catch(Interrupted. Exception e){}

Wait, Notify synchronized(this){ notify(); } L 1 Time synchronized(this){ try{ wait(); }catch(Interrupted. Exception e){} } L 1 Current Thread Waiting Thread

Notify. All Public class Multiplewriters{ private Object item; public synchronized void write(Object o) throws

Notify. All Public class Multiplewriters{ private Object item; public synchronized void write(Object o) throws Interrupted. Exception{ while (item!=null) wait(); item=o; notify(); // single writer, notifying one reader // is sufficient }

Notify. All Public synchronized Object read() throws Interrupted. Exception { while (item==null) wait(); Object

Notify. All Public synchronized Object read() throws Interrupted. Exception { while (item==null) wait(); Object my. Item=item; item=null; notify. All(); // multiple readers, // notify. All ensures writer notification return my. Item; } }

Java Threads 6. When threads freeze: deadlock

Java Threads 6. When threads freeze: deadlock

When Threads Freeze: Deadlock Thread 1 Thread 2 L 1 Time L 2 L

When Threads Freeze: Deadlock Thread 1 Thread 2 L 1 Time L 2 L 1 L 2 Blocks here waiting for L 1 Blocks here waiting for L 2 Current Thread Waiting Thread

Java Threads 6. When threads freeze: deadlock 7. Scheduling: problems and solutions

Java Threads 6. When threads freeze: deadlock 7. Scheduling: problems and solutions

Scheduling: Problem and Solution When does the current thread lose its turn at CPU:

Scheduling: Problem and Solution When does the current thread lose its turn at CPU: n Yield() w Thread. yield(); CPU n n n //will give up the turn on the Sleep, wait, or blocked by I/O Priority “time-slicing”

Scheduling: Problem and Solution Which thread will be the next to execute? n Specify

Scheduling: Problem and Solution Which thread will be the next to execute? n Specify the priority of threads w set. Priority(int n) n=1 to 10 n Main 5 w Static final ints: n n n MIN_PRIORITY NORM_PRIORITY MAX_PRIORITY 1 5 10

Java Threads 6. When threads freeze: deadlock 7. Scheduling: problems and solutions 8. The

Java Threads 6. When threads freeze: deadlock 7. Scheduling: problems and solutions 8. The end of the thread

The End of The Thread System. exit(n) Returning from run method Throwing an unchecked

The End of The Thread System. exit(n) Returning from run method Throwing an unchecked exception Blocked by I/O others

Summary How create threads by extending Thread and by implementing Runnable, and how to

Summary How 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.

Summary How to coordinate activity through time, using wait/notify model. What different thread states

Summary How 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.

Example: Neko http: //www. naviseek. com/book/java 21/day 10. shtml Neko 12. java Neko. html

Example: Neko http: //www. naviseek. com/book/java 21/day 10. shtml Neko 12. java Neko. html

References: Java Threads, 2 nd Edition, Scott Oaks & Henry Wong Multithreaded programming with

References: Java Threads, 2 nd Edition, Scott Oaks & Henry Wong Multithreaded programming with Java technology, Bil Lewis & Daniel J. Berg Teaching yourself Java 1. 2 in 21 days, Laura Lemay & Rogers Cadenhead Java How to program, third edition, Deitel & Deitel Java 2 certificate