SlideShare a Scribd company logo
Effective Java
Item 80: Prefer executors, tasks, and streams to threads
Item 81: Prefer concurrency utilities to wait and notify
Item 80
Prefer executors, tasks, and streams to threads
Executor Framework - Basic Example
● Submit a runnable for execution
ExecutorService exec = Executors.newSingleThreadExecutor();
exec.execute(runnable);
exec.shutdown();
Executor Framework - invokeAny - Wait until first done
Executor Framework - invokeAny - Wait until ALL done
Executor Framework - invokeAny - Wait until ALL done
● Hard to get future result by complete sequence
Executor Framework - ExecutorCompletionService
● Get future result by sequence
Executor Framework - Cancel rest jobs
Executor Framework - Schedule task
Executor Framework - Choose executor
● Executors.newCachedThreadPool
○ Easy, no configuration
○ Do the right thing
○ When no available thread, new one will be added to pool
○ No task will be queued
○ Choose for small program
■ Why not heavy load program?
Executor Framework - Choose executor
● Executors.newCachedThreadPool
○ Easy, no configuration
○ Do the right thing
○ When no available thread, new one will be added to pool
○ No task will be queued
○ Choose for small program
■ Why not heavy load program?
● all of CPUs are fully utilized
● more tasks arrive, more threads will be created
● less CPU available, cause matters worse
Executor Framework - Choose executor
● Executors.newFixedThreadPool
○ Gives a pool with fixed number of threads
○ For heavy load program
○ Still need monitor queue size
Runnable & Callable
● Runnable: no return value
● Callable: return value
ForkJoinPool
● Normal Executor Thread Management
○ Main thread put task to queue
○ Sub tasks put tasks to queue
○ Every task in a queue without boundry
Main
Time
ForkJoinPool
● ForkJoinPool Thread Management
○ Main thread create tasks
○ Tasks create sub tasks to fork
○ tasks wait for sub tasks to join
○ Main thread get result or wait for done
Time
task1
Main
task1-1
task1-2
task2
task2-1
task2-2
Time
ForkJoinPool Example
Item 81
Prefer concurrency utilities to wait and notify
It’s difficult
“Given the difficulity of using wait and notify correctly, you should use the
higher-level concurrency utilities instead”
Related Topics
● Executor Framework - Item 80
● Concurrent collections - Item 81
● Synchronizers - Item 81
Concurrent collections
● They are high-performance concurrent implementation of standard collection
interfaces.
● Each implementations need manage their own synchronization internally.
● It is impossible to exclude concurrent activity from a concurrent collection
● locking it will only slow the program
Compose method invocations
● You can’t exclude concurrent activity on concurrent collections (will become
slow), you can’t automatically compose method invocations on them either.
● So concurrent collection interfaces were outfitted with state-dependent modify
operations, combine several primitives into a single atomic operation.
Ex. Map.putIfAbsent(key, value)
String#intern
String#intern will find string from cache before creating new instance.
String s1 = “test”;
String s2 = new String(“test”);
String s3 = new String(“test”).intern();
System.out.println(s1 == s2); // false
System.out.println(s1 == s3); // true
String#intern implementation by putIfAbsent
private static final ConcurrentMap<String,String> map =
new ConcurrentHashMap<>();
public static String intern(String s) {
String pre = map.putIfAbsent(s, s);
return pre == null ? s : pre;
}
String#intern implemented by ConcurrentHashMap
ConcurrentHashMap is optimized for retrieval operations, such as get.
public static String intern(String s) {
String result = map.get(s);
if (result == null) {
result = map.putIfAbsent(s, s);
if (result == null) {
result = s;
}
}
return result;
}
ConcurrentHashMap is high performance
Replace Collections.synchronizedMap by new ConcurrentHashMap can
dramatically increase performance.
BlockingQueue
● take method will wait until queue is not empty
Synchronizer
● Enable threads to wait for one another
● Ex. CountDownLatch: Wait until count down to zero
● Ex. Semaphore: Wait until token available
● Ex. CylicBarrier: Wait until all are waiting
● Ex. Exchanger: Wait until another thread is ready
● Ex. Phaser: Wait until other arrived
Legacy
● Always use the wait loop idiom to invoke the wait method, never invoke it
outside of a loop
synchronized (obj) {
while (<condition>) {
obj.wait();
}
}
Ad

More Related Content

What's hot (20)

Java spring batch
Java spring batchJava spring batch
Java spring batch
furuCRM株式会社 CEO/Dreamforce Vietnam Founder
 
Building Scalable Stateless Applications with RxJava
Building Scalable Stateless Applications with RxJavaBuilding Scalable Stateless Applications with RxJava
Building Scalable Stateless Applications with RxJava
Rick Warren
 
