Download presentation
Presentation is loading. Please wait.
Published byAubrey Jefferson Modified over 9 years ago
1
Threads in Java1 Concurrency Synchronizing threads, thread pools, etc.
2
Life Cycle of threads Threads in Java2
3
3 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
4
Threads in Java4 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.
5
Threads in Java5 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.
6
Threads in Java6 Synchronization: Java syntax 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.
7
Threads in Java7 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.
8
Threads in Java8 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()
9
Threads in Java9 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()
10
Threads in Java10 Wait “Code pattern” synchronized (obj) { while (conditionDoesNotHold) { obj.wait(); } // perform action appropriate to the condition } http://download.oracle.com/javase/tutorial/essential/conc urrency/guardmeth.htmlhttp://download.oracle.com/javase/tutorial/essential/conc urrency/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.
11
Threads in Java11 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.
12
Threads in Java12 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 )
13
Threads in Java13 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/concu rrent/ExecutorService.htmlhttp://java.sun.com/j2se/1.5.0/docs/api/java/util/concu rrent/ExecutorService.html
14
Threads in Java14 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)
15
Threads in Java15 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
16
Threads in Java16 References Sun Microsystems The Java Tutorial, Threads –http://java.sun.com/docs/books/tutorial/essential/threads/index.ht mlhttp://java.sun.com/docs/books/tutorial/essential/threads/index.ht ml 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/packa ge-summary.htmlhttp://java.sun.com/j2se/1.5.0/docs/api/java/util/concurrent/packa ge-summary.html
17
Threads in Java17 More references Brian Goetz et al Java Concurrency in Practice, Addison Wesley 2006 http://javaconcurrencyinpractice.com Doug Lea Concurrent Programming in Java 2 nd edition, Addison Wesley 2000 Oaks & Wong Java Threads, O’Reilly 2004 Niemeyer & Knudsen Learning Java, 3 rd edition, O’Reilly 2005 –9. Threads, page 249-297
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.