SlideShare a Scribd company logo
1 of 9
Practical Concurrency January, 2009 http://javadom.blogspot.com/2011/06/lecture-on-java-concurrency-day-4.html
Day 4 (9am, Feb 18, 2009) Thread Pool Lock and Condition CAS (Compare And Set)
Thread Pool ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
BlockingQueue and ThreadPool ,[object Object],[object Object],[object Object],[object Object]
Thread Pool Saturation Policy ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Lock and Condition ,[object Object],[object Object],[object Object]
Atomic Variables and CAS ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
References ,[object Object],[object Object]
Q&A ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]

More Related Content

What's hot

MongoDb scalability and high availability with Replica-Set
MongoDb scalability and high availability with Replica-SetMongoDb scalability and high availability with Replica-Set
MongoDb scalability and high availability with Replica-SetVivek Parihar
 
java-monitoring-troubleshooting
java-monitoring-troubleshootingjava-monitoring-troubleshooting
java-monitoring-troubleshootingWilliam Au
 
Performance schema 설정
Performance schema 설정Performance schema 설정
Performance schema 설정EXEM
 
Matthew Vignau: Memory Management in SharePoint 2007 Development
Matthew Vignau: Memory Management in SharePoint 2007 DevelopmentMatthew Vignau: Memory Management in SharePoint 2007 Development
Matthew Vignau: Memory Management in SharePoint 2007 DevelopmentSharePoint Saturday NY
 
Oracle Database Cloud Performance Doag 2016
Oracle Database Cloud Performance Doag 2016Oracle Database Cloud Performance Doag 2016
Oracle Database Cloud Performance Doag 2016Randolf Geist
 

What's hot (7)

MongoDb scalability and high availability with Replica-Set
MongoDb scalability and high availability with Replica-SetMongoDb scalability and high availability with Replica-Set
MongoDb scalability and high availability with Replica-Set
 
java-monitoring-troubleshooting
java-monitoring-troubleshootingjava-monitoring-troubleshooting
java-monitoring-troubleshooting
 
Performance schema 설정
Performance schema 설정Performance schema 설정
Performance schema 설정
 
Matthew Vignau: Memory Management in SharePoint 2007 Development
Matthew Vignau: Memory Management in SharePoint 2007 DevelopmentMatthew Vignau: Memory Management in SharePoint 2007 Development
Matthew Vignau: Memory Management in SharePoint 2007 Development
 
Javantura v2 - Replication with MongoDB - what could go wrong... - Philipp Krenn
Javantura v2 - Replication with MongoDB - what could go wrong... - Philipp KrennJavantura v2 - Replication with MongoDB - what could go wrong... - Philipp Krenn
Javantura v2 - Replication with MongoDB - what could go wrong... - Philipp Krenn
 
Oracle Database Cloud Performance Doag 2016
Oracle Database Cloud Performance Doag 2016Oracle Database Cloud Performance Doag 2016
Oracle Database Cloud Performance Doag 2016
 
Scaling WordPress
Scaling WordPressScaling WordPress
Scaling WordPress
 

Viewers also liked

Java Concurrency Gotchas
Java Concurrency GotchasJava Concurrency Gotchas
Java Concurrency GotchasAlex Miller
 
GPars: Groovy Parallelism for Java
GPars: Groovy Parallelism for JavaGPars: Groovy Parallelism for Java
GPars: Groovy Parallelism for JavaRussel Winder
 
Basics of Java Concurrency
Basics of Java ConcurrencyBasics of Java Concurrency
Basics of Java Concurrencykshanth2101
 
concurrency gpars
concurrency gparsconcurrency gpars
concurrency gparsPaul King
 
Java concurrency
Java concurrencyJava concurrency
Java concurrencyducquoc_vn
 
Project Fortress
Project FortressProject Fortress
Project FortressAlex Miller
 
groovy and concurrency
groovy and concurrencygroovy and concurrency
groovy and concurrencyPaul King
 
Groovy concurrency
Groovy concurrencyGroovy concurrency
Groovy concurrencyAlex Miller
 
Java Course 10: Threads and Concurrency
Java Course 10: Threads and ConcurrencyJava Course 10: Threads and Concurrency
Java Course 10: Threads and ConcurrencyAnton Keks
 
Java Multithreading and Concurrency
Java Multithreading and ConcurrencyJava Multithreading and Concurrency
Java Multithreading and ConcurrencyRajesh Ananda Kumar
 