Infrastructure & System Monitoring using Prometheus
Infrastructure & System Monitoring using PrometheusInfrastructure & System Monitoring using Prometheus
Infrastructure & System Monitoring using Prometheus
Marco Pas
 
CS106 Lab 10 - Functions (passing by value)
CS106 Lab 10 - Functions (passing by value)CS106 Lab 10 - Functions (passing by value)
CS106 Lab 10 - Functions (passing by value)
Nada Kamel
 
Reactive by example - at Reversim Summit 2015
Reactive by example - at Reversim Summit 2015Reactive by example - at Reversim Summit 2015
Reactive by example - at Reversim Summit 2015
Eran Harel
 
Streams, Streams Everywhere! An Introduction to Rx
Streams, Streams Everywhere! An Introduction to RxStreams, Streams Everywhere! An Introduction to Rx
Streams, Streams Everywhere! An Introduction to Rx
Andrzej Sitek
 
Spring batch showCase
Spring batch showCaseSpring batch showCase
Spring batch showCase
taher abdo
 
CS106 Lab 7 - For loop
CS106 Lab 7 - For loopCS106 Lab 7 - For loop
CS106 Lab 7 - For loop
Nada Kamel
 
09 advanced c#
09 advanced c#09 advanced c#
09 advanced c#
eleksdev
 
RxJava@Android
RxJava@AndroidRxJava@Android
RxJava@Android
Maxim Volgin
 
Flink Forward Berlin 2017: Matt Zimmer - Custom, Complex Windows at Scale Usi...
Flink Forward Berlin 2017: Matt Zimmer - Custom, Complex Windows at Scale Usi...Flink Forward Berlin 2017: Matt Zimmer - Custom, Complex Windows at Scale Usi...
Flink Forward Berlin 2017: Matt Zimmer - Custom, Complex Windows at Scale Usi...
Flink Forward
 
Top 10 RxJs Operators in Angular
Top 10 RxJs Operators in Angular Top 10 RxJs Operators in Angular
Top 10 RxJs Operators in Angular
Jalpesh Vadgama
 
Flink Forward SF 2017: Feng Wang & Zhijiang Wang - Runtime Improvements in Bl...
Flink Forward SF 2017: Feng Wang & Zhijiang Wang - Runtime Improvements in Bl...Flink Forward SF 2017: Feng Wang & Zhijiang Wang - Runtime Improvements in Bl...
Flink Forward SF 2017: Feng Wang & Zhijiang Wang - Runtime Improvements in Bl...
Flink Forward
 
Load test REST APIs using gatling
Load test REST APIs using gatlingLoad test REST APIs using gatling
Load test REST APIs using gatling
Jayaram Sankaranarayanan
 
CS106 Lab 6 - While and Do..While loop
CS106 Lab 6 - While and Do..While loopCS106 Lab 6 - While and Do..While loop
CS106 Lab 6 - While and Do..While loop
Nada Kamel
 
Reactive
ReactiveReactive
Reactive
Pranav E K
 
Study & Analysis of Complexities of Stack & Queue Operations in Data Structure
Study & Analysis of Complexities of Stack & Queue Operations in Data StructureStudy & Analysis of Complexities of Stack & Queue Operations in Data Structure
Study & Analysis of Complexities of Stack & Queue Operations in Data Structure
Meghaj Mallick
 
MCRL2
MCRL2MCRL2
MCRL2
kashif kashif
 
Introduction to Reactive programming
Introduction to Reactive programmingIntroduction to Reactive programming
Introduction to Reactive programming
Dwi Randy Herdinanto
 
Monitoring using Prometheus and Grafana
Monitoring using Prometheus and GrafanaMonitoring using Prometheus and Grafana
Monitoring using Prometheus and Grafana
Arvind Kumar G.S
 
Building Scalable Stateless Applications with RxJava
Building Scalable Stateless Applications with RxJavaBuilding Scalable Stateless Applications with RxJava
Building Scalable Stateless Applications with RxJava
Rick Warren
 
Infrastructure & System Monitoring using Prometheus
Infrastructure & System Monitoring using PrometheusInfrastructure & System Monitoring using Prometheus
Infrastructure & System Monitoring using Prometheus
Marco Pas
 
CS106 Lab 10 - Functions (passing by value)
CS106 Lab 10 - Functions (passing by value)CS106 Lab 10 - Functions (passing by value)
CS106 Lab 10 - Functions (passing by value)
Nada Kamel
 
Reactive by example - at Reversim Summit 2015
Reactive by example - at Reversim Summit 2015Reactive by example - at Reversim Summit 2015
Reactive by example - at Reversim Summit 2015
Eran Harel
 
