1 / 37

Concurrency in Java

Concurrency in Java. Brad Vander Zanden. Processes and Threads. Process: A self-contained execution environment Thread: Exists within a process and shares the process’s resources with other threads. Java’s Thread Mechanism. Low Level Thread Class Runnable Interface

jaimin
Download Presentation

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 Brad Vander Zanden

  2. Processes and Threads • Process: A self-contained execution environment • Thread: Exists within a process and shares the process’s resources with other threads

  3. Java’s Thread Mechanism • Low Level • Thread Class • Runnable Interface • High Level: Thread executors and tasks

  4. Runnable Interface public class HelloRunnable implements Runnable { public void run() { System.out.println("Hello from a thread!"); } public static void main(String args[]) { (new Thread(new HelloRunnable())).start(); } }

  5. Subclassing Thread public class HelloThread extends Thread { public void run() { System.out.println("Hello from a thread!"); } public static void main(String args[]) { (new HelloThread()).start(); } }

  6. Thread vs. Runnable • Runnable allows you to subclass another object • Thread is more direct and a bit simpler

  7. Pacing a Thread • Thread.sleep(ms) suspends execution for the specified period • gives up processor • allows thread to pace execution, such as when doing an animation

  8. Handling Interrupts • Interrupt() method may be invoked on a thread to notify it of an interrupt • Ways to handle an interrupt • Catch InterruptedException: Thrown by methods like sleep and wait • Call Thread.interrupted() • Interrupt status flag • Checked by interrupted • Cleared by InterruptedException or by calling interrupted()

  9. Examples for (int i = 0; i < importantInfo.length; i++) { // Pause for 4 seconds try { Thread.sleep(4000); } catch (InterruptedException e) { // We've been interrupted: no more messages. return; } // Print a message System.out.println(importantInfo[i]); }

  10. Examples for (int i = 0; i < inputs.length; i++) { heavyCrunch(inputs[i]); if (Thread.interrupted()) { // We've been interrupted: no more crunching. return; } }

  11. Join • The join method allows one thread to wait for the completion of another thread • Example: t.join() waits for the thread referenced by t to finish execution

  12. A Detailed Example • //docs.oracle.com/javase/tutorial/essential/concurrency/simple.html

  13. Synchronization • Why we need it • Thread interference: contention for shared resources, such as a counter • Memory inconsistency: if there is a happens-before relationship where thread A relies on thread B performing a write before it does a read • joins are a trivial way to handle memory inconsistency

  14. Synchronization Techniques • Synchronized Methods • Synchronized Statements/Locks • Volatile Variables

  15. Synchronized Methods public class SynchronizedCounter { private int c = 0; public synchronized void increment() { c++; } public synchronized void decrement() { c--; } public synchronized int value() { return c; } }

  16. Problem w/o Synchronization • The single expression c++ can be decomposed into three steps: • Retrieve the current value of c. • Increment the retrieved value by 1. • Store the incremented value back in c.

  17. A Bad Interleaving of Operations • A possible interleaving of Thread A and B • Thread A: Retrieve c. • Thread B: Retrieve c. • Thread A: Increment retrieved value; result is 1. • Thread B: Decrement retrieved value; result is -1. • Thread A: Store result in c; c is now 1. • Thread B: Store result in c; c is now -1.

  18. Synchronized Statements public void addName(String name) { synchronized(this) { lastName = name; nameCount++; } nameList.add(name); }

  19. Example with Multiple Locks public class MsLunch { private long c1 = 0; private long c2 = 0; private Object lock1 = new Object(); private Object lock2 = new Object(); public void inc1() { synchronized(lock1) { c1++; }} public void inc2() { synchronized(lock2) { c2++; }} }

  20. Volatile Variables • Example: volatile int x1; • Forces any change made by a thread to be forced out to main memory • Ordinarily threads maintain local copies of variables for efficiency

  21. Synchronized Method vs Volatile Variables • synchronized methods • force all of a thread’s variables to be updated from main memory on method entry • flush all changes to a thread’s variables to main memory on method exit • obtain and release a lock on the object • volatile variable • only reads/writes one variable to main memory • does no locking

  22. Happens-Before Using Wait • Object.wait(): suspends execution until another thread calls notifyAll() or notify() • Must check condition because notifyAll/notify does not specify which condition has changed • Use notify for a mutex where only one thread can use the lock • Use notifyAll for situations where all threads might be able to usefully continue

  23. Example public synchronized guardedJoy() { // keep looping until event we’re // waiting for happens while(!joy) { try { wait(); } catch (InterruptedException e) {} } System.out.println("Joy and efficiencyhavebeenachieved!"); } Thread 1 Thread 2 public synchronized notifyJoy() { joy = true; notifyAll(); }

  24. Producer-Consumer Example • http://docs.oracle.com/javase/tutorial/essential/concurrency/guardmeth.html

  25. High Level Java Concurrency • Mutex Locks • Executors • Concurrent collections • Atomic variables • Random number generation

  26. Mutex Locks • lock interface • lock(): acquires a lock and sleeps if necessary • tryLock(ms): tries to acquire a lock • returns true on success and false on failure • can specify optional ms, in which case it will timeout after that length of time • tryLock allows thread to back out without sleeping if lock is unavailable • unlock(): releases the lock • lockInterruptibly(): like lock but allows thread to be interrupted while waiting by throwing InterruptedException

  27. Mutex Example • Alphonse and Gaston bowing to one another: http://docs.oracle.com/javase/tutorial/essential/concurrency/newlocks.html

  28. Tasks and Thread Pools • A taskis a computation that you want repeated one or more times • it should be embedded in a thread • A thread pool is a pool of one or more worker threads to which tasks may be assigned • When a task is submitted to a thread pool, it is placed on a queue and ultimately executed by one of the worker threads

  29. Executors • Executors manage thread pools • Executor, a simple interface that supports launching new tasks. • ExecutorService, a subinterface of Executor, which adds features that help manage the lifecycle, both of the individual tasks and of the executor itself. • ScheduledExecutorService, a subinterface of ExecutorService, supports future and/or periodic execution of tasks.

  30. Executor Class • The Executor class provides a collection of factory methods that create thread pools which are managed using one of the three desired executor interfaces

  31. Executor Interface • allows you to submit Runnable tasks to a thread pool via the execute method

  32. ExecutorService • allows you to submit either Runnable or Callable tasks via the submit method • Callable tasks may return a value. This value may be retrieved using the Future object returned by the submit method. • The Future object represents the pending result of that task. • You access the result using the get() method. The thread will wait until the result is returned • The Future object also allows you to cancel the execution of the task

  33. ExecutorService (cont) • allows you to shutdown a thread pool • shutdown(): accepts no new tasks but finishes execution of all running and waiting tasks • shutdownNow() • accepts no new tasks • kills waiting tasks • tries to kill running tasks by calling interrupt(): up to each task as to whether or not they actually die

  34. ExecutorService (cont) • can submit a collection of tasks for execution using invokeAll()method • returns a list of Future object that can be monitored for task completion • takes a collection object as a parameter

  35. ScheduledExecutorService • Allows you to schedule repeating tasks • fixed rate: execute every n time units (useful for clocks) • fixed delay: execute every n time units after the termination of the current task (can cause drift in a clock) • Can cancel a repeating task by calling cancel on its returned Future object

  36. ScheduledExecutorService • Also allows you to schedule a one-shot task at a future time

  37. Example • The following example prints “beep” every 10 seconds for an hour http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ScheduledExecutorService.html

More Related