lecture on java concurrency day 3 on feb 11, 2009

Post on 15-Jan-2015

787 Views

Category:

Technology

5 Downloads

Preview:

Click to see full reader

DESCRIPTION

Lecture on Java Concurrency Day 3 on Feb 11, 2009. (in Korean)Lectures are 4 days in all.See http://javadom.blogspot.com/2011/06/lecture-on-java-concurrency-day-3.html

TRANSCRIPT

Practical Concurrency

January, 2009

http://javadom.blogspot.com/2011/06/lecture-on-java-concurrency-day-3.html

Day 3 (9am, Feb 11, 2009)

Worker Thread ModelsSome Sample CodesCase StudyPackage java.util.concurrent

Worker Thread Models (1)

Boss-Worker (or Master-Slave)

MasterMaster

WorkerWorker

WorkerWorker

WorkerWorker

Task

RequestRequest

Task

TaskRequestRequestRequestRequest

Worker Thread Models (2)

Work Crew (or Divide and Conquer)

Prepare(Divide)Prepare(Divide)

Part Job 1Part Job 1

Part Job 2Part Job 2

Part Job 3Part Job 3

Time Progress

MergeMerge

Worker Thread Models (3)

Pipeline

Task 1 Step 1

Task 2 Step 1

Task 3 Step 1

Time Progress

Task 1 Step 2

Task 2 Step 2

Task 3 Step 2

Task 1 Step 3

Task 2 Step 3

Task 3 Step 3

Task 1

Task 2

Task 3

Step 1 Thread Step 2 Thread Step 3 Thread

CompletedIn ProgressBefore StartLegend

Sample : volatile on busy wait

Barrier blocks the number of threads until the

barrier is called the number of times

T1

T2

T1

T2

Call 1Call 2

Call 3

Time

Behavior of size 3 Barrier

T3 T3

Sample : volatile on busy wait (2) Code (Barrier.java)

private volatile int count; // sync 까지 남은 쓰레드 개수

private volatile boolean reset; // reset 상태인지 여부

루프 안에서 yield 를 하면 busy wait 이 아니므로 volatile 이 불필요함에 주의

Test Run (BarrierTest.java) 5 threads/A Barrier of size 5 All threads call sync 3 times one by one

Sample : long-lived object in worker

Shared Object Model among worker threads Servlet, JMX MBean, ProBus Adapter

Rules, … Avoid using instance member fields since

they will be accessed by multiple threads

Case : Collection Cache In Workers

Multiple collections in Cache class Some have dependencies which

should be managed in atomic way Document “Guarded By”

java.util.concurrent

History of java.util.concurrent util.concurrent of Prof. Doug Lea

http://g.oswego.edu/dl/classes/EDU/oswego/cs/dl/util/concurrent/intro.html

JSR 166 (Spec Lead : Doug Lea) Java 5 and 6

Backport-util-concurrent http://dcl.mathcs.emory.edu/util/

backport-util-concurrent/ For JDK 1.3, 1.4, 5.0(!)

Concurrent Data Structures Map

ConcurrentHashMap Lock striping

List/Set CopyOnWriteArrayList/CopyOnWriteArraySet

Copying backing array every time the collection is modified

Queue BlockingQueue

Deque (Java 6) BlockingDeque

Queue interface

Extends Collection I/F boolean add(java.lang.Object e) java.lang.Object element() boolean offer(java.lang.Object e) java.lang.Object peek() java.lang.Object poll() java.lang.Object remove()

Concurrency Artifacts

CountDownLatch CyclicBarrier Semaphore

Acquire : decrease count Release : increase count

Exchanger

Executor & Future

Executor New way to execute threads Executes Runnable/Callable

Future Asynchronous execution

FutureTask An impl class of Future I/F

top related