Concurrency in Java (Shooting yourself in the foot) n.

80
Concurrency in Java (Shooting yourself in the foot) n

Transcript of Concurrency in Java (Shooting yourself in the foot) n.

Page 1: Concurrency in Java (Shooting yourself in the foot) n.

Concurrency in JavaConcurrency in Java

(Shooting yourself in the foot)n(Shooting yourself in the foot)n

Page 2: Concurrency in Java (Shooting yourself in the foot) n.

AcknowledgmentAcknowledgment

This lecture is derived almost exclusively from

Java Concurrency in Practice by Goetz, et. al.http://

www.javaconcurrencyinpractice.com

Additional notes from Dr. Dan Wallach

This lecture is derived almost exclusively from

Java Concurrency in Practice by Goetz, et. al.http://

www.javaconcurrencyinpractice.com

Additional notes from Dr. Dan Wallach

Page 3: Concurrency in Java (Shooting yourself in the foot) n.

OutlineOutline

BackgroundBasic Thread SafetyConcurrent Object

ManagementConcurrent Library

ComponentsTask Execution and

Shutdown

BackgroundBasic Thread SafetyConcurrent Object

ManagementConcurrent Library

ComponentsTask Execution and

Shutdown

Page 4: Concurrency in Java (Shooting yourself in the foot) n.

Next TimeNext Time

Thread PoolsGUI ApplicationsSafety and PerformanceDocumentation and TestingAdv. Topics

Explicit LocksCustom SynchronizersNonblocking Synchronization

Thread PoolsGUI ApplicationsSafety and PerformanceDocumentation and TestingAdv. Topics

Explicit LocksCustom SynchronizersNonblocking Synchronization

Page 5: Concurrency in Java (Shooting yourself in the foot) n.

BackgroundBackground

Why concurrency?Resource utilization - why

wait?Fairness - why wait?Convenience - why wait?

First concurrency: processes

Later: threads

Why concurrency?Resource utilization - why

wait?Fairness - why wait?Convenience - why wait?

First concurrency: processes

Later: threads

Page 6: Concurrency in Java (Shooting yourself in the foot) n.

Benefits of ThreadsBenefits of Threads

Responsiveness (esp. GUIs)

Exploiting multi-processorsSimplicity of modelingSimplified handling of

asynchronous events

Responsiveness (esp. GUIs)

Exploiting multi-processorsSimplicity of modelingSimplified handling of

asynchronous events

Page 7: Concurrency in Java (Shooting yourself in the foot) n.

Risks of ThreadsRisks of Threads

Java’s “built-in” threads means that concurrency is NOT an advanced topic

Safety hazards (correctness)Liveness hazards (progress)Performance hazards

(happiness)

Java’s “built-in” threads means that concurrency is NOT an advanced topic

Safety hazards (correctness)Liveness hazards (progress)Performance hazards

(happiness)

Page 8: Concurrency in Java (Shooting yourself in the foot) n.

Threads are EverywhereThreads are Everywhere

Frameworks use threadsTimerServlets and JSPsRMISwing and AWT

You will use this one!

Frameworks use threadsTimerServlets and JSPsRMISwing and AWT

You will use this one!

Page 9: Concurrency in Java (Shooting yourself in the foot) n.

Starting a Java Thread

Starting a Java Thread

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(); }}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 10: Concurrency in Java (Shooting yourself in the foot) n.

OutlineOutline

BackgroundBasic Thread SafetyConcurrent Object

ManagementConcurrent Library

ComponentsTask Execution and Shutdown

BackgroundBasic Thread SafetyConcurrent Object

ManagementConcurrent Library

ComponentsTask Execution and Shutdown

Page 11: Concurrency in Java (Shooting yourself in the foot) n.

SynchronizationSynchronization

Given a mutable variable vIf multiple threads access vAND if one can modify vALL must synchronize accessThis includes read access

Never attempt to ignore thisIf sync is broken, so is the