Java Concurrency in Practice
Java Concurrency in PracticeJava Concurrency in Practice
Java Concurrency in PracticeAlina Dolgikh
 
Java concurrency in practice
Java concurrency in practiceJava concurrency in practice
Java concurrency in practiceMikalai Alimenkou
 
Java Concurrency Idioms
Java Concurrency IdiomsJava Concurrency Idioms
Java Concurrency IdiomsAlex Miller
 

Viewers also liked (13)

Java Concurrency Gotchas
Java Concurrency GotchasJava Concurrency Gotchas
Java Concurrency Gotchas
 
GPars: Groovy Parallelism for Java
GPars: Groovy Parallelism for JavaGPars: Groovy Parallelism for Java
GPars: Groovy Parallelism for Java
 
Basics of Java Concurrency
Basics of Java ConcurrencyBasics of Java Concurrency
Basics of Java Concurrency
 
concurrency gpars
concurrency gparsconcurrency gpars
concurrency gpars
 
Java concurrency
Java concurrencyJava concurrency
Java concurrency
 
Project Fortress
Project FortressProject Fortress
Project Fortress
 
groovy and concurrency
groovy and concurrencygroovy and concurrency
groovy and concurrency
 
Groovy concurrency
Groovy concurrencyGroovy concurrency
Groovy concurrency
 
Java Course 10: Threads and Concurrency
Java Course 10: Threads and ConcurrencyJava Course 10: Threads and Concurrency
Java Course 10: Threads and Concurrency
 
Java Multithreading and Concurrency
Java Multithreading and ConcurrencyJava Multithreading and Concurrency
Java Multithreading and Concurrency
 
Java Concurrency in Practice
Java Concurrency in PracticeJava Concurrency in Practice
Java Concurrency in Practice
 
Java concurrency in practice
Java concurrency in practiceJava concurrency in practice
Java concurrency in practice
 
Java Concurrency Idioms
Java Concurrency IdiomsJava Concurrency Idioms
Java Concurrency Idioms
 

Similar to Lecture on Java Concurrency Day 4 on Feb 18, 2009.

Ehcache Architecture, Features And Usage Patterns
Ehcache Architecture, Features And Usage PatternsEhcache Architecture, Features And Usage Patterns
Ehcache Architecture, Features And Usage PatternsEduardo Pelegri-Llopart
 
Section 7 fundamentals
Section 7   fundamentalsSection 7   fundamentals
Section 7 fundamentalsJuarez Junior
 
Java programing considering performance
Java programing considering performanceJava programing considering performance
Java programing considering performanceRoger Xia
 
Lecture on Java Concurrency Day 3 on Feb 11, 2009.
Lecture on Java Concurrency Day 3 on Feb 11, 2009.Lecture on Java Concurrency Day 3 on Feb 11, 2009.
Lecture on Java Concurrency Day 3 on Feb 11, 2009.Kyung Koo Yoon
 
Support formation vidéo : OCA Java SE 8 Programmer (1Z0-808) (2)
Support formation vidéo : OCA Java SE 8 Programmer (1Z0-808) (2)Support formation vidéo : OCA Java SE 8 Programmer (1Z0-808) (2)
Support formation vidéo : OCA Java SE 8 Programmer (1Z0-808) (2)SmartnSkilled
 
Mule memory leak issue
Mule memory leak issueMule memory leak issue
Mule memory leak issueJeeHyunLim
 
StormCrawler at Bristech
StormCrawler at BristechStormCrawler at Bristech
StormCrawler at BristechJulien Nioche
 
Efficient Memory and Thread Management in Highly Parallel Java Applications
Efficient Memory and Thread Management in Highly Parallel Java ApplicationsEfficient Memory and Thread Management in Highly Parallel Java Applications
Efficient Memory and Thread Management in Highly Parallel Java ApplicationsPhillip Koza
 
Java Concurrency and Asynchronous
Java Concurrency and AsynchronousJava Concurrency and Asynchronous
Java Concurrency and AsynchronousLifan Yang
 
Optimizing your java applications for multi core hardware
Optimizing your java applications for multi core hardwareOptimizing your java applications for multi core hardware
Optimizing your java applications for multi core hardwareIndicThreads
 
Preview of Groovy 3
Preview of Groovy 3Preview of Groovy 3
Preview of Groovy 3Vijay Shukla
 
