+ All Categories
Home > Technology > Lecture on Java Concurrency Day 3 on Feb 11, 2009.

Lecture on Java Concurrency Day 3 on Feb 11, 2009.

Date post: 15-Jan-2015
Category:
Upload: kyung-koo-yoon
View: 786 times
Download: 5 times
Share this document with a friend
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
Popular Tags:
15
Practical Concurrency January, 2009 http://javadom.blogspot.com/2011/06/ lecture-on-java-concurrency-day- 3.html
Transcript
Page 1: Lecture on Java Concurrency Day 3 on Feb 11, 2009.

Practical Concurrency

January, 2009

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

Page 2: Lecture on Java Concurrency Day 3 on Feb 11, 2009.

Day 3 (9am, Feb 11, 2009)

Worker Thread ModelsSome Sample CodesCase StudyPackage java.util.concurrent

Page 3: Lecture on Java Concurrency Day 3 on Feb 11, 2009.

Worker Thread Models (1)

Boss-Worker (or Master-Slave)

MasterMaster

WorkerWorker

WorkerWorker

WorkerWorker

Task

RequestRequest

Task

TaskRequestRequestRequestRequest

Page 4: Lecture on Java Concurrency Day 3 on Feb 11, 2009.

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

Page 5: Lecture on Java Concurrency Day 3 on Feb 11, 2009.

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

Page 6: Lecture on Java Concurrency Day 3 on Feb 11, 2009.

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

Page 7: Lecture on Java Concurrency Day 3 on Feb 11, 2009.

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

Page 8: Lecture on Java Concurrency Day 3 on Feb 11, 2009.

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

Page 9: Lecture on Java Concurrency Day 3 on Feb 11, 2009.

Case : Collection Cache In Workers

Multiple collections in Cache class Some have dependencies which

should be managed in atomic way Document “Guarded By”

Page 10: Lecture on Java Concurrency Day 3 on Feb 11, 2009.

java.util.concurrent

Page 11: Lecture on Java Concurrency Day 3 on Feb 11, 2009.

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

Page 12: Lecture on Java Concurrency Day 3 on Feb 11, 2009.

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

Page 13: Lecture on Java Concurrency Day 3 on Feb 11, 2009.

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

Page 14: Lecture on Java Concurrency Day 3 on Feb 11, 2009.

Concurrency Artifacts

CountDownLatch CyclicBarrier Semaphore

Acquire : decrease count Release : increase count

Exchanger

Page 15: Lecture on Java Concurrency Day 3 on Feb 11, 2009.

Executor & Future

Executor New way to execute threads Executes Runnable/Callable

Future Asynchronous execution

FutureTask An impl class of Future I/F


Recommended