1 / 37

Java Threads

Java Threads. Lilin Zhong. Java Threads. 1. New threads 2. Threads in the running state 3. Sleeping threads and interruptions 4. Concurrent access problems and solutions. Java Threads. 5. Signaling with wait, notify, and notifyAll 6. When threads freeze: deadlock

albina
Télécharger la présentation

Java Threads

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Java Threads Lilin Zhong

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

  3. 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

  4. Why Thread? • Thread • Java threads • Example: Stockbroker

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

  6. Threads New Threads Process • Public void run();

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

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

  9. 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);

  10. Starting an Instance of a Thread • testThread.start();

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

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

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

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

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

  16. 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.

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

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

  19. Synchronized Thread 1 Thread 2 L1 Synchronized block Time L1 Current Thread Waiting Thread

  20. Synchronized public 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(); } }

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

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

  23. 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 occurred • void notifyAll() • Notifies all thethreads waiting for a condition that the condition has occurred

  24. Wait, Notify synchronized(this){ try{ wait(); }catch(InterruptedException e){} } synchronized(this){ notify(); } L1 L1 Time L1 Current Thread Waiting Thread

  25. NotifyAll Public 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 }

  26. NotifyAll Public synchronized Object read() throws InterruptedException { while (item==null) wait(); Object myItem=item; item=null; notifyAll(); // multiple readers, // notifyAll ensures writer notification return myItem; } }

  27. Java Threads 6. When threads freeze: deadlock

  28. When Threads Freeze: Deadlock Thread 1 Thread 2 L1 L2 Time Blocks here waiting for L1 L1 Blocks here waiting for L2 L2 Current Thread Waiting Thread

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

  30. 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”

  31. 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

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

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

  34. 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.

  35. 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.

  36. http://www.naviseek.com/book/java21/day10.shtml Neko12.java Neko.html Example: Neko

  37. References: • Java Threads, 2nd 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

More Related