1 / 25

Concurrency

Concurrency. Synchronizing threads, thread pools, etc. Benefits of threads More CPU’s. Many modern computers has more than one CPU Dual Core By dividing the program into more threads the program can utilize the CPU’s The program will run faster. Benefits of threads.

pascha
Download Presentation

Concurrency

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. Concurrency Synchronizing threads, thread pools, etc. Threads in Java

  2. Benefits of threadsMore CPU’s • Many modern computers has more than one CPU • Dual Core • By dividing the program into more threads the program can utilize the CPU’s • The program will run faster Threads in Java

  3. Benefits of threads • A thread may be waiting for something to happen • File to be read • Incoming network requests • Sleep() • Wait() • While one thread is waiting other threads should use the CPU • The program will run faster • Figure from • http://jayford040.blogspot.com/2009/07/process-concept-operating-system.html Threads in Java

  4. Class A extends Thread { public void run() { … } } A a = new A(); a.start(); Class A can notextend other classes. Class B implements Runnable { public void run() { … } } B b = new B(); Tread t = new Thread(b); t.start(); Class Bmay extend another class. Decouples task submission from thread scheduling, etc. Creating threads Threads in Java

  5. Somemethodson the classThread start() Starts the thread join() waits for the thread to die Runnable printerA = new Printer("Anders", 10); Thread threadA = new Thread(printerA); threadA.start(); // threadA.join(); System.out.println( "Main is done"); Joining treads Threads in Java

  6. Interrupting threads • You can interrupt a thread. • An interrupted thread should stop doing what it is doing, and do something else. • Like stop running • Example • Interrupting (NetBeans project) • Threads in the “waiting state” receives an InterruptedException • Thrown by sleep(), etc. • A thread may ask if it has been interrupted • boolean Thread.interrupted() • Static method in the Thread class (like sleep(…)) Threads in Java

  7. Synchronizing threads • Having threads in a programming language is a nice feature, but threads must to be controlled. • If you have 2 or more threads with reference to the same object, the threads might execute methods on that object simultaneously. • This must be controlled Threads in Java

  8. Race conditions and critical sections • Race condition • 2 or more threads are reading or writing shared data and the final result depends on the timing of the thread scheduling. • Race conditions are generally a bad thing! • Critical section • Part of a program (whole method or just a part of a method) where race conditions might happen. • To avoid race conditions • We want to make sure that at most one thread executes the critical section at any point in time. • Threads must be synchronized. Threads in Java

  9. Locks on objects • Every object has an associated lock. • At most one thread can have the lock at any point in time. • A thread acquire the lock of the object when it enters a synchronized block (i.e. critical section) • If another thread holds the lock on the object the entering thread has to wait (hopefully not forever). • A thread releases the lock when it leaves the synchronized block. Threads in Java

  10. public synchronized method() { // critical section } Often the whole method is a critical section. You synchronize on the current object, i.e. this. public method() { … synchronized(obj) { // critical section } … } Sometimes only part of a method is a critical section. You synchronize on the object mentioned. Less synchronization, means more concurrency. Synchronization: Java syntax Threads in Java

  11. Reentrant locks • If a thread has a the lock on some object, and then calls another synchronized method, • The thread does not have to wait for itself to release the lock • The lock is not released when it leaves the latter method • Since the thread had the lock before entering the method. Threads in Java

  12. wait • Entering a synchronized method a thread might realize that it is not in a state to fulfill its task –and it cannot simply return from the method because the return value is not “ready”. • The thread must wait • Call wait() • Method from class Object • wait() really means this.wait() • Releases the lock on the object • Another thread can run, hopefully “fixing the state” • Waits for another thread to call a notify() or notifyAll() Threads in Java

  13. notify() and notifyAll() • obj.notify() • Wakes up a single thread waiting on obj. • The thread is randomly chosen • obj.notifyAll() • Wakes up all threads waiting on obj. • Generally you want to use notifyAll() not notify() • Example • ThreadBank, package waitingacccount Threads in Java

  14. Wait “Code pattern” synchronized (obj) { while (conditionDoesNotHold) { obj.wait(); } // perform action appropriate to the condition } • http://download.oracle.com/javase/tutorial/essential/concurrency/guardmeth.html • wait() should always be called in a loop, since obj.notifyAll() wakes up all thread waiting for the object. • Only the first (quickest) should execute the synchronized block. Other thread go back to wait. Threads in Java

  15. 3 versions of wait • wait() • Waits indefinitely for notification • hopefully not forever • wait(long timeout) • Waits for notification or until timeout milliseconds has elapsed • wait(long timeout, int nanos) • Waits for notification or until timeout milliseconds + nanos nanoseconds have elapsed. Threads in Java

  16. Thread pools • Creating a new thread object takes relatively much time • Idea: Recycle thread objects. • Keep threads in a thread pool. • Request thread from pool. • Put used threads back into the pool. • java.util.concurrent offers more implementations of this idea • New in Java 5.0 • Example • http://java.sun.com/j2se/1.5.0/docs/api/java/util/concurrent/ExecutorService.html Threads in Java

  17. Thread pool implementations • The class Executors has static methods to create thread pools • ExecutorService newFixedThreadPool(int nThreads) • Pool of a fixed size • ExecutorService newCachedThreadPool() • Creates new threads as needed. • New threads are added to the pool, and recycled. • ExecutorService has an execute method • void execute(Runnable command) Threads in Java

  18. Callable has 1 method V call() throws Exception Can return a value Can throw exceptions Can be executed using an Executor object Runnable has 1 method void run() No return value No exceptions Can be executed using an Executor object Callable vs. runnable interface Threads in Java

  19. Collection framework • Vector and HashTable • Old collections • From before the collections framework • Methods are synchronized • Synchronized wrappers • Modern collections like List, Set, and Map • From the collections framework • Methods are not synchronized • Methods can be synchronized using the synchronized wrappers • Static methods in class Collections • Collection synchronizedCollection( Collection c ) • List synchronizedList( List l ) • Set synchronizedSet( Set s ) • Map synchronizedMap( Map m ) Threads in Java

  20. Threads in Java Swing • Java Swing has a single thread • Called the event-dispatching-thread (EDT) • Event-handling • Painting visual components • Creating and showing the GUI should be executed by the EDT • To avoid deadlock Threads in Java

  21. Methods in javax.swing.Utilities • The class javax.swing.Utilities has 2 interesting static methods • SwingUtilities.invokeLater(Runnable r) • Returns as soon as r is handed over to the EDT. • Asynchronous • Used for updating visual components. • SwingUtilities.invokeAndWait(Runnable r) • Returns when r has finished running • Synchronous Threads in Java

  22. Starting a Swing based application class MyFrame extends JFrame { … } public static void main(String [] args) { Runnable r = new Runnable() { // anonymous inner class public void run() { new MyFrame().setVisible(); } } SwingUtilities.invokeLater(r); } • This is (almost) the code NetBeans generates when you make a new JFrame class! Threads in Java

  23. Use threads to improve performance • Move time-consuming tasks out of the main thread • GUI responds faster • Create special threads for the time-consuming tasks. • Examples: • Reading data from files, network connections, databases, etc. • Long running computations • Example longRunningThreadcalling run() vs. start() Threads in Java

  24. References • Sun Microsystems The Java Tutorial, Threads • http://java.sun.com/docs/books/tutorial/essential/threads/index.html • Sun Microsystems Java 2 Platform Standard Edition 5.0 API Specification: java.util.concurrent • http://java.sun.com/j2se/1.5.0/docs/api/java/util/concurrent/package-summary.html Threads in Java

  25. More references • Brian Goetz et al Java Concurrency in Practice, Addison Wesley 2006 http://javaconcurrencyinpractice.com • Doug LeaConcurrent Programming in Java 2nd edition, Addison Wesley 2000 • Oaks & WongJava Threads, O’Reilly 2004 • Niemeyer & KnudsenLearning Java, 3rd edition, O’Reilly 2005 • 9. Threads, page 249-297 Threads in Java

More Related