code (even if it passes tests)

Given a mutable variable vIf multiple threads access vAND if one can modify vALL must synchronize accessThis includes read access

Never attempt to ignore thisIf sync is broken, so is the

code (even if it passes tests)

Page 12: Concurrency in Java (Shooting yourself in the foot) n.

Concurrent CorrectnessConcurrent Correctness

Fixing a broken “shared” varDon’t Share! OrMake the var immutable! OrSynchronize access

Better yet, design it rightEncapsulationImmutabilityClear specifications of

invariants

Fixing a broken “shared” varDon’t Share! OrMake the var immutable! OrSynchronize access

Better yet, design it rightEncapsulationImmutabilityClear specifications of

invariants

Page 13: Concurrency in Java (Shooting yourself in the foot) n.

What is “Thread-Safe”?

What is “Thread-Safe”?

Definitions are vague and varyProblem: what is correctness?Thread Safe:

“Correct” in multi-threaded envOR, no more broken in a mult-

threaded environment then in a single-threaded one

Thread-safe classes encapsulate all synchronization

Definitions are vague and varyProblem: what is correctness?Thread Safe:

“Correct” in multi-threaded envOR, no more broken in a mult-

threaded environment then in a single-threaded one

Thread-safe classes encapsulate all synchronization

Page 14: Concurrency in Java (Shooting yourself in the foot) n.

Example: Unsafe Counter

Example: Unsafe Counter

@NoThreadSafepublic class UnsafeSequence{ private int value; public int getNext() { return value++; }}

@NoThreadSafepublic class UnsafeSequence{ private int value; public int getNext() { return value++; }}

Page 15: Concurrency in Java (Shooting yourself in the foot) n.

What’s Wrong with That?

What’s Wrong with That?

Invariant:getNext must return a

sequenceUnlucky execution timing:

Invariant:getNext must return a

sequenceUnlucky execution timing:

Value->9 9+1->10 Value=10

Value->9 9+1->10 Value=10

Page 16: Concurrency in Java (Shooting yourself in the foot) n.

Race ConditionsRace Conditions

The problem is a race conditionDef: r.c. occurs when the

correctness of a computation depends on the relative timing of multiple threads by the runtime.

Often confused for data race

The problem is a race conditionDef: r.c. occurs when the

correctness of a computation depends on the relative timing of multiple threads by the runtime.

Often confused for data race

Page 17: Concurrency in Java (Shooting yourself in the foot) n.

Achieving SafetyAchieving Safety

Go stateless!Use atomic operationsUse locking

Go stateless!Use atomic operationsUse locking

Page 18: Concurrency in Java (Shooting yourself in the foot) n.

Using Atomics!(It’s not a treaty violation)

Using Atomics!(It’s not a treaty violation)

@ThreadSafepublic class UnsafeSequence{ private final AtomicLong value = new

AtomicLong(0); public long getNext() { return value.incrementAndGet(); }}

@ThreadSafepublic class UnsafeSequence{ private final AtomicLong value = new

AtomicLong(0); public long getNext() { return value.incrementAndGet(); }}

Page 19: Concurrency in Java (Shooting yourself in the foot) n.

LockingLocking

Solved one mutable variable by making the var atomic

What if we have 2? Can we just make them both atomic?

NOT if they are dependentWe have to lock any

combined operations

Solved one mutable variable by making the var atomic

What if we have 2? Can we just make them both atomic?

NOT if they are dependentWe have to lock any

combined operations

Page 20: Concurrency in Java (Shooting yourself in the foot) n.

ExampleExample

Suppose we have a dictionary that stores the last (key,value) pair

Setting the (k,v) pair must be locked. It is not enough to use an atomic var for k, and another one for v

Suppose we have a dictionary that stores the last (key,value) pair

Setting the (k,v) pair must be locked. It is not enough to use an atomic var for k, and another one for v

Page 21: Concurrency in Java (Shooting yourself in the foot) n.

