18
Java-7 Concurrency Masudul Haque

Java-7 Concurrency

Embed Size (px)

DESCRIPTION

 

Citation preview

Page 1: Java-7 Concurrency

Java-7 ConcurrencyMasudul Haque

Page 2: Java-7 Concurrency

Thread & Process

• Allows you to run the Java Compiler at the same time that you are using an editor

Process-based multitasking

• A text editor can format text at the same time that it is printing

Thread-based multitasking

Page 3: Java-7 Concurrency

Implement Runnable interface Subclass Thread class

Defining a Thread

Page 4: Java-7 Concurrency

Life Cycle

Page 5: Java-7 Concurrency

Thread execution◦ start() – Start a thread by calling its run() method◦ run() – Entry point for the thread

Thread blocking◦ sleep() – Suspend a thread for a period of time◦ yield() – Voluntarily relinquish thread control to another

thread of the same priority

Thread Class

Page 6: Java-7 Concurrency

Thread lifetime and termination◦ isAlive() – Determine if a thread is still running◦ join() – Wait for a thread to terminate

Thread communication◦ wait() – Instructs the calling thread to give up the monitor

and sleep until some other thread enters the same monitor and calls notify()

◦ notify() – Wakes up a thread that called wait() on the same object

◦ notifyAll() – Wakes up all threads that called wait() on the same object

Thread Class

Page 7: Java-7 Concurrency

A concurrent application's ability to execute in a timely manner is known as its liveness

Deadlock describes a situation where two or more threads are blocked forever◦Starvation describes a situation where a thread

is unable to gain regular access to shared resources

◦ A thread often acts in response to the action of another thread. If the other thread's action is also a response to the action of another thread, then livelock may result.

Liveness & Deadlock

Page 8: Java-7 Concurrency

Thread interference Memory Consistency Error

Synchronization

Page 9: Java-7 Concurrency

Concurrency Utilities

• Concurrency APIs

java.util.concurrent

• Atomic data types

java.util.concurrent.atomic

• Locks for synchronization

java.util.concurrent.locks

Page 10: Java-7 Concurrency

Synchronizer

• Implements the classic semaphore

Semaphore

• Waits until a specified number of events have occurred

CountDownLatch

• Enables a group of threads to wait at a predefined execution point

CyclicBarrier

• Exchanges data between two threads

Exchanger

Page 11: Java-7 Concurrency

Executor

Executor

ExecutorService

ThreadPool Executor

ScheduledPool Executor

Future

Callable

Page 12: Java-7 Concurrency

Executor Interface◦ Executor◦ ExecutorService◦ SheduledThreadExecutorService

Fork/Join Thread Pools

◦ ThreadPoolExecutor◦ ScheduledThreadPoolExecutor

Executors

Page 13: Java-7 Concurrency

Concurrent Collections

Concurrent HashMap

Concurrent LinkedQueue

CopyOnWrite ArrayList

Array BlockingQueue

Concurrent SkipListMap

Concurrent SkipListSet

CopyOnWrite ArraySet

DelayQueue

LinkedBlocking Deque

LinkedBlocking Queue

Prioirty BlockingQueue

Synchronous Queue

Page 14: Java-7 Concurrency

TimeUnit Enumeration

TimeUnit Enumeratio

n

DAYS

HOURS

MINUTES

SECONDS

MICRO SECOND

S

MILLI SECOND

S

NANO SECON

DS

Page 15: Java-7 Concurrency

Lock

Lock

ReentrantLock

ReadWriteLock

Reentrant ReadWriteLo

ck

Page 16: Java-7 Concurrency

Atomic Operations

AtomicInteger

AtomicLong

decrementAndGet() compareAndSet()

addAndGet()

Page 17: Java-7 Concurrency

Thread Safety

Thread

safe

Behaves correctly

(unambiguous)

Independent of scheduling

and interleaving

Without additional

synchronization

Page 18: Java-7 Concurrency

Thread Safety

synchronized

• The primary mechanism for synchronization

volatile• Enables the thread to bypass the

cache when accessing the data