Introduction to Testcontainers
Introduction to TestcontainersIntroduction to Testcontainers
Introduction to TestcontainersVMware Tanzu
 

Similar to Lecture on Java Concurrency Day 4 on Feb 18, 2009. (20)

Hackingtomcat
HackingtomcatHackingtomcat
Hackingtomcat
 
Hacking Tomcat
Hacking TomcatHacking Tomcat
Hacking Tomcat
 
Ehcache Architecture, Features And Usage Patterns
Ehcache Architecture, Features And Usage PatternsEhcache Architecture, Features And Usage Patterns
Ehcache Architecture, Features And Usage Patterns
 
Introduction+To+Java+Concurrency
Introduction+To+Java+ConcurrencyIntroduction+To+Java+Concurrency
Introduction+To+Java+Concurrency
 
Section 7 fundamentals
Section 7   fundamentalsSection 7   fundamentals
Section 7 fundamentals
 
Java programing considering performance
Java programing considering performanceJava programing considering performance
Java programing considering performance
 
Lecture on Java Concurrency Day 3 on Feb 11, 2009.
Lecture on Java Concurrency Day 3 on Feb 11, 2009.Lecture on Java Concurrency Day 3 on Feb 11, 2009.
Lecture on Java Concurrency Day 3 on Feb 11, 2009.
 
Support formation vidéo : OCA Java SE 8 Programmer (1Z0-808) (2)
Support formation vidéo : OCA Java SE 8 Programmer (1Z0-808) (2)Support formation vidéo : OCA Java SE 8 Programmer (1Z0-808) (2)
Support formation vidéo : OCA Java SE 8 Programmer (1Z0-808) (2)
 
shashank_micro92_00697015
shashank_micro92_00697015shashank_micro92_00697015
shashank_micro92_00697015
 
Mule memory leak issue
Mule memory leak issueMule memory leak issue
Mule memory leak issue
 
StormCrawler at Bristech
StormCrawler at BristechStormCrawler at Bristech
StormCrawler at Bristech
 
Java 8 고급 (4/6)
Java 8 고급 (4/6)Java 8 고급 (4/6)
Java 8 고급 (4/6)
 
The Java Memory Model
The Java Memory ModelThe Java Memory Model
The Java Memory Model
 
Java tips
Java tipsJava tips
Java tips
 
Efficient Memory and Thread Management in Highly Parallel Java Applications
Efficient Memory and Thread Management in Highly Parallel Java ApplicationsEfficient Memory and Thread Management in Highly Parallel Java Applications
Efficient Memory and Thread Management in Highly Parallel Java Applications
 
Junit_.pptx
Junit_.pptxJunit_.pptx
Junit_.pptx
 
Java Concurrency and Asynchronous
Java Concurrency and AsynchronousJava Concurrency and Asynchronous
Java Concurrency and Asynchronous
 
Optimizing your java applications for multi core hardware
Optimizing your java applications for multi core hardwareOptimizing your java applications for multi core hardware
Optimizing your java applications for multi core hardware
 
Preview of Groovy 3
Preview of Groovy 3Preview of Groovy 3
Preview of Groovy 3
 
Introduction to Testcontainers
Introduction to TestcontainersIntroduction to Testcontainers
Introduction to Testcontainers
 

More from Kyung Koo Yoon

Spring Framework - Inversion of Control Container
Spring Framework - Inversion of Control ContainerSpring Framework - Inversion of Control Container
Spring Framework - Inversion of Control ContainerKyung Koo Yoon
 
Smart software engineer
Smart software engineerSmart software engineer
Smart software engineerKyung Koo Yoon
 
Lecture on Java Concurrency Day 2 on Feb 4, 2009.
Lecture on Java Concurrency Day 2 on Feb 4, 2009.Lecture on Java Concurrency Day 2 on Feb 4, 2009.
Lecture on Java Concurrency Day 2 on Feb 4, 2009.Kyung Koo Yoon
 
Lecture on Java Concurrency Day 1 on Jan 21, 2009.
Lecture on Java Concurrency Day 1 on Jan 21, 2009.Lecture on Java Concurrency Day 1 on Jan 21, 2009.
Lecture on Java Concurrency Day 1 on Jan 21, 2009.Kyung Koo Yoon
 
