1 / 13

Concurrency in Java

Concurrency in Java. Last Updated: Fall 2010. Paul Ammann SWE 619. Agenda. Some General Concurrency Mechanisms Concurrency in Java Synchronization in Java Rules for Threads. Concepts to be covered. Threads, concurrency Interleaving, race conditions Atomicity of execution

quito
Télécharger la présentation

Concurrency in Java

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 in Java Last Updated: Fall 2010 Paul Ammann SWE 619

  2. Agenda • Some General Concurrency Mechanisms • Concurrency in Java • Synchronization in Java • Rules for Threads

  3. Concepts to be covered • Threads, concurrency • Interleaving, race conditions • Atomicity of execution • Mutual exclusion • Thread states • Synchronization, locks • Inter-thread communication

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

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

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

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

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

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

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

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

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

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

More Related