Streams, Streams Everywhere! An Introduction to Rx
Streams, Streams Everywhere! An Introduction to RxStreams, Streams Everywhere! An Introduction to Rx
Streams, Streams Everywhere! An Introduction to Rx
Andrzej Sitek
 
Spring batch showCase
Spring batch showCaseSpring batch showCase
Spring batch showCase
taher abdo
 
CS106 Lab 7 - For loop
CS106 Lab 7 - For loopCS106 Lab 7 - For loop
CS106 Lab 7 - For loop
Nada Kamel
 
09 advanced c#
09 advanced c#09 advanced c#
09 advanced c#
eleksdev
 
Flink Forward Berlin 2017: Matt Zimmer - Custom, Complex Windows at Scale Usi...
Flink Forward Berlin 2017: Matt Zimmer - Custom, Complex Windows at Scale Usi...Flink Forward Berlin 2017: Matt Zimmer - Custom, Complex Windows at Scale Usi...
Flink Forward Berlin 2017: Matt Zimmer - Custom, Complex Windows at Scale Usi...
Flink Forward
 
Top 10 RxJs Operators in Angular
Top 10 RxJs Operators in Angular Top 10 RxJs Operators in Angular
Top 10 RxJs Operators in Angular
Jalpesh Vadgama
 
Flink Forward SF 2017: Feng Wang & Zhijiang Wang - Runtime Improvements in Bl...
Flink Forward SF 2017: Feng Wang & Zhijiang Wang - Runtime Improvements in Bl...Flink Forward SF 2017: Feng Wang & Zhijiang Wang - Runtime Improvements in Bl...
Flink Forward SF 2017: Feng Wang & Zhijiang Wang - Runtime Improvements in Bl...
Flink Forward
 
CS106 Lab 6 - While and Do..While loop
CS106 Lab 6 - While and Do..While loopCS106 Lab 6 - While and Do..While loop
CS106 Lab 6 - While and Do..While loop
Nada Kamel
 
Study & Analysis of Complexities of Stack & Queue Operations in Data Structure
Study & Analysis of Complexities of Stack & Queue Operations in Data StructureStudy & Analysis of Complexities of Stack & Queue Operations in Data Structure
Study & Analysis of Complexities of Stack & Queue Operations in Data Structure
Meghaj Mallick
 
Introduction to Reactive programming
Introduction to Reactive programmingIntroduction to Reactive programming
Introduction to Reactive programming
Dwi Randy Herdinanto
 
Monitoring using Prometheus and Grafana
Monitoring using Prometheus and GrafanaMonitoring using Prometheus and Grafana
Monitoring using Prometheus and Grafana
Arvind Kumar G.S
 

Similar to Effective java item 80 and 81 (20)

Concurrent Programming in Java
Concurrent Programming in JavaConcurrent Programming in Java
Concurrent Programming in Java
Ruben Inoto Soto
 
Parallel Programming With Dot Net
Parallel Programming With Dot NetParallel Programming With Dot Net
Parallel Programming With Dot Net
Neeraj Kaushik
 
Java util concurrent
Java util concurrentJava util concurrent
Java util concurrent
Roger Xia
 
Async and parallel patterns and application design - TechDays2013 NL
Async and parallel patterns and application design - TechDays2013 NLAsync and parallel patterns and application design - TechDays2013 NL
Async and parallel patterns and application design - TechDays2013 NL
Arie Leeuwesteijn
 
.NET Multithreading/Multitasking
.NET Multithreading/Multitasking.NET Multithreading/Multitasking
.NET Multithreading/Multitasking
Sasha Kravchuk
 
Workflow as code with Azure Durable Functions
Workflow as code with Azure Durable FunctionsWorkflow as code with Azure Durable Functions
Workflow as code with Azure Durable Functions
Massimo Bonanni
 
What is new in java 8 concurrency
What is new in java 8 concurrencyWhat is new in java 8 concurrency
What is new in java 8 concurrency
kshanth2101
 
In Class AssignmetzCST280W13a-1.pdfCST 280 In-Class Pract.docx
In Class AssignmetzCST280W13a-1.pdfCST 280 In-Class Pract.docxIn Class AssignmetzCST280W13a-1.pdfCST 280 In-Class Pract.docx
In Class AssignmetzCST280W13a-1.pdfCST 280 In-Class Pract.docx
bradburgess22840
 
.Net Multithreading and Parallelization
.Net Multithreading and Parallelization.Net Multithreading and Parallelization
.Net Multithreading and Parallelization
Dmitri Nesteruk
 
Groovy concurrency
Groovy concurrencyGroovy concurrency
Groovy concurrency
Alex Miller
 
