1 / 64

Threads

Threads. Written by Amir Kirsh, Dr. Yaron Kanza. Edited by Liron Blecher. Threads Overview Creating threads in Java Synchronization wait() and notify() Notes Thread Pools. Agenda. Threads Overview. – Threads allow the program to run tasks in parallel

gauri
Télécharger la présentation

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. Threads Written by Amir Kirsh, Dr. Yaron Kanza. Edited by Liron Blecher

  2. Threads Overview • Creating threads in Java • Synchronization • wait() and notify() • Notes • Thread Pools Agenda

  3. Threads Overview • – Threads allow the program to run tasks in parallel • –If two threads execute the same method, each will have its own copy of the local variables the methods uses • – In many cases threads need to be synchronized, that is, be kept not to handle the same data in memory concurrently– There are cases in which a thread needs to wait for another thread before proceeding • –Threads have access to the same memory, meaning, they can change the same objects

  4. Threads Overview • Creating threads in Java • Synchronization • wait() and notify() • Notes • Thread Pools Agenda

  5. Threads in Java The operation we want to be threaded: public class PrintNumbers { static publicvoidprintNumbers() { for(inti=0; i<1000; i++) { System.out.println( Thread.currentThread().getId() + ":"+ i); } } }

  6. Threads in Java Option 1 – extending class Thread: publicclass Thread1 extends Thread { @Override publicvoidrun() { System.out.println("Thread1 ThreadId: " + Thread.currentThread().getId()); // do our thing PrintNumbers.printNumbers(); } }

  7. Threads in Java Option 1 – extending class Thread (cont’): staticpublicvoidmain(String[] args) { System.out.println("Main ThreadId: " + Thread.currentThread().getId()); for(int i=0; i<3; i++) { new Thread1().start(); // don't call run! // (if you want a separate thread) } printNumbers(); }

  8. Threads in Java Option 2 – implementing Runnable: publicclassMyRunnableimplements Runnable { @Override publicvoidrun() { System.out.println("Thread2 ThreadId: " + Thread.currentThread().getId()); // do our thing PrintNumbers.printNumbers(); } }

  9. Threads in Java Option 2 – implementing Runnable (cont’): staticpublicvoidmain(String[] args) { System.out.println("Main ThreadId: " + Thread.currentThread().getId()); for(inti=0; i<3; i++) { new Thread(newMyRunnable()).start(); // again, don't call run! // (if you want a separate thread) } printNumbers(); }

  10. Threads in Java Option 3 – implementing Runnable as Anonymous: staticpublicvoid main(String[] args) { System.out.println("Main ThreadId: " + Thread.currentThread().getId()); new Thread(new Runnable() { @Override publicvoid run() { System.out.println("Thread3 ThreadId: " + Thread.currentThread().getId()); // do our thing printNumbers(); } }).start(); // don't call run! ... printNumbers(); }

  11. examples.threads.intro demo

  12. Threads Overview • Creating threads in Java • Thread Scheduling • Synchronization • wait() and notify() • Notes • Thread Pools Agenda

  13. Thread Scheduling • – Usually, threads run one at a time • – Unless several processors are being used • – Thread execution is merged in order to provide concurrency

  14. start() Ready queue Newly created threads Blocked queue Currently executed thread • Waiting for I/O operation to be completed • Waiting to be notified • Sleeping • Waiting to enter a synchronized section Thread Scheduling I/O operation completes

  15. Don’t get confused with Runnable interface Thread Scheduling Thread.getState() Running Alive new PlusPrinter(); for (…) { … } New Dead Runnable thread.start(); run() method returns Blocked Object.wait() Thread.sleep() blocking IO call waiting on a monitor

  16. Thread Scheduling • Threadschedulingis the mechanism used to determine how Runnable threads (in the ready queue) are allocated CPU time • Java scheduling is preemptive, priority based • Each thread has a priority - an integer number Use thread.getPriority()/setPriority() to control priorities • In principle, the runtime system chooses the thread that has the highest priority Priority has range 1-10 What is the priority of the main thread?

  17. Thread Scheduling • If several threads have the same priority, an arbitrary one is chosen • Scheduling 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 system Mainly used for sharing CPU among equal highest-priority threads

  18. Thread Scheduling 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) • Ahigher-prioritythread becomes Runnable • On systems that support time-slicing, its time allotment has expired • The thread yields(discussed later)

  19. Scheduling is OS Dependent! • Java maps Java threads to OS threads • In particular, Java relies on the operating system for • Time slicing • Priorities • Thus, scheduling differs from one system to another • Do not count on scheduling and priorities for algorithm correctness!

  20. Relinquishing the CPU • A running thread can explicitly relinquish the CPU for other threads to use • The 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)

  21. Daemon Threads • There 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.setDaemon()

  22. Threads Overview • Creating threads in Java • Thread Scheduling • Synchronization • wait() and notify() • Notes • Thread Pools Agenda

  23. Synchronization Synchronization of threads is needed for in order to control threads coordination, mainly in order to prevent simultaneous operations on dataFor simple synchronization Java provides the synchronized keywordFor more sophisticated locking mechanisms, starting from Java 5, the package java.concurrent.locks provides additional locking options, see: http://docs.oracle.com/javase/6/docs/api/java/util/concurrent/locks/package-summary.html

  24. Synchronization • Monitors are key elements in Java's thread synchronization • Every object has a monitor • An 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

  25. Synchronization • Only one thread can own a specific monitor • If a thread Atries to enter a block under a monitor and a different thread B has already entered that block, A will wait until Breleases the monitor and (hopefully) that monitor will be passed to A • Hence, 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

  26. Synchronization Example 1 – synchronizing methods: public class SynchronizedCounter { private int c = 0; public synchronized void increment() { c++; } public synchronized void decrement() { c--; } public synchronized int value() { return c; } } The synchronized keyword on a method means that if this is already locked anywhere(on this method or elsewhere) by another thread,we need to wait till this is unlocked before entering the method

  27. Synchronization Example 2 – synchronizing blocks: public void addName(String name) { synchronized(this) { lastName = name; nameCount++; } nameList.add(name); } When synchronizing a block, key for the locking should be supplied (usually would be this)The advantage of not synchronizing the entire method is efficiency

  28. Synchronization Example 3 – synchronizing using different locks: public class TwoCounters { private long c1 = 0, c2 = 0; private Object lock1 = new Object(); private Object lock2 = new Object(); public void inc1() { synchronized(lock1) { c1++; } } public void inc2() { synchronized(lock2) { c2++; } } } You must be absolutely sure that there is no tie between c1 and c2

  29. examples.threads.deadlock demo

  30. Synchronization Example 4 – synchronizing static methods: public class Screen { private static Screen theScreen; private Screen(){…} // private c’tor public static synchronizedScreen getScreen() { if(theScreen == null) { theScreen = new Screen(); } return theScreen; } } This is a Singleton example It is not the mostefficient way to implement Singleton in Java

  31. Synchronization Example 4 – synchronizing static methods … Having a static method be synchronized means that ALL objects of this type are locked on the method and can get in one thread at a time.The lock is the Class object representing this class. The performance penalty might be sometimes too high –needs careful attention!

  32. Synchronization Example 4’ – a better singleton: public class Screen { private static Screen theScreen = new Screen(); private Screen(){…} // private c’tor public static getScreen() { return theScreen; } } No synchronization

  33. Threads Overview • Creating threads in Java • Thread Scheduling • Synchronization • wait() and notify() • Notes • Thread Pools Agenda

  34. wait(), notify(), notifyAll() wait() and notify() allows a thread towait for an eventA call to notifyAll() allows all threads that are on wait() with the same lock to be releasedA call to notify() allows one arbitrary thread that is on a wait() with the same lock to be releasedRead:(a) http://java.sun.com/docs/books/tutorial/essential/concurrency/guardmeth.html (b) http://java.sun.com/javase/6/docs/api/java/lang/Object.html#wait() Instead of “busy wait” or sleep loop!

  35. wait(), notify(), notifyAll() • 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

  36. wait() • The method Object.wait() requires the current thread to own the monitor of the object • When called, the current thread • releases ownership on the object's monitor • stops and waits until some other thread will wake it up andthe monitor will be re-obtained

  37. notify() • Likewait, requires the object to own the monitor • The method Object.notify() wakes up an arbitrary threadthat waits on the monitor of the object • Object.notifyAll() wakes all such threads • When a thread is waken up, it regularly waits for the monitor to be available (since it called Object.wait()) • The thread calling notifyshould release the monitor for the waiting thread to continue (e.g exit thesynchronizedscope)

  38. wait(), notify(), notifyAll() Example(from http://java.sun.com/docs/books/tutorial/essential/concurrency/example/Drop.java): public class Drop { // Message sent from producer to consumer private String message; // A flag, True if consumer should wait for // producer to send message, False if producer // should wait for consumer to retrieve message private boolean empty = true; ... Flag must be used, never count only on the notify

  39. wait(), notify(), notifyAll() Example (cont’) Must be in synchronized context public class Drop { ... public synchronized String take() { // Wait until message is available while (empty) { // we do nothing on InterruptedException // since the while condition is checked anyhow try { wait(); } catch (InterruptedException e) {} } // Toggle status and notify on the status change empty = true; notifyAll(); return message; } ... }

  40. wait(), notify(), notifyAll() Example (cont’) Must be in synchronized context public class Drop { ... public synchronized void put(String message) { // Wait until message has been retrieved while (!empty) { // we do nothing on InterruptedException // since the while condition is checked anyhow try { wait(); } catch (InterruptedException e) {} } // Toggle status, store message and notify consumer empty = false; this.message = message; notifyAll(); } ... }

  41. examples.threads.producerconsumer demo

  42. Threads Overview • Creating threads in Java • Thread Scheduling • Synchronization • wait() and notify() • Notes • Thread Pools Agenda

  43. Notes • Threads inherit their priority and daemon properties from their creating threads • The methodthread.join() blocks and waits until the thread completes running • A thread can have a namefor identification • Stopping a running thread was possible in old versions of Java, but it is now deprecated • Instead, interruptionmechanisms should be used (thread.interrupt())

  44. Notes • isInterupted() – can be used by a thread to check if another thread has been interrupted • isAlive() – can be used by a thread to check if another thread is alive • join() – allows one thread to wait for the completion of another

  45. examples.threads.poetthread demo

  46. Notes Some more reading material: 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

  47. Threads Overview • Creating threads in Java • Thread Scheduling • Synchronization • wait() and notify() • Notes • Thread Pools Agenda

  48. What is a Thread Pool? • A collection of threads that are createdonce (e.g. when a server starts) • That is, no need to create a new thread for every client request • Instead, the server uses an already prepared thread if there is a free one, or waits until there is a free thread

  49. Why Using Thread Pools? • Thread pools improve resource utilization • The overhead of creating a new thread is significant • Thread pools enable applications to control and bound their thread usage • Creating too many threads in one JVM can cause the system to run out of memory and even crash • There is a need to limit the usage of system resources such as connections to a database

  50. Thread Pools in Servers • Thread pools are especially important in client-server applications • The processing of each individual task is short-lived and the number of requests is large • Servers should not spend more time and consume more system resources creating and destroying threads than processing actual user requests • When too many requests arrive, thread pools enable the server to force clients to wait until threads are available

More Related