1 / 9

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 Threads waiting.

Télécharger la présentation

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 threadsThreads waiting • 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(); A may not extend other classes. Class B implements Runnable { public void run() { … } } B b = new B(); Tread t = new Thread(b); t.start(); B may extend another class. Decouples task submission from thread scheduling, etc. Creating threads Threads in Java

  5. Some methods on the class Thread 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. 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

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

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

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