Advanced patterns in asynchronous programming
Advanced patterns in asynchronous programmingAdvanced patterns in asynchronous programming
Advanced patterns in asynchronous programming
Michael Arenzon
 
Concurrency and Thread-Safe Data Processing in Background Tasks
Concurrency and Thread-Safe Data Processing in Background TasksConcurrency and Thread-Safe Data Processing in Background Tasks
Concurrency and Thread-Safe Data Processing in Background Tasks
WO Community
 
Container Orchestration from Theory to Practice
Container Orchestration from Theory to PracticeContainer Orchestration from Theory to Practice
Container Orchestration from Theory to Practice
Docker, Inc.
 
Java Concurrency in Practice
Java Concurrency in PracticeJava Concurrency in Practice
Java Concurrency in Practice
Alina Dolgikh
 
Fork and join framework
Fork and join frameworkFork and join framework
Fork and join framework
Minh Tran
 
Code Kata: String Calculator in Flex
Code Kata: String Calculator in FlexCode Kata: String Calculator in Flex
Code Kata: String Calculator in Flex
Chris Farrell
 
Threads
ThreadsThreads
Threads
Sameer Shaik
 
Java concurrency
Java concurrencyJava concurrency
Java concurrency
Abhijit Gaikwad
 
Stateful streaming data pipelines
Stateful streaming data pipelinesStateful streaming data pipelines
Stateful streaming data pipelines
Timothy Farkas
 
Twins: OOP and FP
Twins: OOP and FPTwins: OOP and FP
Twins: OOP and FP
RichardWarburton
 
Concurrent Programming in Java
Concurrent Programming in JavaConcurrent Programming in Java
Concurrent Programming in Java
Ruben Inoto Soto
 
Parallel Programming With Dot Net
Parallel Programming With Dot NetParallel Programming With Dot Net
Parallel Programming With Dot Net
Neeraj Kaushik
 
Java util concurrent
Java util concurrentJava util concurrent
Java util concurrent
Roger Xia
 
Async and parallel patterns and application design - TechDays2013 NL
Async and parallel patterns and application design - TechDays2013 NLAsync and parallel patterns and application design - TechDays2013 NL
Async and parallel patterns and application design - TechDays2013 NL
Arie Leeuwesteijn
 
.NET Multithreading/Multitasking
.NET Multithreading/Multitasking.NET Multithreading/Multitasking
.NET Multithreading/Multitasking
Sasha Kravchuk
 
Workflow as code with Azure Durable Functions
Workflow as code with Azure Durable FunctionsWorkflow as code with Azure Durable Functions
Workflow as code with Azure Durable Functions
Massimo Bonanni
 
What is new in java 8 concurrency
What is new in java 8 concurrencyWhat is new in java 8 concurrency
What is new in java 8 concurrency
kshanth2101
 
In Class AssignmetzCST280W13a-1.pdfCST 280 In-Class Pract.docx
In Class AssignmetzCST280W13a-1.pdfCST 280 In-Class Pract.docxIn Class AssignmetzCST280W13a-1.pdfCST 280 In-Class Pract.docx
In Class AssignmetzCST280W13a-1.pdfCST 280 In-Class Pract.docx
bradburgess22840
 
.Net Multithreading and Parallelization
.Net Multithreading and Parallelization.Net Multithreading and Parallelization
.Net Multithreading and Parallelization
Dmitri Nesteruk
 
Groovy concurrency
Groovy concurrencyGroovy concurrency
Groovy concurrency
Alex Miller
 
Advanced patterns in asynchronous programming
Advanced patterns in asynchronous programmingAdvanced patterns in asynchronous programming
Advanced patterns in asynchronous programming
Michael Arenzon
 
Concurrency and Thread-Safe Data Processing in Background Tasks
Concurrency and Thread-Safe Data Processing in Background TasksConcurrency and Thread-Safe Data Processing in Background Tasks
Concurrency and Thread-Safe Data Processing in Background Tasks
WO Community
 
Container Orchestration from Theory to Practice
Container Orchestration from Theory to PracticeContainer Orchestration from Theory to Practice
Container Orchestration from Theory to Practice
Docker, Inc.
 
Java Concurrency in Practice
Java Concurrency in PracticeJava Concurrency in Practice
Java Concurrency in Practice
Alina Dolgikh
 
Fork and join framework
Fork and join frameworkFork and join framework
Fork and join framework
Minh Tran
 
Code Kata: String Calculator in Flex
Code Kata: String Calculator in FlexCode Kata: String Calculator in Flex
Code Kata: String Calculator in Flex
Chris Farrell
 
Stateful streaming data pipelines
Stateful streaming data pipelinesStateful streaming data pipelines
Stateful streaming data pipelines
Timothy Farkas
 
Ad

More from Isaac Liao (8)