Intrinsic LocksIntrinsic Locks

Java provides built-in locksEach object is a lock, so is

each classMarked by synchronizedEnforces

AtomicityMemory visibility (more later)

Java provides built-in locksEach object is a lock, so is

each classMarked by synchronizedEnforces

AtomicityMemory visibility (more later)

Page 22: Concurrency in Java (Shooting yourself in the foot) n.

ReentrancyReentrancy

Requesting a lock held by another causes a block

Intrinsic locks are reentrant

A thread that owns a lock, that requests the same lock, will succeed (good for inheritance!)

Requesting a lock held by another causes a block

Intrinsic locks are reentrant

A thread that owns a lock, that requests the same lock, will succeed (good for inheritance!)

Page 23: Concurrency in Java (Shooting yourself in the foot) n.

Example LockingExample Locking

@ThreadSafePublic class LockSafe<K,V> { @GuardedBy(“this”) private K key; @GuardedBy(“this”) private V value;

public synchronized setKvPair(K k, V v) { this.key = k; this.value = v; }}

@ThreadSafePublic class LockSafe<K,V> { @GuardedBy(“this”) private K key; @GuardedBy(“this”) private V value;

public synchronized setKvPair(K k, V v) { this.key = k; this.value = v; }}

Page 24: Concurrency in Java (Shooting yourself in the foot) n.

Locks: “Guarded By”Locks: “Guarded By”

“For each mutable state variable that may be accessed by more than one thread, all accesses to that variable must be performed with the same lock held. In this case, we say that the variable is guarded by that lock” (JCP 28)

“For each mutable state variable that may be accessed by more than one thread, all accesses to that variable must be performed with the same lock held. In this case, we say that the variable is guarded by that lock” (JCP 28)

Page 25: Concurrency in Java (Shooting yourself in the foot) n.

Locks: Documentation

Locks: Documentation

“Every shared, mutable variable should be guarded by exactly one lock. Make it clear to maintainers which lock that is” - (JCP p28)

“Every shared, mutable variable should be guarded by exactly one lock. Make it clear to maintainers which lock that is” - (JCP p28)

Page 26: Concurrency in Java (Shooting yourself in the foot) n.

Locks: Across Invariants

Locks: Across Invariants

“For every invariant that involves more that one variable, all the variables involved in that invariant must be guarded by the same lock” - (JCP p29)

“For every invariant that involves more that one variable, all the variables involved in that invariant must be guarded by the same lock” - (JCP p29)

Page 27: Concurrency in Java (Shooting yourself in the foot) n.

Critical ConceptCritical Concept

Any mutable state that can be concurrently accessed, must be synchronized EVERYWHERE else in the program!

EXAMPLE: TimerTask (JCP p29)

Any mutable state that can be concurrently accessed, must be synchronized EVERYWHERE else in the program!

EXAMPLE: TimerTask (JCP p29)

Page 28: Concurrency in Java (Shooting yourself in the foot) n.

Why Not Lock Everything?

Why Not Lock Everything?

Even if every method were synchronized, it doesn’t solve actions that combine those methods! (example: vector)

Also, possible liveness and/or performance problems

Poor concurrency example (JCP p30)

Even if every method were synchronized, it doesn’t solve actions that combine those methods! (example: vector)

Also, possible liveness and/or performance problems

Poor concurrency example (JCP p30)

Page 29: Concurrency in Java (Shooting yourself in the foot) n.

Lock AdviceLock Advice

“Avoid holding locks during lengthy computations or operations at risk of not completing quickly such as network or console I/O” - (JCP p32)

“Avoid holding locks during lengthy computations or operations at risk of not completing quickly such as network or console I/O” - (JCP p32)

Page 30: Concurrency in Java (Shooting yourself in the foot) n.

More SyntaxMore Syntax

Other use of synchronized

