90 likes | 226 Views
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.
E N D
Concurrency Synchronizing threads, thread pools, etc. Threads in Java
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
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
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
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
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
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
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
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