Design of everyday things fundamental principles of interaction - V2
Design of everyday things   fundamental principles of interaction - V2Design of everyday things   fundamental principles of interaction - V2
Design of everyday things fundamental principles of interaction - V2
Isaac Liao
 
Design of everyday things fundamental principles of interaction
Design of everyday things   fundamental principles of interactionDesign of everyday things   fundamental principles of interaction
Design of everyday things fundamental principles of interaction
Isaac Liao
 
Java reference objects basic
Java reference objects   basicJava reference objects   basic
Java reference objects basic
Isaac Liao
 
Coding practice
Coding practiceCoding practice
Coding practice
Isaac Liao
 
Concurrent package classes
Concurrent package classesConcurrent package classes
Concurrent package classes
Isaac Liao
 
Count downlatch &amp; implementation
Count downlatch &amp; implementationCount downlatch &amp; implementation
Count downlatch &amp; implementation
Isaac Liao
 
Study effective java item 78 synchronize access to mutable data
Study effective java   item 78  synchronize access to mutable dataStudy effective java   item 78  synchronize access to mutable data
Study effective java item 78 synchronize access to mutable data
Isaac Liao
 
Concurrency
ConcurrencyConcurrency
Concurrency
Isaac Liao
 
Design of everyday things fundamental principles of interaction - V2
Design of everyday things   fundamental principles of interaction - V2Design of everyday things   fundamental principles of interaction - V2
Design of everyday things fundamental principles of interaction - V2
Isaac Liao
 
Design of everyday things fundamental principles of interaction
Design of everyday things   fundamental principles of interactionDesign of everyday things   fundamental principles of interaction
Design of everyday things fundamental principles of interaction
Isaac Liao
 
Java reference objects basic
Java reference objects   basicJava reference objects   basic
Java reference objects basic
Isaac Liao
 
Coding practice
Coding practiceCoding practice
Coding practice
Isaac Liao
 
Concurrent package classes
Concurrent package classesConcurrent package classes
Concurrent package classes
Isaac Liao
 
Count downlatch &amp; implementation
Count downlatch &amp; implementationCount downlatch &amp; implementation
Count downlatch &amp; implementation
Isaac Liao
 
Study effective java item 78 synchronize access to mutable data
Study effective java   item 78  synchronize access to mutable dataStudy effective java   item 78  synchronize access to mutable data
Study effective java item 78 synchronize access to mutable data
Isaac Liao
 
Ad

Recently uploaded (20)

Codingo Ltd. - Introduction - Mobile application, web, custom software develo...
Codingo Ltd. - Introduction - Mobile application, web, custom software develo...Codingo Ltd. - Introduction - Mobile application, web, custom software develo...
Codingo Ltd. - Introduction - Mobile application, web, custom software develo...
Codingo
 
Beyond the code. Complexity - 2025.05 - SwiftCraft
Beyond the code. Complexity - 2025.05 - SwiftCraftBeyond the code. Complexity - 2025.05 - SwiftCraft
Beyond the code. Complexity - 2025.05 - SwiftCraft
Dmitrii Ivanov
 
Lumion Pro Crack + 2025 Activation Key Free Code
Lumion Pro Crack + 2025 Activation Key Free CodeLumion Pro Crack + 2025 Activation Key Free Code
Lumion Pro Crack + 2025 Activation Key Free Code
raheemk1122g
 
The-Future-is-Hybrid-Exploring-Azure’s-Role-in-Multi-Cloud-Strategies.pptx
The-Future-is-Hybrid-Exploring-Azure’s-Role-in-Multi-Cloud-Strategies.pptxThe-Future-is-Hybrid-Exploring-Azure’s-Role-in-Multi-Cloud-Strategies.pptx
The-Future-is-Hybrid-Exploring-Azure’s-Role-in-Multi-Cloud-Strategies.pptx
james brownuae
 
Mastering Selenium WebDriver: A Comprehensive Tutorial with Real-World Examples
Mastering Selenium WebDriver: A Comprehensive Tutorial with Real-World ExamplesMastering Selenium WebDriver: A Comprehensive Tutorial with Real-World Examples
Mastering Selenium WebDriver: A Comprehensive Tutorial with Real-World Examples
jamescantor38
 
Welcome to QA Summit 2025.
Welcome to QA Summit 2025.Welcome to QA Summit 2025.
Welcome to QA Summit 2025.
QA Summit
 
Mobile Application Developer Dubai | Custom App Solutions by Ajath
Mobile Application Developer Dubai | Custom App Solutions by AjathMobile Application Developer Dubai | Custom App Solutions by Ajath
Mobile Application Developer Dubai | Custom App Solutions by Ajath
Ajath Infotech Technologies LLC
 
