Concurrency in Java

37
Concurrency in Java Brad Vander Zanden

description

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 - PowerPoint PPT Presentation

Transcript of Concurrency in Java

Page 1: Concurrency  in Java

Concurrency in Java

Brad Vander Zanden

Page 2: Concurrency  in Java

Processes and Threads

• Process: A self-contained execution environment

• Thread: Exists within a process and shares the process’s resources with other threads

Page 3: Concurrency  in Java

Java’s Thread Mechanism

• Low Level– Thread Class– Runnable Interface

• High Level: Thread executors and tasks

Page 4: Concurrency  in Java

Runnable Interfacepublic 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(); }

}

Page 5: Concurrency  in Java

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(); }

}

Page 6: Concurrency  in Java

Thread vs. Runnable

• Runnable allows you to subclass another object

• Thread is more direct and a bit simpler

Page 7: Concurrency  in Java

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

Page 8: Concurrency  in Java

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()

Page 9: Concurrency  in Java

Examplesfor (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]);}

Page 10: Concurrency  in Java

Examples

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

Page 11: Concurrency  in Java

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

Page 12: Concurrency  in Java

A Detailed Example

• //docs.oracle.com/javase/tutorial/essential/concurrency/simple.html

Page 13: Concurrency  in Java

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

Page 14: Concurrency  in Java

Synchronization Techniques

• Synchronized Methods• Synchronized Statements/Locks• Volatile Variables

Page 15: Concurrency  in Java

Synchronized Methodspublic class SynchronizedCounter { private int c = 0;

public synchronized void increment() { c++; }

public synchronized void decrement() { c--; }

public synchronized int value() { return c; }}

Page 16: Concurrency  in Java

Problem w/o Synchronization

• The single expression c++ can be decomposed into three steps:1. Retrieve the current value of c.2. Increment the retrieved value by 1.3. Store the incremented value back in c.

Page 17: Concurrency  in Java

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.

Page 18: Concurrency  in Java

Synchronized Statements

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

Page 19: Concurrency  in Java

Example with Multiple Lockspublic 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++; }}}

Page 20: Concurrency  in Java

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

Page 21: Concurrency  in Java

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

Page 22: Concurrency  in Java

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

Page 23: Concurrency  in Java

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 efficiency have been achieved!");}

public synchronized notifyJoy() { joy = true; notifyAll();}

Thread 1 Thread 2

Page 24: Concurrency  in Java

Producer-Consumer Example

• http://docs.oracle.com/javase/tutorial/essential/concurrency/guardmeth.html

Page 25: Concurrency  in Java

High Level Java Concurrency

• Mutex Locks• Executors• Concurrent collections• Atomic variables• Random number generation

Page 26: Concurrency  in Java

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

Page 27: Concurrency  in Java

Mutex Example

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

Page 28: Concurrency  in Java

Tasks and Thread Pools

• A task is 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

Page 29: Concurrency  in Java

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.

Page 30: Concurrency  in Java

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

Page 31: Concurrency  in Java

Executor Interface

• allows you to submit Runnable tasks to a thread pool via the execute method

Page 32: Concurrency  in Java

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

Page 33: Concurrency  in Java

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

Page 34: Concurrency  in Java

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

Page 35: Concurrency  in Java

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

Page 36: Concurrency  in Java

ScheduledExecutorService

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

Page 37: Concurrency  in Java

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