130 likes | 301 Views
Concurrency in Java. Last Updated: Fall 2009. Paul Ammann SWE 619. Agenda. Some General Concurrency Mechanisms Concurrency in Java Synchronization in Java Rules for Threads Liskov Abstraction Example. Concepts to be covered. Threads, concurrency Interleaving, race conditions
E N D
Concurrency in Java Last Updated: Fall 2009 Paul Ammann SWE 619
Agenda • Some General Concurrency Mechanisms • Concurrency in Java • Synchronization in Java • Rules for Threads • Liskov Abstraction Example
Concepts to be covered • Threads, concurrency • Interleaving, race conditions • Atomicity of execution • Mutual exclusion • Thread states • Synchronization, locks • Inter-thread communication
Some General Concurrency Mechanisms • Rendevous (Ada) • Obviates need for certain low level primitives • In practice: Web Servers • Single server, scores of clients! Waiting time? • Threads • Subject of today’s talk • Database mechanisms • Concurrency entirely hidden from user • ACID properties
Interleaving c.inc(); // c is a counter c.inc(); System.out.println(c.get()); • Suppose 2 threads are created • Each executes the 3 statements • Order in which they are executed? • Does thread 1 get to execute all 3 before thread 2?
Atomicity of execution • What is the smallest unit of execution that cannot be interleaved? • Can we ensure atomicity across multiple statements? // broken because of interleaving private static int nextSerialNumber = 0; // in short nSN public static int generateSerialNumber(){ return nextSerialNumber++; } // nSN = nSN + 1
Atomic read and write Thread 1 reads nSN = 4 Thread 2 reads nSN = 4 //should read 5 Thread 3 reads nSN = 4 //should read 6 Thread 1 writes nSN = 5 Thread 2 writes nSN = 5 //should write 6 Thread 4 reads nSN = 5 //should read 7 Time
Concurrency in Java • Various mechanisms to obtain a Thread • Extend the Thread class class T extends Thread { … public void run() {…} } Thread t = new T(); t.start(); // start() calls run() • Implement the Runnable (or Callable) interface • Use an ExecutorService • Examples: Counter1.java, Counter2.java, Counter3.java, Counter4.java
Thread states • Initial – prior to start() • Runnable – started, but not necessarily running (may be preempted) • Blocked – waiting for some event to occur • Stop – the run() method has returned (avoid other ways of entering Stop state) • The join() method waits for a thread to stop • Or ExecutorService shutdown(), get() methods
Synchronization in Java • Threads use a “lock” model • Lock is associated with an object • “this” for synchronized methods • Specified object for synchronized block • Security/Reliability implications for public locks • Only one thread at a time may hold the lock • Examples • Wacky.java, Wacky1.java • BoundedQueue.java • SyncBoundedQueue.java
Java Synchronization • Every Java object has an associated lock • “synchronized” makes a method lockable • A thread that locked a lock is said to be holding the lock • When a thread unlocks a lock, the thread is said to release the lock • Synchronization: mutual exclusion and signal-wait
Communication among Threads • wait() – enter blocked state and release the lock • notify() – wake up some (other) blocked thread • notifyAll() – wake up all the (other) blocked threads. • This is harder than it looks! • Example: BoundedQueueWithGuards.java
What can go Wrong? • Deadlock – no progress possible • Starvation – no access to a resource • Simultaneous access to shared resources – unpredictable behavior • Various tools to analyze these issues