GC Tuning: A Masterpiece in Performance Engineering
GC Tuning: A Masterpiece in Performance EngineeringGC Tuning: A Masterpiece in Performance Engineering
GC Tuning: A Masterpiece in Performance Engineering
Tier1 app
 
Buy vs. Build: Unlocking the right path for your training tech
Buy vs. Build: Unlocking the right path for your training techBuy vs. Build: Unlocking the right path for your training tech
Buy vs. Build: Unlocking the right path for your training tech
Rustici Software
 
Reinventing Microservices Efficiency and Innovation with Single-Runtime
Reinventing Microservices Efficiency and Innovation with Single-RuntimeReinventing Microservices Efficiency and Innovation with Single-Runtime
Reinventing Microservices Efficiency and Innovation with Single-Runtime
Natan Silnitsky
 
Robotic Process Automation (RPA) Software Development Services.pptx
Robotic Process Automation (RPA) Software Development Services.pptxRobotic Process Automation (RPA) Software Development Services.pptx
Robotic Process Automation (RPA) Software Development Services.pptx
julia smits
 
!%& IDM Crack with Internet Download Manager 6.42 Build 32 >
!%& IDM Crack with Internet Download Manager 6.42 Build 32 >!%& IDM Crack with Internet Download Manager 6.42 Build 32 >
!%& IDM Crack with Internet Download Manager 6.42 Build 32 >
Ranking Google
 
Let's Do Bad Things to Unsecured Containers
Let's Do Bad Things to Unsecured ContainersLet's Do Bad Things to Unsecured Containers
Let's Do Bad Things to Unsecured Containers
Gene Gotimer
 
Deploying & Testing Agentforce - End-to-end with Copado - Ewenb Clark
Deploying & Testing Agentforce - End-to-end with Copado - Ewenb ClarkDeploying & Testing Agentforce - End-to-end with Copado - Ewenb Clark
Deploying & Testing Agentforce - End-to-end with Copado - Ewenb Clark
Peter Caitens
 
Why CoTester Is the AI Testing Tool QA Teams Can’t Ignore
Why CoTester Is the AI Testing Tool QA Teams Can’t IgnoreWhy CoTester Is the AI Testing Tool QA Teams Can’t Ignore
Why CoTester Is the AI Testing Tool QA Teams Can’t Ignore
Shubham Joshi
 
NYC ACE 08-May-2025-Combined Presentation.pdf
NYC ACE 08-May-2025-Combined Presentation.pdfNYC ACE 08-May-2025-Combined Presentation.pdf
NYC ACE 08-May-2025-Combined Presentation.pdf
AUGNYC
 
Download 4k Video Downloader Crack Pre-Activated
Download 4k Video Downloader Crack Pre-ActivatedDownload 4k Video Downloader Crack Pre-Activated
Download 4k Video Downloader Crack Pre-Activated
Web Designer
 
Do not let staffing shortages and limited fiscal view hamper your cause
Do not let staffing shortages and limited fiscal view hamper your causeDo not let staffing shortages and limited fiscal view hamper your cause
Do not let staffing shortages and limited fiscal view hamper your cause
Fexle Services Pvt. Ltd.
 
iTop VPN With Crack Lifetime Activation Key
iTop VPN With Crack Lifetime Activation KeyiTop VPN With Crack Lifetime Activation Key
iTop VPN With Crack Lifetime Activation Key
raheemk1122g
 
Time Estimation: Expert Tips & Proven Project Techniques
Time Estimation: Expert Tips & Proven Project TechniquesTime Estimation: Expert Tips & Proven Project Techniques
Time Estimation: Expert Tips & Proven Project Techniques
Livetecs LLC
 
Codingo Ltd. - Introduction - Mobile application, web, custom software develo...
Codingo Ltd. - Introduction - Mobile application, web, custom software develo...Codingo Ltd. - Introduction - Mobile application, web, custom software develo...
Codingo Ltd. - Introduction - Mobile application, web, custom software develo...
Codingo
 
Beyond the code. Complexity - 2025.05 - SwiftCraft
Beyond the code. Complexity - 2025.05 - SwiftCraftBeyond the code. Complexity - 2025.05 - SwiftCraft
Beyond the code. Complexity - 2025.05 - SwiftCraft
Dmitrii Ivanov
 
Lumion Pro Crack + 2025 Activation Key Free Code
Lumion Pro Crack + 2025 Activation Key Free CodeLumion Pro Crack + 2025 Activation Key Free Code
Lumion Pro Crack + 2025 Activation Key Free Code
raheemk1122g
 