창의와 열정, 소프트웨어 엔지니어
창의와 열정, 소프트웨어 엔지니어창의와 열정, 소프트웨어 엔지니어
창의와 열정, 소프트웨어 엔지니어Kyung Koo Yoon
 

More from Kyung Koo Yoon (11)

Kubernetes
Kubernetes Kubernetes
Kubernetes
 
Java 8 고급 (6/6)
Java 8 고급 (6/6)Java 8 고급 (6/6)
Java 8 고급 (6/6)
 
Java 8 고급 (5/6)
Java 8 고급 (5/6)Java 8 고급 (5/6)
Java 8 고급 (5/6)
 
Java 8 고급 (3/6)
Java 8 고급 (3/6)Java 8 고급 (3/6)
Java 8 고급 (3/6)
 
Java 8 고급 (2/6)
Java 8 고급 (2/6)Java 8 고급 (2/6)
Java 8 고급 (2/6)
 
Java 8 고급 (1/6)
Java 8 고급 (1/6)Java 8 고급 (1/6)
Java 8 고급 (1/6)
 
Spring Framework - Inversion of Control Container
Spring Framework - Inversion of Control ContainerSpring Framework - Inversion of Control Container
Spring Framework - Inversion of Control Container
 
Smart software engineer
Smart software engineerSmart software engineer
Smart software engineer
 
Lecture on Java Concurrency Day 2 on Feb 4, 2009.
Lecture on Java Concurrency Day 2 on Feb 4, 2009.Lecture on Java Concurrency Day 2 on Feb 4, 2009.
Lecture on Java Concurrency Day 2 on Feb 4, 2009.
 
Lecture on Java Concurrency Day 1 on Jan 21, 2009.
Lecture on Java Concurrency Day 1 on Jan 21, 2009.Lecture on Java Concurrency Day 1 on Jan 21, 2009.
Lecture on Java Concurrency Day 1 on Jan 21, 2009.
 
창의와 열정, 소프트웨어 엔지니어
창의와 열정, 소프트웨어 엔지니어창의와 열정, 소프트웨어 엔지니어
창의와 열정, 소프트웨어 엔지니어
 

Lecture on Java Concurrency Day 4 on Feb 18, 2009.

  • 1. Practical Concurrency January, 2009 http://javadom.blogspot.com/2011/06/lecture-on-java-concurrency-day-4.html
  • 2. Day 4 (9am, Feb 18, 2009) Thread Pool Lock and Condition CAS (Compare And Set)
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.

Editor's Notes

  1. 쓰레드 풀은 서블릿 서버 , EJB 서버 등의 구현에 종종 사용되는 구조이다 . 주인 / 일꾼 모델 (boss/worker model, master/slave model) 에서 많이 사용되는데 이 형태의 쓰레드 모델에서 주인 쓰레드는 쓰레드 풀에서 쉬고 있는 일꾼 쓰레드를 골라 작업을 넘겨준다 . 쓰레드 풀 역시 Executors 클래스의 팩토리 메쏘드를 사용하여 다음과 같이 Executor 처럼 사용할 수 있다 . ExecutorService executor = Executors.newCachedThreadPool(); 지원하는 쓰레드 풀은 재사용되고 필요할 경우 생성하는 방식의 캐시 쓰레드 풀 , 고정 크기 쓰레드 풀 , 쓰레드 실행을 스케줄할 수 있는 스케줄링 쓰레드 풀 등이 있다 . ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue workQueue, ThreadFactory threadFactory, RejectedExecutionHandler handler) FixedThreadPool : unbounded LinkedBlockingQueue SingleThreadExecutor : unbounded LinkedBlockingQueue CachedThreadPool : SynchronousQueue
  2. Queue holds tasks for thread pool a.Unbounded LinkedBlockingQueue -. Tasks are unbounded -. Even if thread pool size get larger than core size, but the task never get saturated since the task queue is unbounded. -. So, the thread pool doesn ’ t(need not) enlarge its pool size above core size (so, max size is not meaningful in this case) b.Bounded BlockingQueues -. Tasks are bounded -. If the thread pool size get larger than core size, and the task get saturated then the thread pool will grow (up to the max pool size) -. Large queue size with small (max) pool size : saves CPU usage, OS resources, context switch overhead but may lead to low thruput -. Small queue size with large (max) pool size : make CPU busier, but can encounter scheduling overhead (which can also lead to low thruput) c. SynchronousQueue -. CachedThreadPool uses this -. Not a real queue -. A mechanism for managing handoff between threads -. When task arrives, if no thread is waiting and the pool size is less than max pool size, then the pool grows. (create a new thread) otherwise, the task is rejected according to the saturation policy. -. SynchronousQueue impl of Java 6 is three times faster than that of Java 5
  3. Lock 인터페이스는 자바의 동기화 블럭 표현 방식인 synchronized 블럭 대신에 명시적인 잠금 획득과 잠금 해제를 사용하는 방식이다 . C/C++ 에서 제공하는 잠금 방식과 유사한 방식으로 코드 차원에서 잠금을 반드시 풀어줘야 한다는 제약이 있지만 블럭 구조가 아닌 방식 , 즉 필요에 따라 잠금 획득과 잠금 해제가 완전히 다른 메쏘드나 블럭에서 처리될 수도 있으며 , 잠금을 획득할 때 tryLock() 을 사용할 수 있다는 장점이 있다 . Condition 인터페이스는 자바의 동기화 이벤트 표현 방식인 wait(), notify() 메쏘드에 대응하는 개념으로 wait(), notify() 메쏘드들이 대응하는 synchronized 블럭 안에서 동작하듯이 Condition 객체는 대응하는 Lock 객체를 통해 동작한다 . Condition 객체를 사용할 때의 장점으로는 하나의 Lock 객체에서 여러 개의 Condition 객체를 사용할 수 있다는 점을 들 수 있다 . class BoundedBuffer { final Lock lock = new ReentrantLock(); final Condition notFull = lock.newCondition(); final Condition notEmpty = lock.newCondition(); final Object[] items = new Object[100]; int putptr, takeptr, count; public void put(Object x) throws InterruptedException { lock.lock(); try { while (count == items.length) notFull.await(); items[putptr] = x; if (++putptr == items.length) putptr = 0; ++count; notEmpty.signal(); } finally { lock.unlock(); } } public Object take() throws InterruptedException { lock.lock(); try { while (count == 0) notEmpty.await(); Object x = items[takeptr]; if (++takeptr == items.length) takeptr = 0; --count; notFull.signal(); return x; } finally { lock.unlock(); } } }
  4. Class AtomicInteger int addAndGet(int delta) Atomically adds the given value to the current value. boolean compareAndSet(int expect, int update) Atomically sets the value to the given updated value if the current value the expected value. int decrementAndGet() Atomically decrements by one the current value. double doubleValue() float floatValue() int get() Gets the current value. int getAndAdd(int delta) Atomically adds the given value to the current value. int getAndDecrement() Atomically decrements by one the current value. int getAndIncrement() Atomically increments by one the current value. int getAndSet(int newValue) Atomically sets to the given value and returns the old value. int incrementAndGet() Atomically increments by one the current value. int intValue() void lazySet(int newValue) Eventually sets to the given value. long longValue() void set(int newValue) Sets to the given value. java.lang.String toString() Returns the String representation of the current value. boolean weakCompareAndSet(int expect, int update) Atomically sets the value to the given updated value if the current value the expected value.
  5. Answer 1 : core size 0, max size INFINITE is the setting of CachedThreadPool which uses SynchronousQueue as its queue implementation which is in reality no queue. if the setting has queue of unbounded, then no queue saturation will occur, so the thread pool will never increase its size (so, the thread pool size will always be zero, and just the queue grows infinitely..) // no thread will be executed ThreadPoolExecutor executor = new ThreadPoolExecutor(0, Integer.MAX_VALUE, 60L, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>()); executor.execute(new Runnable() { public void run() { System.out.println("do INFINITE"); } }); Answer 2 : Executor.execute 가 호출될 때마다 다음에 의해 결정 1. 현재 쓰레드 개수가 core 크기보다 작을 경우 새 쓰레드를 만들어 실행 2. 현재 쓰레드 개수가 core 크기보다 크고 max 크기보다 작을 경우 2.1 queue 가 full 이면 새 쓰레드를 만들어 실행 ( 이때 core size 보다 커짐 ) 2.2 queue 가 full 이 아니면 그냥 queue 에 넣어둠 따라서 fixed thread pool 인 경우 core 크기와 max 크기가 같으므로 , queue full 이 나면 saturation policy 에 의해 결정하면 됨 . 1 번 문항처럼 core size 가 0 이고 queue 크기가 무한대이면 thread pool 크기를 늘릴 일이 없게 됨