SlideShare uma empresa Scribd logo
1 de 27
JDK 7 출시 기념 (2011.7)JDK 7 소개 #4 ConcurrentyFork & Join (jsr166y) 김용환 knight76.tistory.com Knight76 at gmail.com 1
Fork          &         Join 2
Multicore-friendly lightweight parallel framework JSR166y (maintenance) 목적 놀고 있는 여러 개의 processor를 최대한 활용을 위해서 디자인됨 병렬처리 필요 MapReduce와 비슷하지만, 더 세분화할 수 있다. Work-stealing 알고리즘을 기반. 3
Fork/Join Fork Recursively큰 단위의 작업을 작은 단위로 쪼갬 Join 결과를 Recursive하게모음 Doug Lee(뉴욕 주립대 교수) http://gee.cs.oswego.edu/dl/papers/fj.pdf http://g.oswego.edu/dl/concurrency-interest/ 4
Map Reduce와 비교 Map Reduce Node 기반의 Cluster 기반 Single fork Fork / Join 하나의장비안에서 여러 개의 CPU를 사용하려는 하나의 JVM Recursive fork Work Stealing 5
Work Stealing Cilk에서개념을 채용 (논문) MIT에서 만든 멀티쓰레드 프로그래밍 언어 예약어 조금 + C언어(http://supertech.csail.mit.edu/cilk/manual-5.4.6.pdf) 특징 각 worker 는 DeQueue(double-ended queue)를 가지고 있음   worker가 idle일 때, 바쁜 Worker의 Queue에 쌓인 task 를 steal. 왜? 여러 개의 processor(core)에서 동작 가능 로드 밸런스 (작은 task로 나눌 수 있도록 함) 6
Work Stealing 7 ForkJoinPool클래스에구현되어 있음 출처 : http://karlsenchoi.blogspot.com/2011/02/threadpool-work-stealing.html
jsr166y 추가된 API http://gee.cs.oswego.edu/dl/jsr166/dist/jsr166ydocs/ 8
Pseudo code 9 if (my portion of the work is small enough) dothe work directly else split my work into two pieces invokethe two pieces and wait for the results
Java pseudo code  10 class OOOTaskextends RecursiveTask {     @Override     public void compute() { // small enough         if (problem.size< THRESHOLD) {               // do directly problem.solve();                  } else { // split             Task worker1 = new OOOTask(new Problem(problem.size - 1));             Task worker2 = new OOOTask(new Problem(problem.size- 2)); // invoke invokeAll(worker1, worker2);         }     } }
예제 피보나치 계산 Fib(7) 계산 CPU는 4 11
FibonacciProblem 12 class FibonacciProblem { public int n; 	public FibonacciProblem(int n) { this.n = n; 	} 	public long solve() { 		return fibonacci(n); 	} 	private long fibonacci(int n) { System.out.println("Thread: " + Thread.currentThread().getName() 				+ " calculates " + n); 		if (n <= 1) { 			return n; } else { return fibonacci(n - 1) + fibonacci(n - 2);                          } 	} }
FibonacciTask 13 @SuppressWarnings("serial") class FibonacciTask extends RecursiveTask<Long> { 	private static final intTHRESHOLD = 5; 	private FibonacciProblem problem; 	public long result; 	public FibonacciTask(FibonacciProblem problem) { this.problem = problem; 	} 	@Override 	public Long compute() { 		if (problem.n < THRESHOLD) { 			result = problem.solve(); 		} else { FibonacciTask worker1 = new FibonacciTask(                                                           new FibonacciProblem(problem.n - 1)); FibonacciTask worker2 = new FibonacciTask(                                                           new FibonacciProblem(problem.n - 2)); worker1.fork(); 			result = worker2.compute() + worker1.join(); 		} 		return result; 	} }
Main 14 public static void main(String[] args) throws Exception { System.out.println("Number of processors: " +  Runtime.getRuntime().availableProcessors()); intn = 7; StopWatchstopWatch = new StopWatch();    FibonacciProblembigProblem = new FibonacciProblem(n); FibonacciTasktask = new FibonacciTask(bigProblem); ForkJoinPool pool = new ForkJoinPool(); pool.invoke(task); long result = task.result; System.out.println("Computed Result: " + result); stopWatch.stop(); System.out.println("Elapsed Time: " + stopWatch.getTotalTimeMillis()); System.out.println("Steal Count : " + pool.getStealCount()); }
실행 결과 15 No of processors: 4 Thread: ForkJoinPool-1-worker-1 calculates 3 Thread: ForkJoinPool-1-worker-2 calculates 4 Thread: ForkJoinPool-1-worker-3 calculates 4 Thread: ForkJoinPool-1-worker-3 calculates 3 Thread: ForkJoinPool-1-worker-3 calculates 2 Thread: ForkJoinPool-1-worker-3 calculates 1 Thread: ForkJoinPool-1-worker-4 calculates 3 Thread: ForkJoinPool-1-worker-4 calculates 2 Thread: ForkJoinPool-1-worker-4 calculates 1 Thread: ForkJoinPool-1-worker-2 calculates 3 Thread: ForkJoinPool-1-worker-2 calculates 2 Thread: ForkJoinPool-1-worker-1 calculates 2 Thread: ForkJoinPool-1-worker-1 calculates 1 Thread: ForkJoinPool-1-worker-2 calculates 1 Thread: ForkJoinPool-1-worker-4 calculates 0 Thread: ForkJoinPool-1-worker-3 calculates 0 Thread: ForkJoinPool-1-worker-3 calculates 1 Thread: ForkJoinPool-1-worker-4 calculates 1 Thread: ForkJoinPool-1-worker-2 calculates 0 Thread: ForkJoinPool-1-worker-1 calculates 0 Thread: ForkJoinPool-1-worker-2 calculates 1 Thread: ForkJoinPool-1-worker-3 calculates 2 Thread: ForkJoinPool-1-worker-2 calculates 2 Thread: ForkJoinPool-1-worker-1 calculates 1 Thread: ForkJoinPool-1-worker-2 calculates 1 Thread: ForkJoinPool-1-worker-3 calculates 1 Thread: ForkJoinPool-1-worker-3 calculates 0 Thread: ForkJoinPool-1-worker-2 calculates 0 Thread: ForkJoinPool-1-worker-4 calculates 4 Thread: ForkJoinPool-1-worker-4 calculates 3 Thread: ForkJoinPool-1-worker-4 calculates 2 Thread: ForkJoinPool-1-worker-4 calculates 1 Thread: ForkJoinPool-1-worker-4 calculates 0 Thread: ForkJoinPool-1-worker-4 calculates 1 Thread: ForkJoinPool-1-worker-4 calculates 2 Thread: ForkJoinPool-1-worker-4 calculates 1 Thread: ForkJoinPool-1-worker-4 calculates 0 Computed Result: 13 Elapsed Time: 4 Steal Count : 4
RecursiveTask & RecursiveAction 16 Future ForkJoinTask RecursiveTask RecursiveAction
API (RecursiveAction) 17 public abstract class RecursiveAction<V> extends ForkJoinTask<V> {     private static final long serialVersionUID = 5232453952276485270L;     protected abstract V compute();     public final void getRawResult() {         return null;     }     protected final void setRawResult(V value) {     }     protected final boolean exec() {         compute();         return true;     } }
API (RecursiveTask) 18 public abstract class RecursiveTaskextends ForkJoinTask<Void> {     private static final long serialVersionUID = 5232453952276485270L;     V result;     protected abstract V compute();     public final V getRawResult() {         return result;     }     protected final void setRawResult(V value) {         result = value;     }     protected final boolean exec() {         result = compute();         return true;     } }
API (ForkJoinTask) 19
API (ForkJoinPool) 20 Executor ExecutorService AbstractExecutorService ForkJoinPool
ForkJoinPool 21
기타 22
ThreadLocalRandom 멀티쓰레드상에서Random 은 contention과 overhead가 존재. Random 내부에서 thread를 isolation을 함 ForkJoinPool에서 Random 사용시 이슈가 속도가 저하되기 때문에 이를 해결할 수 있는 클래스 23 long f = ThreadLocalRandom.current().nextLong(); System.out.println("1 : " + f); f = ThreadLocalRandom.current().nextLong(); System.out.println("2 : " + f); 1 : 4232237 2 : 767956431209526212
Phaser 특징 쓰레드를 동시 시작/종료시킬 수 있도록 함 CyclicBarrier와 CountDownLatch클래스를 보다 유연함 좋은 자료 http://download.oracle.com/javase/7/docs/api/java/util/concurrent/Phaser.html http://olegignatenko.livejournal.com/16771.html 24
Phaser예제 25 public static void main(String[] args) { runDevelopment(Arrays.asList(new Developer(), new Developer(),                              new Developer(), new Developer())); } private static void runDevelopment(Iterable<Developer> team) {         final Phasermanager = new Phaser(1); //"1" to register self System.out.println("mgr: assign all developers, then start coding");         for (final Developer developer : team) {             final String dev = developer.toString(); System.out.println("mgr: assigns a new unarrived " + dev + " to the project"); manager.register();             new Thread() {                 public void run() { System.out.println("mgr: " + dev + ", please await all developers"); manager.arriveAndAwaitAdvance(); // await all creation System.out.println("mgr: " + dev + ", OK to start coding"); developer.run(); }             }.start(); } System.out.println("mgr: all assigned, start coding after all arrive"); manager.arriveAndDeregister(); } class Developer implements Runnable {     private final static AtomicIntegeridSource = new AtomicInteger();     private final int id = idSource.incrementAndGet();     public void run() { System.out.println(toString() + ": coding"); }     public String toString() { return "developer #" + id; } }
Phaser결과 26 mgr: assign all developers, then start coding mgr: assigns a new unarrived developer #1 to the project mgr: assigns a new unarrived developer #2 to the project mgr: developer #1, please await all developers mgr: assigns a new unarrived developer #3 to the project mgr: assigns a new unarrived developer #4 to the project mgr: developer #3, please await all developers mgr: all assigned, start coding after all arrive mgr: developer #4, please await all developers mgr: developer #2, please await all developers mgr: developer #2, OK to start coding developer #2: coding mgr: developer #1, OK to start coding developer #1: coding mgr: developer #3, OK to start coding developer #3: coding mgr: developer #4, OK to start coding developer #4: coding
To be continued #5 27

Mais conteúdo relacionado

Mais procurados

Java 7 Launch Event at LyonJUG, Lyon France. Fork / Join framework and Projec...
Java 7 Launch Event at LyonJUG, Lyon France. Fork / Join framework and Projec...Java 7 Launch Event at LyonJUG, Lyon France. Fork / Join framework and Projec...
Java 7 Launch Event at LyonJUG, Lyon France. Fork / Join framework and Projec...
julien.ponge
 
Java Bytecode for Discriminating Developers - JavaZone 2011
Java Bytecode for Discriminating Developers - JavaZone 2011Java Bytecode for Discriminating Developers - JavaZone 2011
Java Bytecode for Discriminating Developers - JavaZone 2011
Anton Arhipov
 
Java Bytecode For Discriminating Developers - GeeCON 2011
Java Bytecode For Discriminating Developers - GeeCON 2011Java Bytecode For Discriminating Developers - GeeCON 2011
Java Bytecode For Discriminating Developers - GeeCON 2011
Anton Arhipov
 

Mais procurados (20)

Java 10, Java 11 and beyond
Java 10, Java 11 and beyondJava 10, Java 11 and beyond
Java 10, Java 11 and beyond
 
Riga Dev Day 2016 - Having fun with Javassist
Riga Dev Day 2016 - Having fun with JavassistRiga Dev Day 2016 - Having fun with Javassist
Riga Dev Day 2016 - Having fun with Javassist
 
GeeCON 2017 - TestContainers. Integration testing without the hassle
GeeCON 2017 - TestContainers. Integration testing without the hassleGeeCON 2017 - TestContainers. Integration testing without the hassle
GeeCON 2017 - TestContainers. Integration testing without the hassle
 
Java 7 LavaJUG
Java 7 LavaJUGJava 7 LavaJUG
Java 7 LavaJUG
 
Java 7 Launch Event at LyonJUG, Lyon France. Fork / Join framework and Projec...
Java 7 Launch Event at LyonJUG, Lyon France. Fork / Join framework and Projec...Java 7 Launch Event at LyonJUG, Lyon France. Fork / Join framework and Projec...
Java 7 Launch Event at LyonJUG, Lyon France. Fork / Join framework and Projec...
 
JVM
JVMJVM
JVM
 
Java Concurrency by Example
Java Concurrency by ExampleJava Concurrency by Example
Java Concurrency by Example
 
Java Bytecode for Discriminating Developers - JavaZone 2011
Java Bytecode for Discriminating Developers - JavaZone 2011Java Bytecode for Discriminating Developers - JavaZone 2011
Java Bytecode for Discriminating Developers - JavaZone 2011
 
A topology of memory leaks on the JVM
A topology of memory leaks on the JVMA topology of memory leaks on the JVM
A topology of memory leaks on the JVM
 
201913046 wahyu septiansyah network programing
201913046 wahyu septiansyah network programing201913046 wahyu septiansyah network programing
201913046 wahyu septiansyah network programing
 
Oredev 2015 - Taming Java Agents
Oredev 2015 - Taming Java AgentsOredev 2015 - Taming Java Agents
Oredev 2015 - Taming Java Agents
 
Server1
Server1Server1
Server1
 
Jenkins 2を使った究極のpipeline ~ 明日もう一度来てください、本物のpipelineをお見せしますよ ~
Jenkins 2を使った究極のpipeline ~ 明日もう一度来てください、本物のpipelineをお見せしますよ ~Jenkins 2を使った究極のpipeline ~ 明日もう一度来てください、本物のpipelineをお見せしますよ ~
Jenkins 2を使った究極のpipeline ~ 明日もう一度来てください、本物のpipelineをお見せしますよ ~
 
15 tips to improve your unit tests (Droidcon Berlin 2016 Barcamp)
15 tips to improve your unit tests (Droidcon Berlin 2016 Barcamp)15 tips to improve your unit tests (Droidcon Berlin 2016 Barcamp)
15 tips to improve your unit tests (Droidcon Berlin 2016 Barcamp)
 
Down to Stack Traces, up from Heap Dumps
Down to Stack Traces, up from Heap DumpsDown to Stack Traces, up from Heap Dumps
Down to Stack Traces, up from Heap Dumps
 
모던자바의 역습
모던자바의 역습모던자바의 역습
모던자바의 역습
 
Java Bytecode For Discriminating Developers - GeeCON 2011
Java Bytecode For Discriminating Developers - GeeCON 2011Java Bytecode For Discriminating Developers - GeeCON 2011
Java Bytecode For Discriminating Developers - GeeCON 2011
 
Monitoring distributed (micro-)services
Monitoring distributed (micro-)servicesMonitoring distributed (micro-)services
Monitoring distributed (micro-)services
 
JEEConf 2017 - The hitchhiker’s guide to Java class reloading
JEEConf 2017 - The hitchhiker’s guide to Java class reloadingJEEConf 2017 - The hitchhiker’s guide to Java class reloading
JEEConf 2017 - The hitchhiker’s guide to Java class reloading
 
Con-FESS 2015 - Having Fun With Javassist
Con-FESS 2015 - Having Fun With JavassistCon-FESS 2015 - Having Fun With Javassist
Con-FESS 2015 - Having Fun With Javassist
 

Destaque (14)

Java 8 Stream API. A different way to process collections.
Java 8 Stream API. A different way to process collections.Java 8 Stream API. A different way to process collections.
Java 8 Stream API. A different way to process collections.
 
JavaOne 2016 - Learn Lambda and functional programming
JavaOne 2016 - Learn Lambda and functional programmingJavaOne 2016 - Learn Lambda and functional programming
JavaOne 2016 - Learn Lambda and functional programming
 
구글크롬Os
구글크롬Os구글크롬Os
구글크롬Os
 
하이브리드앱
하이브리드앱하이브리드앱
하이브리드앱
 
Spring MVC 3 Restful
Spring MVC 3 RestfulSpring MVC 3 Restful
Spring MVC 3 Restful
 
Comet
CometComet
Comet
 
Apache avro
Apache avroApache avro
Apache avro
 
2014년 3월호_미니 이그나이트 한컴 - 망하는 세가지 방법
2014년 3월호_미니 이그나이트 한컴 - 망하는 세가지 방법2014년 3월호_미니 이그나이트 한컴 - 망하는 세가지 방법
2014년 3월호_미니 이그나이트 한컴 - 망하는 세가지 방법
 
Jdk(java) 7 - 5. invoke-dynamic
Jdk(java) 7 - 5. invoke-dynamicJdk(java) 7 - 5. invoke-dynamic
Jdk(java) 7 - 5. invoke-dynamic
 
Jdk 7 3-nio2
Jdk 7 3-nio2Jdk 7 3-nio2
Jdk 7 3-nio2
 
Apache Thrift
Apache ThriftApache Thrift
Apache Thrift
 
Google Protocol buffer
Google Protocol bufferGoogle Protocol buffer
Google Protocol buffer
 
데이터 바인딩 ( Binding )
데이터 바인딩 ( Binding )데이터 바인딩 ( Binding )
데이터 바인딩 ( Binding )
 
Redis
RedisRedis
Redis
 

Semelhante a Jdk 7 4-forkjoin

Whats new in_csharp4
Whats new in_csharp4Whats new in_csharp4
Whats new in_csharp4
Abed Bukhari
 
13multithreaded Programming
13multithreaded Programming13multithreaded Programming
13multithreaded Programming
Adil Jafri
 

Semelhante a Jdk 7 4-forkjoin (20)

JVM Mechanics: When Does the JVM JIT & Deoptimize?
JVM Mechanics: When Does the JVM JIT & Deoptimize?JVM Mechanics: When Does the JVM JIT & Deoptimize?
JVM Mechanics: When Does the JVM JIT & Deoptimize?
 
Parallel Programming With Dot Net
Parallel Programming With Dot NetParallel Programming With Dot Net
Parallel Programming With Dot Net
 
Silicon Valley JUG: JVM Mechanics
Silicon Valley JUG: JVM MechanicsSilicon Valley JUG: JVM Mechanics
Silicon Valley JUG: JVM Mechanics
 
Java Concurrency
Java ConcurrencyJava Concurrency
Java Concurrency
 
RxJava on Android
RxJava on AndroidRxJava on Android
RxJava on Android
 
Global Interpreter Lock: Episode I - Break the Seal
Global Interpreter Lock: Episode I - Break the SealGlobal Interpreter Lock: Episode I - Break the Seal
Global Interpreter Lock: Episode I - Break the Seal
 
Java util concurrent
Java util concurrentJava util concurrent
Java util concurrent
 
#JavaFX.forReal() - ElsassJUG
#JavaFX.forReal() - ElsassJUG#JavaFX.forReal() - ElsassJUG
#JavaFX.forReal() - ElsassJUG
 
Construire une application JavaFX 8 avec gradle
Construire une application JavaFX 8 avec gradleConstruire une application JavaFX 8 avec gradle
Construire une application JavaFX 8 avec gradle
 
Whats new in_csharp4
Whats new in_csharp4Whats new in_csharp4
Whats new in_csharp4
 
Fork and join framework
Fork and join frameworkFork and join framework
Fork and join framework
 
Multithreading in Java
Multithreading in JavaMultithreading in Java
Multithreading in Java
 
Os lab final
Os lab finalOs lab final
Os lab final
 
Microkernel Development
Microkernel DevelopmentMicrokernel Development
Microkernel Development
 
Counter Wars (JEEConf 2016)
Counter Wars (JEEConf 2016)Counter Wars (JEEConf 2016)
Counter Wars (JEEConf 2016)
 
Rx workshop
Rx workshopRx workshop
Rx workshop
 
00_Introduction to Java.ppt
00_Introduction to Java.ppt00_Introduction to Java.ppt
00_Introduction to Java.ppt
 
13multithreaded Programming
13multithreaded Programming13multithreaded Programming
13multithreaded Programming
 
Java Fundamentals
Java FundamentalsJava Fundamentals
Java Fundamentals
 
Tools and Techniques for Understanding Threading Behavior in Android
Tools and Techniques for Understanding Threading Behavior in AndroidTools and Techniques for Understanding Threading Behavior in Android
Tools and Techniques for Understanding Threading Behavior in Android
 

Mais de knight1128 (7)

Hancom MDS Conference - KAKAO DEVOPS Practice (카카오 스토리의 Devops 사례)
Hancom MDS Conference - KAKAO DEVOPS Practice (카카오 스토리의 Devops 사례)Hancom MDS Conference - KAKAO DEVOPS Practice (카카오 스토리의 Devops 사례)
Hancom MDS Conference - KAKAO DEVOPS Practice (카카오 스토리의 Devops 사례)
 
공유 Jdk 7-2-project coin
공유 Jdk 7-2-project coin공유 Jdk 7-2-project coin
공유 Jdk 7-2-project coin
 
공유 Jdk 7-1-short introduction
공유 Jdk 7-1-short introduction공유 Jdk 7-1-short introduction
공유 Jdk 7-1-short introduction
 
아마존 Aws 서비스_연구
아마존 Aws 서비스_연구아마존 Aws 서비스_연구
아마존 Aws 서비스_연구
 
속도체크
속도체크속도체크
속도체크
 
오픈소스를 활용한 Batch_처리_플랫폼_공유
오픈소스를 활용한 Batch_처리_플랫폼_공유오픈소스를 활용한 Batch_처리_플랫폼_공유
오픈소스를 활용한 Batch_처리_플랫폼_공유
 
Ssl 하드웨어 가속기를 이용한 성능 향상
Ssl 하드웨어 가속기를 이용한 성능 향상Ssl 하드웨어 가속기를 이용한 성능 향상
Ssl 하드웨어 가속기를 이용한 성능 향상
 

Último

Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Victor Rentea
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
WSO2
 

Último (20)

Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
 
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Six Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal OntologySix Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal Ontology
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 

Jdk 7 4-forkjoin

  • 1. JDK 7 출시 기념 (2011.7)JDK 7 소개 #4 ConcurrentyFork & Join (jsr166y) 김용환 knight76.tistory.com Knight76 at gmail.com 1
  • 2. Fork & Join 2
  • 3. Multicore-friendly lightweight parallel framework JSR166y (maintenance) 목적 놀고 있는 여러 개의 processor를 최대한 활용을 위해서 디자인됨 병렬처리 필요 MapReduce와 비슷하지만, 더 세분화할 수 있다. Work-stealing 알고리즘을 기반. 3
  • 4. Fork/Join Fork Recursively큰 단위의 작업을 작은 단위로 쪼갬 Join 결과를 Recursive하게모음 Doug Lee(뉴욕 주립대 교수) http://gee.cs.oswego.edu/dl/papers/fj.pdf http://g.oswego.edu/dl/concurrency-interest/ 4
  • 5. Map Reduce와 비교 Map Reduce Node 기반의 Cluster 기반 Single fork Fork / Join 하나의장비안에서 여러 개의 CPU를 사용하려는 하나의 JVM Recursive fork Work Stealing 5
  • 6. Work Stealing Cilk에서개념을 채용 (논문) MIT에서 만든 멀티쓰레드 프로그래밍 언어 예약어 조금 + C언어(http://supertech.csail.mit.edu/cilk/manual-5.4.6.pdf) 특징 각 worker 는 DeQueue(double-ended queue)를 가지고 있음 worker가 idle일 때, 바쁜 Worker의 Queue에 쌓인 task 를 steal. 왜? 여러 개의 processor(core)에서 동작 가능 로드 밸런스 (작은 task로 나눌 수 있도록 함) 6
  • 7. Work Stealing 7 ForkJoinPool클래스에구현되어 있음 출처 : http://karlsenchoi.blogspot.com/2011/02/threadpool-work-stealing.html
  • 8. jsr166y 추가된 API http://gee.cs.oswego.edu/dl/jsr166/dist/jsr166ydocs/ 8
  • 9. Pseudo code 9 if (my portion of the work is small enough) dothe work directly else split my work into two pieces invokethe two pieces and wait for the results
  • 10. Java pseudo code 10 class OOOTaskextends RecursiveTask { @Override public void compute() { // small enough if (problem.size< THRESHOLD) { // do directly problem.solve(); } else { // split Task worker1 = new OOOTask(new Problem(problem.size - 1)); Task worker2 = new OOOTask(new Problem(problem.size- 2)); // invoke invokeAll(worker1, worker2); } } }
  • 11. 예제 피보나치 계산 Fib(7) 계산 CPU는 4 11
  • 12. FibonacciProblem 12 class FibonacciProblem { public int n; public FibonacciProblem(int n) { this.n = n; } public long solve() { return fibonacci(n); } private long fibonacci(int n) { System.out.println("Thread: " + Thread.currentThread().getName() + " calculates " + n); if (n <= 1) { return n; } else { return fibonacci(n - 1) + fibonacci(n - 2); } } }
  • 13. FibonacciTask 13 @SuppressWarnings("serial") class FibonacciTask extends RecursiveTask<Long> { private static final intTHRESHOLD = 5; private FibonacciProblem problem; public long result; public FibonacciTask(FibonacciProblem problem) { this.problem = problem; } @Override public Long compute() { if (problem.n < THRESHOLD) { result = problem.solve(); } else { FibonacciTask worker1 = new FibonacciTask( new FibonacciProblem(problem.n - 1)); FibonacciTask worker2 = new FibonacciTask( new FibonacciProblem(problem.n - 2)); worker1.fork(); result = worker2.compute() + worker1.join(); } return result; } }
  • 14. Main 14 public static void main(String[] args) throws Exception { System.out.println("Number of processors: " + Runtime.getRuntime().availableProcessors()); intn = 7; StopWatchstopWatch = new StopWatch(); FibonacciProblembigProblem = new FibonacciProblem(n); FibonacciTasktask = new FibonacciTask(bigProblem); ForkJoinPool pool = new ForkJoinPool(); pool.invoke(task); long result = task.result; System.out.println("Computed Result: " + result); stopWatch.stop(); System.out.println("Elapsed Time: " + stopWatch.getTotalTimeMillis()); System.out.println("Steal Count : " + pool.getStealCount()); }
  • 15. 실행 결과 15 No of processors: 4 Thread: ForkJoinPool-1-worker-1 calculates 3 Thread: ForkJoinPool-1-worker-2 calculates 4 Thread: ForkJoinPool-1-worker-3 calculates 4 Thread: ForkJoinPool-1-worker-3 calculates 3 Thread: ForkJoinPool-1-worker-3 calculates 2 Thread: ForkJoinPool-1-worker-3 calculates 1 Thread: ForkJoinPool-1-worker-4 calculates 3 Thread: ForkJoinPool-1-worker-4 calculates 2 Thread: ForkJoinPool-1-worker-4 calculates 1 Thread: ForkJoinPool-1-worker-2 calculates 3 Thread: ForkJoinPool-1-worker-2 calculates 2 Thread: ForkJoinPool-1-worker-1 calculates 2 Thread: ForkJoinPool-1-worker-1 calculates 1 Thread: ForkJoinPool-1-worker-2 calculates 1 Thread: ForkJoinPool-1-worker-4 calculates 0 Thread: ForkJoinPool-1-worker-3 calculates 0 Thread: ForkJoinPool-1-worker-3 calculates 1 Thread: ForkJoinPool-1-worker-4 calculates 1 Thread: ForkJoinPool-1-worker-2 calculates 0 Thread: ForkJoinPool-1-worker-1 calculates 0 Thread: ForkJoinPool-1-worker-2 calculates 1 Thread: ForkJoinPool-1-worker-3 calculates 2 Thread: ForkJoinPool-1-worker-2 calculates 2 Thread: ForkJoinPool-1-worker-1 calculates 1 Thread: ForkJoinPool-1-worker-2 calculates 1 Thread: ForkJoinPool-1-worker-3 calculates 1 Thread: ForkJoinPool-1-worker-3 calculates 0 Thread: ForkJoinPool-1-worker-2 calculates 0 Thread: ForkJoinPool-1-worker-4 calculates 4 Thread: ForkJoinPool-1-worker-4 calculates 3 Thread: ForkJoinPool-1-worker-4 calculates 2 Thread: ForkJoinPool-1-worker-4 calculates 1 Thread: ForkJoinPool-1-worker-4 calculates 0 Thread: ForkJoinPool-1-worker-4 calculates 1 Thread: ForkJoinPool-1-worker-4 calculates 2 Thread: ForkJoinPool-1-worker-4 calculates 1 Thread: ForkJoinPool-1-worker-4 calculates 0 Computed Result: 13 Elapsed Time: 4 Steal Count : 4
  • 16. RecursiveTask & RecursiveAction 16 Future ForkJoinTask RecursiveTask RecursiveAction
  • 17. API (RecursiveAction) 17 public abstract class RecursiveAction<V> extends ForkJoinTask<V> { private static final long serialVersionUID = 5232453952276485270L; protected abstract V compute(); public final void getRawResult() { return null; } protected final void setRawResult(V value) { } protected final boolean exec() { compute(); return true; } }
  • 18. API (RecursiveTask) 18 public abstract class RecursiveTaskextends ForkJoinTask<Void> { private static final long serialVersionUID = 5232453952276485270L; V result; protected abstract V compute(); public final V getRawResult() { return result; } protected final void setRawResult(V value) { result = value; } protected final boolean exec() { result = compute(); return true; } }
  • 20. API (ForkJoinPool) 20 Executor ExecutorService AbstractExecutorService ForkJoinPool
  • 23. ThreadLocalRandom 멀티쓰레드상에서Random 은 contention과 overhead가 존재. Random 내부에서 thread를 isolation을 함 ForkJoinPool에서 Random 사용시 이슈가 속도가 저하되기 때문에 이를 해결할 수 있는 클래스 23 long f = ThreadLocalRandom.current().nextLong(); System.out.println("1 : " + f); f = ThreadLocalRandom.current().nextLong(); System.out.println("2 : " + f); 1 : 4232237 2 : 767956431209526212
  • 24. Phaser 특징 쓰레드를 동시 시작/종료시킬 수 있도록 함 CyclicBarrier와 CountDownLatch클래스를 보다 유연함 좋은 자료 http://download.oracle.com/javase/7/docs/api/java/util/concurrent/Phaser.html http://olegignatenko.livejournal.com/16771.html 24
  • 25. Phaser예제 25 public static void main(String[] args) { runDevelopment(Arrays.asList(new Developer(), new Developer(), new Developer(), new Developer())); } private static void runDevelopment(Iterable<Developer> team) { final Phasermanager = new Phaser(1); //"1" to register self System.out.println("mgr: assign all developers, then start coding"); for (final Developer developer : team) { final String dev = developer.toString(); System.out.println("mgr: assigns a new unarrived " + dev + " to the project"); manager.register(); new Thread() { public void run() { System.out.println("mgr: " + dev + ", please await all developers"); manager.arriveAndAwaitAdvance(); // await all creation System.out.println("mgr: " + dev + ", OK to start coding"); developer.run(); } }.start(); } System.out.println("mgr: all assigned, start coding after all arrive"); manager.arriveAndDeregister(); } class Developer implements Runnable { private final static AtomicIntegeridSource = new AtomicInteger(); private final int id = idSource.incrementAndGet(); public void run() { System.out.println(toString() + ": coding"); } public String toString() { return "developer #" + id; } }
  • 26. Phaser결과 26 mgr: assign all developers, then start coding mgr: assigns a new unarrived developer #1 to the project mgr: assigns a new unarrived developer #2 to the project mgr: developer #1, please await all developers mgr: assigns a new unarrived developer #3 to the project mgr: assigns a new unarrived developer #4 to the project mgr: developer #3, please await all developers mgr: all assigned, start coding after all arrive mgr: developer #4, please await all developers mgr: developer #2, please await all developers mgr: developer #2, OK to start coding developer #2: coding mgr: developer #1, OK to start coding developer #1: coding mgr: developer #3, OK to start coding developer #3: coding mgr: developer #4, OK to start coding developer #4: coding