The-Future-is-Hybrid-Exploring-Azure’s-Role-in-Multi-Cloud-Strategies.pptx
The-Future-is-Hybrid-Exploring-Azure’s-Role-in-Multi-Cloud-Strategies.pptxThe-Future-is-Hybrid-Exploring-Azure’s-Role-in-Multi-Cloud-Strategies.pptx
The-Future-is-Hybrid-Exploring-Azure’s-Role-in-Multi-Cloud-Strategies.pptx
james brownuae
 
Mastering Selenium WebDriver: A Comprehensive Tutorial with Real-World Examples
Mastering Selenium WebDriver: A Comprehensive Tutorial with Real-World ExamplesMastering Selenium WebDriver: A Comprehensive Tutorial with Real-World Examples
Mastering Selenium WebDriver: A Comprehensive Tutorial with Real-World Examples
jamescantor38
 
Welcome to QA Summit 2025.
Welcome to QA Summit 2025.Welcome to QA Summit 2025.
Welcome to QA Summit 2025.
QA Summit
 
Mobile Application Developer Dubai | Custom App Solutions by Ajath
Mobile Application Developer Dubai | Custom App Solutions by AjathMobile Application Developer Dubai | Custom App Solutions by Ajath
Mobile Application Developer Dubai | Custom App Solutions by Ajath
Ajath Infotech Technologies LLC
 
GC Tuning: A Masterpiece in Performance Engineering
GC Tuning: A Masterpiece in Performance EngineeringGC Tuning: A Masterpiece in Performance Engineering
GC Tuning: A Masterpiece in Performance Engineering
Tier1 app
 
Buy vs. Build: Unlocking the right path for your training tech
Buy vs. Build: Unlocking the right path for your training techBuy vs. Build: Unlocking the right path for your training tech
Buy vs. Build: Unlocking the right path for your training tech
Rustici Software
 
Reinventing Microservices Efficiency and Innovation with Single-Runtime
Reinventing Microservices Efficiency and Innovation with Single-RuntimeReinventing Microservices Efficiency and Innovation with Single-Runtime
Reinventing Microservices Efficiency and Innovation with Single-Runtime
Natan Silnitsky
 
Robotic Process Automation (RPA) Software Development Services.pptx
Robotic Process Automation (RPA) Software Development Services.pptxRobotic Process Automation (RPA) Software Development Services.pptx
Robotic Process Automation (RPA) Software Development Services.pptx
julia smits
 
!%& IDM Crack with Internet Download Manager 6.42 Build 32 >
!%& IDM Crack with Internet Download Manager 6.42 Build 32 >!%& IDM Crack with Internet Download Manager 6.42 Build 32 >
!%& IDM Crack with Internet Download Manager 6.42 Build 32 >
Ranking Google
 
Let's Do Bad Things to Unsecured Containers
Let's Do Bad Things to Unsecured ContainersLet's Do Bad Things to Unsecured Containers
Let's Do Bad Things to Unsecured Containers
Gene Gotimer
 
Deploying & Testing Agentforce - End-to-end with Copado - Ewenb Clark
Deploying & Testing Agentforce - End-to-end with Copado - Ewenb ClarkDeploying & Testing Agentforce - End-to-end with Copado - Ewenb Clark
Deploying & Testing Agentforce - End-to-end with Copado - Ewenb Clark
Peter Caitens
 
Why CoTester Is the AI Testing Tool QA Teams Can’t Ignore
Why CoTester Is the AI Testing Tool QA Teams Can’t IgnoreWhy CoTester Is the AI Testing Tool QA Teams Can’t Ignore
Why CoTester Is the AI Testing Tool QA Teams Can’t Ignore
Shubham Joshi
 
NYC ACE 08-May-2025-Combined Presentation.pdf
NYC ACE 08-May-2025-Combined Presentation.pdfNYC ACE 08-May-2025-Combined Presentation.pdf
NYC ACE 08-May-2025-Combined Presentation.pdf
AUGNYC
 
Download 4k Video Downloader Crack Pre-Activated
Download 4k Video Downloader Crack Pre-ActivatedDownload 4k Video Downloader Crack Pre-Activated
Download 4k Video Downloader Crack Pre-Activated
Web Designer
 
Do not let staffing shortages and limited fiscal view hamper your cause
Do not let staffing shortages and limited fiscal view hamper your causeDo not let staffing shortages and limited fiscal view hamper your cause
Do not let staffing shortages and limited fiscal view hamper your cause
Fexle Services Pvt. Ltd.
 
iTop VPN With Crack Lifetime Activation Key
iTop VPN With Crack Lifetime Activation KeyiTop VPN With Crack Lifetime Activation Key
iTop VPN With Crack Lifetime Activation Key
raheemk1122g
 