public somemethod() { synchronized(this) { // do stuff }}

Other use of synchronized

public somemethod() { synchronized(this) { // do stuff }}

Page 31: Concurrency in Java (Shooting yourself in the foot) n.

OutlineOutline

BackgroundBasic Thread Safety

Concurrent Object Management

Concurrent Library ComponentsTask Execution and Shutdown

BackgroundBasic Thread Safety

Concurrent Object Management

Concurrent Library ComponentsTask Execution and Shutdown

Page 32: Concurrency in Java (Shooting yourself in the foot) n.

Sharing ObjectsSharing Objects

Memory VisibilityShared object modification

should be visible to other threads

Without synchronization, this may not happen

Memory VisibilityShared object modification

should be visible to other threads

Without synchronization, this may not happen

Page 33: Concurrency in Java (Shooting yourself in the foot) n.

More about VisibilityMore about Visibility

“In general, there is no guarantee that the reading thread will see a value written by another thread on a timely basis, or even at all” - (JCP p33)

“In general, there is no guarantee that the reading thread will see a value written by another thread on a timely basis, or even at all” - (JCP p33)

Page 34: Concurrency in Java (Shooting yourself in the foot) n.

Public class NoVisibility { private static boolean ready; private static int number;

private static class ReaderThread extends Thread { public void run() { while (!ready) Thread.yield() System.out.println(number) } }

public static void main(String[] args) { new ReaderThread().start(); number = 42; ready = true; }}

Page 35: Concurrency in Java (Shooting yourself in the foot) n.

NoVisibility :(NoVisibility :(

NoVisibility could loop forever!

NoVisibility could write 0 through reordering

Other badness and sadness

Don’t do this!

NoVisibility could loop forever!

NoVisibility could write 0 through reordering

Other badness and sadness

Don’t do this!

Page 36: Concurrency in Java (Shooting yourself in the foot) n.

Synchronization Helps

Synchronization Helps

NoVisibility demonstrates stale data

Synchronization must be used on all shared variables, even just for reads

Enforces memory visibility

NoVisibility demonstrates stale data

Synchronization must be used on all shared variables, even just for reads

Enforces memory visibility

Page 37: Concurrency in Java (Shooting yourself in the foot) n.

A Word about 64bitA Word about 64bit

When a thread reads a var w/o synchronization, at worst it is stale, but not random

Out-of-thin-air safetyOne exception: 64 bit

numbers (double and long)Read/write can be 2 32 bit

operations!

When a thread reads a var w/o synchronization, at worst it is stale, but not random

Out-of-thin-air safetyOne exception: 64 bit

numbers (double and long)Read/write can be 2 32 bit

operations!

Page 38: Concurrency in Java (Shooting yourself in the foot) n.

Volatile VariablesVolatile Variables

Can declare a java var volatileVolatile ensures visibility, but not

locking.Volatile use can be fragile. Many

times, you shouldn’t use itGood uses of volatile

Ensure visibility of their own stateThat of the object they refer toOr indicate that an important event

has occurred (e.g. shutdown)

Can declare a java var volatileVolatile ensures visibility, but not

locking.Volatile use can be fragile. Many

times, you shouldn’t use itGood uses of volatile

Ensure visibility of their own stateThat of the object they refer toOr indicate that an important event

has occurred (e.g. shutdown)

Page 39: Concurrency in Java (Shooting yourself in the foot) n.

Use Volatile ONLY when

Use Volatile ONLY when

Writes to the var do not depend on its current value or are only written by one thread

The var does not participate with invariants with other state vars

Locking is not required for any other reason when the var is being accessed

Writes to the var do not depend on its current value or are only written by one thread

The var does not participate with invariants with other state vars

Locking is not required for any other reason when the var is being accessed

Page 40: Concurrency in Java (Shooting yourself in the foot) n.

Publication and Escape

Publication and Escape

Publishing an object - makes it available outside current scope

Many times, objects should not be published at allBreaks encapsulationNot fully constructed (!)

Object published when it should not have been is said to have escaped!

Publishing an object - makes it available outside current scope

Many times, objects should not be published at allBreaks encapsulationNot fully constructed (!)

Object published when it should not have been is said to have escaped!

Page 41: Concurrency in Java (Shooting yourself in the foot) n.

Publication MethodsPublication Methods

Public static fieldsChained publication

Special Case: published inner class

Passing to an alien method

Public static fieldsChained publication

Special Case: published inner class

Passing to an alien method

Page 42: Concurrency in Java (Shooting yourself in the foot) n.

How Escape Happens

How Escape Happens

All linked to poor designSemantic escapes:

Return ref instead of copyInadvertent chained publication

Syntactic escapes:Escaped this during construction

“Object not properly constructed”

All linked to poor designSemantic escapes:

Return ref instead of copyInadvertent chained publication

Syntactic escapes:Escaped this during construction

“Object not properly constructed”

Page 43: Concurrency in Java (Shooting yourself in the foot) n.

This Escape ExampleThis Escape Example

Public class ThisEscape { // JCP p41 public ThisEscape(Eventsource source) { source.registerListener( new EventListener() { public void onEvent(Event e) { doSomething(e); } }); }}

Page 44: Concurrency in Java (Shooting yourself in the foot) n.

Preventing EscapePreventing Escape

Thread ConfinementImmutabilitySafe Publication

Thread ConfinementImmutabilitySafe Publication

Page 45: Concurrency in Java (Shooting yourself in the foot) n.

Thread ConfinementThread Confinement

Keep mutable vars confined to a single thread

Ad-hoc = enforced by impl.Stack Confinement

Local varsViolated by publication

ThreadLocalPer thread value holding

object

Keep mutable vars confined to a single thread

Ad-hoc = enforced by impl.Stack Confinement

Local varsViolated by publication

ThreadLocalPer thread value holding

object

Page 46: Concurrency in Java (Shooting yourself in the foot) n.

Immutable ObjectsImmutable Objects

Always thread safeCan’t escape after

constructionDefinition

It’s state cannot be modified after construction

All fields are final*Properly constructed

Can have mutable variables

Always thread safeCan’t escape after

constructionDefinition

It’s state cannot be modified after construction

All fields are final*Properly constructed

Can have mutable variables

Page 47: Concurrency in Java (Shooting yourself in the foot) n.

Final FieldsFinal Fields

Can’t be modifiedAND, have special semantics

in the Java Memory model (initialization safety)

Make all fields final by default“Mostly Immutable” is better

than mutable

Can’t be modifiedAND, have special semantics

in the Java Memory model (initialization safety)

Make all fields final by default“Mostly Immutable” is better

than mutable

Page 48: Concurrency in Java (Shooting yourself in the foot) n.

Using Volatile Again!Using Volatile Again!

Volatile can be used to publish immutable objects

Still not locked, but can be safe depending on semantics

Example - JCP p49-50

Volatile can be used to publish immutable objects

Still not locked, but can be safe depending on semantics

Example - JCP p49-50

Page 49: Concurrency in Java (Shooting yourself in the foot) n.

Improper PublicationImproper Publication

If synchronization is not used to publish a mutable object, the object is not properly published

Without proper publication, there are serious problems with stale data

If synchronization is not used to publish a mutable object, the object is not properly published

Without proper publication, there are serious problems with stale data

Page 50: Concurrency in Java (Shooting yourself in the foot) n.

Safe PublicationSafe Publication

Object reference and object state must become visible at the same time!

Idioms:Initializing from static initializerStoring ref in volatile or

AtomicReferenceStoring ref in final field of

properly constructed objectStoring ref in a lock-guarded field

Object reference and object state must become visible at the same time!

Idioms:Initializing from static initializerStoring ref in volatile or

AtomicReferenceStoring ref in final field of

properly constructed objectStoring ref in a lock-guarded field

Page 51: Concurrency in Java (Shooting yourself in the foot) n.

Good News!Good News!

Java Collections safe publication examples:Key or value in Hashtable,

synchronizedMap, or ConcurrentMapInsert in Vector,

CopyOnWriteArrayList, CopyOnWriteArraySet, synchronizedList, or synchronizedSet

Insert in BlockingQueue or ConcurrentLinkedQueue

Java Collections safe publication examples:Key or value in Hashtable,

synchronizedMap, or ConcurrentMapInsert in Vector,

CopyOnWriteArrayList, CopyOnWriteArraySet, synchronizedList, or synchronizedSet

Insert in BlockingQueue or ConcurrentLinkedQueue

Page 52: Concurrency in Java (Shooting yourself in the foot) n.

More Good NewsMore Good News

Effectively Immutable Objects

Objects that are used as if they were immutable

Example: Date

Effectively Immutable Objects

Objects that are used as if they were immutable

Example: Date

Page 53: Concurrency in Java (Shooting yourself in the foot) n.

Modification Vs Visibility

Modification Vs Visibility

Safe publication ensures visibility

Synchronization is required if the object can be modified post-publication

Safe publication ensures visibility

Synchronization is required if the object can be modified post-publication

Page 54: Concurrency in Java (Shooting yourself in the foot) n.

Rules of Engagement

Rules of Engagement

Thread-Confined: no sharingShared read-only:

(effectively) immutable objectsShared thread-safe: internal

synchronizationGuarded: only access with the

associated guard

Thread-Confined: no sharingShared read-only:

(effectively) immutable objectsShared thread-safe: internal

synchronizationGuarded: only access with the

associated guard

Page 55: Concurrency in Java (Shooting yourself in the foot) n.

Designing Thread-Safe Classes

Designing Thread-Safe Classes

Identify the variables that form the object’s state

Identify the invariants that contain the state variables

Establish a policy for managing concurrent access to the object’s state

Identify the variables that form the object’s state

Identify the invariants that contain the state variables

Establish a policy for managing concurrent access to the object’s state

Page 56: Concurrency in Java (Shooting yourself in the foot) n.

Java Monitor PatternJava Monitor Pattern

Principle of instance confinement

Encapsulate all mutable state and guard it with intrinsic lock

Good coarse-grained locking

Principle of instance confinement

Encapsulate all mutable state and guard it with intrinsic lock

Good coarse-grained locking

Page 57: Concurrency in Java (Shooting yourself in the foot) n.

Delegating Thread Safety

Delegating Thread Safety

Safety can often be delgated to an internally-used thread-safe object

Collections are especially useful

Only works for 1 state var, or multiple independent vars

Safety can often be delgated to an internally-used thread-safe object

Collections are especially useful

Only works for 1 state var, or multiple independent vars

Page 58: Concurrency in Java (Shooting yourself in the foot) n.

Extending Thread-safe Classes

Extending Thread-safe Classes

BE CAREFUL! Extending means that the synchronization policy is distributed over multiple separately maintained classes!

Inheritance requires the base class to publish enough state

Wrappers require that the same lock is used

Composition is often less fragile

BE CAREFUL! Extending means that the synchronization policy is distributed over multiple separately maintained classes!

Inheritance requires the base class to publish enough state

Wrappers require that the same lock is used

Composition is often less fragile

Page 59: Concurrency in Java (Shooting yourself in the foot) n.

Extension Examples:Extension Examples:

Inheritance (JCP p72)Wrapping (JCP p72-73)Composition (JCP p74)

Inheritance (JCP p72)Wrapping (JCP p72-73)Composition (JCP p74)

Page 60: Concurrency in Java (Shooting yourself in the foot) n.

OutlineOutline

BackgroundBasic Thread SafetyConcurrent Object ManagementConcurrent Library

ComponentsTask Execution and Shutdown

BackgroundBasic Thread SafetyConcurrent Object ManagementConcurrent Library

ComponentsTask Execution and Shutdown

Page 61: Concurrency in Java (Shooting yourself in the foot) n.

Synchronized Collections

Synchronized Collections

Are thread safe, butComposite actions require

additional locking for semantic correctness

Careless use of locking leads to poor performance (think iteration)

Are thread safe, butComposite actions require

additional locking for semantic correctness

Careless use of locking leads to poor performance (think iteration)

Page 62: Concurrency in Java (Shooting yourself in the foot) n.

IteratorsIterators

The synchronized collections provide iterators

These iterators are fail-fastThrows

ConcurrentModificationException

The synchronized collections provide iterators

These iterators are fail-fastThrows

ConcurrentModificationException

Page 63: Concurrency in Java (Shooting yourself in the foot) n.

Concurrent CollectionsConcurrent Collections

Designed for concurrent access

Improved performance with a few minor trade-offs:

Your assignment: go look up the docs on ConcurrentHashMap and CopyOnWriteArrayList and try them out!

Designed for concurrent access

Improved performance with a few minor trade-offs:

Your assignment: go look up the docs on ConcurrentHashMap and CopyOnWriteArrayList and try them out!

Page 64: Concurrency in Java (Shooting yourself in the foot) n.

Blocking QueuesBlocking Queues

Support the producer-consumer pattern

Can be bounded or unbounded

Serial thread confinementTry it out!

Support the producer-consumer pattern

Can be bounded or unbounded

Serial thread confinementTry it out!

Page 65: Concurrency in Java (Shooting yourself in the foot) n.

BlockingBlocking

Threads may blockWait for I/OWait for LockWait for Thread.sleepWait for computation

put and get from BlockingQueue block

Threads may blockWait for I/OWait for LockWait for Thread.sleepWait for computation

put and get from BlockingQueue block

Page 66: Concurrency in Java (Shooting yourself in the foot) n.

InterruptionInterruption

interrupt() methodRequest a blocking thread

stopA blocking method should

throw an InterruptedExceptionIf you catch this

Propagate the exception ORRestore the interrupt

interrupt() methodRequest a blocking thread

stopA blocking method should

throw an InterruptedExceptionIf you catch this

Propagate the exception ORRestore the interrupt

Page 67: Concurrency in Java (Shooting yourself in the foot) n.

Example:Example:

Try{someBlockingMethod();

} catch (InterruptedException) {

Thread.currentThread().interrupt();

}

Try{someBlockingMethod();

} catch (InterruptedException) {

Thread.currentThread().interrupt();

}

Page 68: Concurrency in Java (Shooting yourself in the foot) n.

SynchronizersSynchronizers

Latches: one-time triggere.g. CountDownLatch

FutureTask: long-running resultHas a get() methodReturns value if doneBlocks until it is done if not

Latches: one-time triggere.g. CountDownLatch

FutureTask: long-running resultHas a get() methodReturns value if doneBlocks until it is done if not

Page 69: Concurrency in Java (Shooting yourself in the foot) n.

More SynchronizersMore Synchronizers

Semaphore: virtual permitsNow a library class!

Barriers: wait for enough threadsUseful for threaded steppingExchanger 2-party barrier

Semaphore: virtual permitsNow a library class!

Barriers: wait for enough threadsUseful for threaded steppingExchanger 2-party barrier

Page 70: Concurrency in Java (Shooting yourself in the foot) n.

OutlineOutline

BackgroundBasic Thread SafetyConcurrent Object ManagementConcurrent Library ComponentsTask Execution and

Shutdown

BackgroundBasic Thread SafetyConcurrent Object ManagementConcurrent Library ComponentsTask Execution and

Shutdown

Page 71: Concurrency in Java (Shooting yourself in the foot) n.

Advanced ExecutionAdvanced Execution

The Executor FrameworkDecouples task submission

and task executionSimple interface:

void execute(Runnable command)

Easiest way to implement a producer-consumer design in an application

The Executor FrameworkDecouples task submission

and task executionSimple interface:

void execute(Runnable command)

Easiest way to implement a producer-consumer design in an application

Page 72: Concurrency in Java (Shooting yourself in the foot) n.

Execution PoliciesExecution Policies

AnswersIn what thread will tasks be

runWhat order should they be runHow many concurrently?How many queued?“Victim” selectionSetup and Teardown

AnswersIn what thread will tasks be

runWhat order should they be runHow many concurrently?How many queued?“Victim” selectionSetup and Teardown

Page 73: Concurrency in Java (Shooting yourself in the foot) n.

Policy ExamplesPolicy Examples

Single-thread (JCP p119)1-thread-per-client (JCP

p118)Thread Pools: some built in

newFixedThreadPoolnewCachedThreadPoolnewSingleThreadExecutornewScheduleThreadPool

Single-thread (JCP p119)1-thread-per-client (JCP

p118)Thread Pools: some built in

newFixedThreadPoolnewCachedThreadPoolnewSingleThreadExecutornewScheduleThreadPool

Page 74: Concurrency in Java (Shooting yourself in the foot) n.

Cancellation & Shutdown

Cancellation & Shutdown

Reasons for cancellationUser-requestedTime-limitedEventsErrorsShutdown

Reasons for cancellationUser-requestedTime-limitedEventsErrorsShutdown

Page 75: Concurrency in Java (Shooting yourself in the foot) n.

Cancellation PolicyCancellation Policy

“how” “when” and “what” of cancellation

Not enforced by Java, but generally, use interruption for cancellation

E.g. have a cancel() method call the interrupt

“how” “when” and “what” of cancellation

Not enforced by Java, but generally, use interruption for cancellation

E.g. have a cancel() method call the interrupt

Page 76: Concurrency in Java (Shooting yourself in the foot) n.

Interruption PolicyInterruption Policy

Determines how a thread interprets an interruption request

Tasks are “guests” in a threadPreserve interruption status

Only call interrupt, when you know the interruption policy

Determines how a thread interprets an interruption request

Tasks are “guests” in a threadPreserve interruption status

Only call interrupt, when you know the interruption policy

Page 77: Concurrency in Java (Shooting yourself in the foot) n.

Detecting InterruptsDetecting Interrupts

Polling the thread’s interrupted status

Detecting an InterruptedException

Polling the thread’s interrupted status

Detecting an InterruptedException

Page 78: Concurrency in Java (Shooting yourself in the foot) n.

Non-interruptible Blocks

Non-interruptible Blocks

Synchronous socket I/O:Close the socket

Asynchronous I/O with Selector:Call wakeup

Lock Acquisition:Use the explicit Lock classHas a lockInterruptibly() method

Synchronous socket I/O:Close the socket

Asynchronous I/O with Selector:Call wakeup

Lock Acquisition:Use the explicit Lock classHas a lockInterruptibly() method

Page 79: Concurrency in Java (Shooting yourself in the foot) n.

Stopping a Thread-Based Service

Stopping a Thread-Based Service

ExceutorService extends Executor

Provides “Lifecycle” optionsshutdown()awaitTermination(timeout, unit)

Any thread based service should provide similar methods

ExceutorService extends Executor

Provides “Lifecycle” optionsshutdown()awaitTermination(timeout, unit)

Any thread based service should provide similar methods

Page 80: Concurrency in Java (Shooting yourself in the foot) n.

Waiting for ThreadsWaiting for Threads

In the “raw” case, use join()A number of Java library

classes have advanced “waits” like ExecutionService’s awaitTermination()

Obviously, if you implement your own ExecutionService, you’ll have to use the raw stuff

In the “raw” case, use join()A number of Java library

classes have advanced “waits” like ExecutionService’s awaitTermination()

Obviously, if you implement your own ExecutionService, you’ll have to use the raw stuff