Time Estimation: Expert Tips & Proven Project Techniques
Time Estimation: Expert Tips & Proven Project TechniquesTime Estimation: Expert Tips & Proven Project Techniques
Time Estimation: Expert Tips & Proven Project Techniques
Livetecs LLC
 

Effective java item 80 and 81

  • 1. Effective Java Item 80: Prefer executors, tasks, and streams to threads Item 81: Prefer concurrency utilities to wait and notify
  • 2. Item 80 Prefer executors, tasks, and streams to threads
  • 3. Executor Framework - Basic Example ● Submit a runnable for execution ExecutorService exec = Executors.newSingleThreadExecutor(); exec.execute(runnable); exec.shutdown();
  • 4. Executor Framework - invokeAny - Wait until first done
  • 5. Executor Framework - invokeAny - Wait until ALL done
  • 6. Executor Framework - invokeAny - Wait until ALL done ● Hard to get future result by complete sequence
  • 7. Executor Framework - ExecutorCompletionService ● Get future result by sequence
  • 8. Executor Framework - Cancel rest jobs
  • 9. Executor Framework - Schedule task
  • 10. Executor Framework - Choose executor ● Executors.newCachedThreadPool ○ Easy, no configuration ○ Do the right thing ○ When no available thread, new one will be added to pool ○ No task will be queued ○ Choose for small program ■ Why not heavy load program?
  • 11. Executor Framework - Choose executor ● Executors.newCachedThreadPool ○ Easy, no configuration ○ Do the right thing ○ When no available thread, new one will be added to pool ○ No task will be queued ○ Choose for small program ■ Why not heavy load program? ● all of CPUs are fully utilized ● more tasks arrive, more threads will be created ● less CPU available, cause matters worse
  • 12. Executor Framework - Choose executor ● Executors.newFixedThreadPool ○ Gives a pool with fixed number of threads ○ For heavy load program ○ Still need monitor queue size
  • 13. Runnable & Callable ● Runnable: no return value ● Callable: return value
  • 14. ForkJoinPool ● Normal Executor Thread Management ○ Main thread put task to queue ○ Sub tasks put tasks to queue ○ Every task in a queue without boundry Main Time
  • 15. ForkJoinPool ● ForkJoinPool Thread Management ○ Main thread create tasks ○ Tasks create sub tasks to fork ○ tasks wait for sub tasks to join ○ Main thread get result or wait for done Time task1 Main task1-1 task1-2 task2 task2-1 task2-2 Time
  • 17. Item 81 Prefer concurrency utilities to wait and notify
  • 18. It’s difficult “Given the difficulity of using wait and notify correctly, you should use the higher-level concurrency utilities instead”
  • 19. Related Topics ● Executor Framework - Item 80 ● Concurrent collections - Item 81 ● Synchronizers - Item 81
  • 20. Concurrent collections ● They are high-performance concurrent implementation of standard collection interfaces. ● Each implementations need manage their own synchronization internally. ● It is impossible to exclude concurrent activity from a concurrent collection ● locking it will only slow the program
  • 21. Compose method invocations ● You can’t exclude concurrent activity on concurrent collections (will become slow), you can’t automatically compose method invocations on them either. ● So concurrent collection interfaces were outfitted with state-dependent modify operations, combine several primitives into a single atomic operation. Ex. Map.putIfAbsent(key, value)
  • 22. String#intern String#intern will find string from cache before creating new instance. String s1 = “test”; String s2 = new String(“test”); String s3 = new String(“test”).intern(); System.out.println(s1 == s2); // false System.out.println(s1 == s3); // true
  • 23. String#intern implementation by putIfAbsent private static final ConcurrentMap<String,String> map = new ConcurrentHashMap<>(); public static String intern(String s) { String pre = map.putIfAbsent(s, s); return pre == null ? s : pre; }
  • 24. String#intern implemented by ConcurrentHashMap ConcurrentHashMap is optimized for retrieval operations, such as get. public static String intern(String s) { String result = map.get(s); if (result == null) { result = map.putIfAbsent(s, s); if (result == null) { result = s; } } return result; }
  • 25. ConcurrentHashMap is high performance Replace Collections.synchronizedMap by new ConcurrentHashMap can dramatically increase performance.
  • 26. BlockingQueue ● take method will wait until queue is not empty
  • 27. Synchronizer ● Enable threads to wait for one another ● Ex. CountDownLatch: Wait until count down to zero ● Ex. Semaphore: Wait until token available ● Ex. CylicBarrier: Wait until all are waiting ● Ex. Exchanger: Wait until another thread is ready ● Ex. Phaser: Wait until other arrived
  • 28. Legacy ● Always use the wait loop idiom to invoke the wait method, never invoke it outside of a loop synchronized (obj) { while (<condition>) { obj.wait(); } }
  翻译: