SlideShare a Scribd company logo
Concurrency
Isaac
Agenda
● Some JVM knowledge
● Some basic concurrency concepts
● Some personal design suggestions
Reference
● Some JVM knowledge - Java Concurrency in Practice
● Some basic concurrency concepts - Clean Code
● Some personal design suggestions
Won’t be mentioned
● Practices to write concurrency program
● Patterns or anti-patterns in concurrency programming
● java.util.concurrent API example
● More detail in JVM
They are big topics, maybe other Tech Talks.
Or we can discuss with each other for interesting.
Target audience
● Java developers who don’t know about concurrency
● Your code will run in multi-threaded environment
Goal
● Be aware concurrency issue while programming
● Get some material or direction to study
● Let concurrency become a famous discussion between developers
Why Concurrency
● CPU core size maybe 8-24
But there are thousands of threads…
Currency must be a very important topic in development
What is Concurrency?
… This[Concurrency] means that even if the concurrent units
of the program, algorithm, or problem are executed
out-of-order or in partial order, the final outcome will
remain the same…
~ WIKI
What is “out-of-order”?
In this paradigm [out-of-order-execution], a processor
executes instructions in an order governed by the availability
of input data, rather than by their original order in a
program. In doing so, the processor can avoid being idle
while waiting for the preceding instruction to complete to
retrieve data for the next instruction
~ WIKI
Is it related to my java code?
Java code Reordered (Maybe)
public int test() {
int a = 1;
a++;
int b = 2;
b++;
return a+b;
}
public int test() {
int a = 1, b = 2; // load var and data
a++; // read and write
b++; // read and write
return a+b; // result is same after reordered
}
// Should be faster in a smater way…
// Most important is we don’t know the order
It seems not my business, should I care?
First of all, the program will be loaded to a CPU core to run.
CPU Core1 CPU Core2
Program in memory
Program Program
It seems not my business, should I care?
CPU only care about to run program faster and correct.
CPU don’t care about the status between threads without special command.
What will happen in main thread?
Thread A Thread B
a=1;
x=b;
b=1;
y=a;Main Thread
1. Start thread A
2. Start thread B
3. Get x and y
int x=0,y=0;
int a=0,b=0;
It seems not my business, should I care?
We can’t expect the results are always identical…
Thread A
Execution
1. a=1,x=0
2. a=1,x=1
Thread B
Execution
1. b=1,y=0
2. b=1,y=1
int x=0,y=0;
int a=0,b=0;
a=1;
x=b;
b=1;
y=a;Main Thread
1. Start thread A
2. Start thread B
3. Read x and y
Result:
1. x=0,y=1 // B finished
2. x=1,y=0 // A finished
3. x=1,y=1 // Both finished
4. x=0,y=0 // Both not finished
// and reordered
int x=0,y=0;
int a=0,b=0;
It seems not my business, should I care?
● Result 1: x=0, y=1
ThreadA ThreadB Main Thread
Code a=1;
x=b;
b=1;
y=a;
Start thread A
Start thread B
Read x and y
Cycle 1 a=1; b=1;
Cycle 2 y=a; Get x=0, y=1
Cycle 3 x=b;
It seems not my business, should I care?
● Result 2: x=1, y=0
ThreadA ThreadB Main Thread
Code a=1;
x=b;
b=1;
y=a;
Start thread A
Start thread B
Read x and y
Cycle 1 a=1; b=1;
Cycle 2 x=b; Get x=1, y=0
Cycle 3 y=a;
It seems not my business, should I care?
● Result 3: x=1, y=1
ThreadA ThreadB Main Thread
Code a=1;
x=b;
b=1;
y=a;
Start thread A
Start thread B
Read x and y
Cycle 1 a=1; b=1;
Cycle 2 x=b; y=a;
Cycle 3 Get x=1, y=1
It seems not my business, should I care?
● Result 4: x=0, y=0
ThreadA ThreadB Main Thread
Code a=1;
x=b;
b=1;
y=a;
Start thread A
Start thread B
Read x and y
Cycle 1 x=b; y=a;
Cycle 2 a=1; b=1;
Cycle 3 Get x=0, y=0
Another example more close to our daily work
class APManager {
ArrayList<AP> aps;
// online, offline AP
// register, unregister AP
}
class APStatusListener {
ArrayList<AP> aps; // set from APManager
public void infiniteCheck() {
while (true) {
for (AP ap: aps) {
if (ap.isOnline()) {...}
else {...}
}
}
}
}class AP {
String status; // online, offline
}
What problem in APStatusListener?
Another example more close to our daily work
class APManager {
ArrayList<AP> aps;
// online, offline AP
// register, unregister AP
}
class APStatusListener {
ArrayList<AP> aps; // set from APManager
public void infiniteCheck() {
while (true) {
for (AP ap: aps) {
if (ap.isOnline()) {...}
else {...}
}
}
}
}
May be always initial data.
No new AP
No AP will be removed
No status change
class AP {
String status; // online, offline
}
How to fix?
class APManager {
final CopyOnWriteArrayList<AP> aps;
// online, offline AP, change AP.status
// register, unregister AP
}
class APStatusListener {
List<AP> aps; // set from APManager
public void infiniteCheck() {
while (true) {
for (AP ap: aps) {
if (ap.isOnline()) {...}
else {...}
}
}
}
}class AP {
volatile String status; // online, offline
}
How to fix?
class APManager {
final CopyOnWriteArrayList<AP> aps;
// online, offline AP, change AP.status
// register, unregister AP
}
class APStatusListener {
List<AP> aps; // set from APManager
public void infiniteCheck() {
while (true) {
for (AP ap: aps) {
if (ap.isOnline()) {...}
else {...}
}
}
}
}
class AP {
volatile String status; // online, offline
}
final => APStatusListener will see initialized aps
CopyOnWriteArrayList => APStatusListener see modified element
(Of course you can declare aps with List<AP> and CopyOnWriteArrayList<AP> implementation)
volatile => APStatusListener see AP status change
How it works?
After JAVA 5
JVM introduce JSR-133 Java Memory Model
who defined happens-before relationship
and give final field a special meaning to make things happen.
What is happens-before relationship?
It’s rules guarantee by JVM, used to define program order.
● Each action in a thread happens before every subsequent action in that thread.
● An unlock on a monitor happens before every subsequent lock on that monitor.
● A write to a volatile field happens before every subsequent read of that volatile.
● A call to start() on a thread happens before any actions in the started thread.
● All actions in a thread happen before any other thread successfully
returns from a join() on that thread.
● If an action a happens before an action b, and b happens before an action c,
then a happens before c.
How about final field?
JSR-133 defined that after an object was properly
constructed, a final field assigned in constructor can be
visible by all threads.
How about final field in the JVM before Java 5?
The final field in old JVM has no difference between general
field except it need assign value in constructor.
Developer still need to use synchronization to ensure the
variable modification visible in all threads.
What is CopyOnWriteArrayList?
It's a thread safe ArrayList implementation in
java.util.concurrent package, introduced from Java 5. It also
leverage Java Memory Model to make sure modifications
visible to other threads.
What is CopyOnWriteArrayList?
ArrayList#add
What is CopyOnWriteArrayList?
CopyOnWriteArrayList#add
Magic in ReentrantLock
setState write value to volatile state, make sure all lock holder get latest state.
Thus don’t need synchronized tryRelease
volatile is not a siliver buttlet
● It can’t perform N++ operation with just N++..
volatile int n = 0; Thread 1 Thread 2
n++; n = 0, n = n+1 n = 0, n = n+1
Get n n = 1; n = 1
How to perform N++ atomically?
● syncrhonized (monitor lock)
int n = 0; // don’t need volatile if using synchronized
public synchronized int incremenetAndGet() {
n++;
}
How to perform N++ atomically?
● AtomicInteger#incrementAndGet
How to perform N++ atomically?
Unsafe.java
How to perform N++ atomically?
unsafe.cpp
How to perform N++ atomically?
LLVM #cmpxchg
JVM knowledge summary
● reorder
● JSR-133 Java Memory Model
● happens-before relationship
● volatile for visibility
● LLVM memory barrier
Basic concurrency concepts
● Definitions
○ Bound Resource
○ Mutual Execution
○ Starvation
○ Deadlock
○ Livelock
● Execution Models
○ Producer-Consumer
○ Writers-Readers
○ Dining-Philosophers
Bound Resource
Resources of a fixed size or number used in a concurrent environment.
Mutual Execution
Only one thread can access shared data or a shared resource at a time.
Such as Java Synchronization
Starvation
One thread or a group of threads is prohibited from proceeding for an excessively
long time or forever.
Ex. Lower priority tasks may encounter Starvation.
(Because always higher priority task)
Deadlock
Two or more threads wait for each other to finish. Each thread has a resource that
the other thread requires and neither can finish until gets the other resource.
Thread 1 Thread 2
Lock1
Lock2
acquire acquire
waitwait
Thread1 and Thread2 all need
acquire Lock1 and Lock2
to do something.
But the order is different so
deadlock may happen.
Note: Acquire and release locks in
the same order can prevent
deadlock
Livelock
A thread often acts in response to the action of another thread. If the other
thread's action is also a response to the action of another thread, then livelock
may result.
Example:
A boy and girl are dating in a restaurant. They are shy.
They order a cup of drink to share.
They try to take the drink but find another also want to drink so stop back.
And again, and again, and again...until someone stop...or never...
Execution Models
● Producer-Consumer
● Writers-Readers
● Dining-Philosophers
Producer-Consumer
● Behavior
a. Producer threads create work in a queue
b. Consumer threads acquire work from queue and complete it
● Challenge
a. Queue is the bound resource
b. Producer need wait for free space in queue
c. Consumer need wait until something in queue
d. Producer may need notify Consumer queue is not empty
e. Consumer may need notify Producer queue is empty
Producer-Consumer
● Benefit
a. Observer pattern, used to decouple model or process
b. Easy to tune thread pool
because producer and consumer have different resource occupation style
● Risk
a. Needless decoupling cause duplication and hard to define model responsibility
b. Too many decoupliing cause system too complex and waste resource
● Example
a. Message Queue
b. Thread Pool
Readers-Writers
● Behavior
○ A shared resource serves as a source of information for Readers
○ Writers may update the shared resource
● Challenge
○ Throughput issue
■ Writer prevent reader read while updating
■ Reader prevent writer write while reading
○ Stale information problem while reader allowing writer update
Readers-Writers
● Risk
○ Try to prevent read stale data
■ System load heavy
■ Useless lock
■ User don’t care
● Example
○ Cassandra
○ ElasticSearch
Dining Philosophers
● Behavior
○ Number of philosophers sitting around a circle table
○ Philosophers can’t eat when they are thinking
○ Philosophers need pick up forks on either side of them and eat
○ Philosophers can’t eat unless they hold two forks
● Challenge
○ If the philosopher to his right or left is already using
one of the forks he need, he must wait until that
philosopher finish eating and put forks back down.
○ Deadlock when everyone pick one fork and wait for another finish
○ Livelock when everyone put forks back down
and try again later
Dining Philosophers
● Challenge
○ Throughput: A philosopher need to wait other finish, although he is not thinking
● Example
○ Programmatically
■ Need hold 2 or more locks to do something
● How to solve
○ A waiter track every forks status,
philosopher need ask waiter and pick up forks
○ Prioritize forks, philosophers need follow the priority
to pick up/put back forks
○ Philosopher who has no fork get a ticket,
use a ticket to replace 2 forks to eat.
Finished philosopher give forks to who has a ticket.
Personal design suggestion
Define the model you are designing is Single-source-of-truth or not.
● Better to use Singleton when it’s Single-source-of-truth.
This model must be thread safe
● Build immutable object if it’s the copy of Single-source-of-truth.
● Be aware the copy of Single-source-of-truth maybe stale
● Single Responsibility Principle is basic
Example
Seat it self
(Single-source-of-truth)Seat list 1 Seat list 2
Manager1
Manager2
Employee1
Employee2
We are going to change seats because of re-org.
Managers ask some people for change requirements..
Example
Seat it self
(Single-source-of-truth)
Seat list 1 Seat list 2
Manager1
Manager2
Employee1
Employee2
Singleton naturally
Maybe stale.
Need compare and swap
We are going to change seats because of re-org.
Managers ask some people for change requirements..
Study Materials
● Java Concurrency in Practice
● Clean Code
● Effective Java
● JSR-133 Java Memory Model
● JSR-133 FAQ
● LLVM Atomics
Thank you~

More Related Content

What's hot

JavaScript Loop: Optimization of Weak Typing
JavaScript Loop: Optimization of Weak TypingJavaScript Loop: Optimization of Weak Typing
JavaScript Loop: Optimization of Weak Typing
Janlay Wu
 
Java findamentals1
Java findamentals1Java findamentals1
Java findamentals1
Todor Kolev
 
Java findamentals1
Java findamentals1Java findamentals1
Java findamentals1
Todor Kolev
 
Java findamentals1
Java findamentals1Java findamentals1
Java findamentals1
Todor Kolev
 

What's hot (20)

Introduction to Java programming - Java tutorial for beginners to teach Java ...
Introduction to Java programming - Java tutorial for beginners to teach Java ...Introduction to Java programming - Java tutorial for beginners to teach Java ...
Introduction to Java programming - Java tutorial for beginners to teach Java ...
 
Working Effectively With Legacy Code
Working Effectively With Legacy CodeWorking Effectively With Legacy Code
Working Effectively With Legacy Code
 
Method Handles in Java
Method Handles in JavaMethod Handles in Java
Method Handles in Java
 
Basics java programing
Basics java programingBasics java programing
Basics java programing
 
Loops in java script
Loops in java scriptLoops in java script
Loops in java script
 
Java8 features
Java8 featuresJava8 features
Java8 features
 
Callback Function
Callback FunctionCallback Function
Callback Function
 
Javascript
JavascriptJavascript
Javascript
 
Intro to Asynchronous Javascript
Intro to Asynchronous JavascriptIntro to Asynchronous Javascript
Intro to Asynchronous Javascript
 
JavaScript Loop: Optimization of Weak Typing
JavaScript Loop: Optimization of Weak TypingJavaScript Loop: Optimization of Weak Typing
JavaScript Loop: Optimization of Weak Typing
 
Clean Code with Java 8 - Functional Patterns and Best Practices
Clean Code with Java 8 - Functional Patterns and Best PracticesClean Code with Java 8 - Functional Patterns and Best Practices
Clean Code with Java 8 - Functional Patterns and Best Practices
 
Asynchronous JavaScript Programming with Callbacks & Promises
Asynchronous JavaScript Programming with Callbacks & PromisesAsynchronous JavaScript Programming with Callbacks & Promises
Asynchronous JavaScript Programming with Callbacks & Promises
 
Jvm performance tuning
Jvm performance tuningJvm performance tuning
Jvm performance tuning
 
Viva file
Viva fileViva file
Viva file
 
Java findamentals1
Java findamentals1Java findamentals1
Java findamentals1
 
Java findamentals1
Java findamentals1Java findamentals1
Java findamentals1
 
Java findamentals1
Java findamentals1Java findamentals1
Java findamentals1
 
Deep dive into Android async operations
Deep dive into Android async operationsDeep dive into Android async operations
Deep dive into Android async operations
 
Functional programming principles and Java 8
Functional programming principles and Java 8Functional programming principles and Java 8
Functional programming principles and Java 8
 
java threads
java threadsjava threads
java threads
 

Viewers also liked

Viewers also liked (20)

Java Concurrency, Memory Model, and Trends
Java Concurrency, Memory Model, and TrendsJava Concurrency, Memory Model, and Trends
Java Concurrency, Memory Model, and Trends
 
Actor Concurrency
Actor ConcurrencyActor Concurrency
Actor Concurrency
 
Memory Leaks on Android
Memory Leaks on AndroidMemory Leaks on Android
Memory Leaks on Android
 
Code lifecycle in the jvm - TopConf Linz
Code lifecycle in the jvm - TopConf LinzCode lifecycle in the jvm - TopConf Linz
Code lifecycle in the jvm - TopConf Linz
 
Java8 training - class 2
Java8 training - class 2Java8 training - class 2
Java8 training - class 2
 
Java8 training - Class 1
Java8 training  - Class 1Java8 training  - Class 1
Java8 training - Class 1
 
Java8 training - class 3
Java8 training - class 3Java8 training - class 3
Java8 training - class 3
 
Concurrency with java
Concurrency with javaConcurrency with java
Concurrency with java
 
Building Applications with a Graph Database
Building Applications with a Graph DatabaseBuilding Applications with a Graph Database
Building Applications with a Graph Database
 
What is concurrency
What is concurrencyWhat is concurrency
What is concurrency
 
Just-in-time compiler (March, 2017)
Just-in-time compiler (March, 2017)Just-in-time compiler (March, 2017)
Just-in-time compiler (March, 2017)
 
系統程式 -- 第 9 章
系統程式 -- 第 9 章系統程式 -- 第 9 章
系統程式 -- 第 9 章
 
系統程式 -- 第 8 章
系統程式 -- 第 8 章系統程式 -- 第 8 章
系統程式 -- 第 8 章
 
Alluxio (formerly Tachyon): Open Source Memory Speed Virtual Distributed Storage
Alluxio (formerly Tachyon): Open Source Memory Speed Virtual Distributed StorageAlluxio (formerly Tachyon): Open Source Memory Speed Virtual Distributed Storage
Alluxio (formerly Tachyon): Open Source Memory Speed Virtual Distributed Storage
 
Java8
Java8Java8
Java8
 
Jumping-with-java8
Jumping-with-java8Jumping-with-java8
Jumping-with-java8
 
Apache camel
Apache camelApache camel
Apache camel
 
Java Hands-On Workshop
Java Hands-On WorkshopJava Hands-On Workshop
Java Hands-On Workshop
 
Intel CPU Manufacturing Process
Intel CPU Manufacturing ProcessIntel CPU Manufacturing Process
Intel CPU Manufacturing Process
 
Concurrency & Parallel Programming
Concurrency & Parallel ProgrammingConcurrency & Parallel Programming
Concurrency & Parallel Programming
 

Similar to Concurrency

Medical Image Processing Strategies for multi-core CPUs
Medical Image Processing Strategies for multi-core CPUsMedical Image Processing Strategies for multi-core CPUs
Medical Image Processing Strategies for multi-core CPUs
Daniel Blezek
 
Programming with Threads in Java
Programming with Threads in JavaProgramming with Threads in Java
Programming with Threads in Java
koji lin
 

Similar to Concurrency (20)

Java For Automation
Java   For AutomationJava   For Automation
Java For Automation
 
Multithreading programming in java
Multithreading programming in javaMultithreading programming in java
Multithreading programming in java
 
Medical Image Processing Strategies for multi-core CPUs
Medical Image Processing Strategies for multi-core CPUsMedical Image Processing Strategies for multi-core CPUs
Medical Image Processing Strategies for multi-core CPUs
 
Synapseindia reviews.odp.
Synapseindia reviews.odp.Synapseindia reviews.odp.
Synapseindia reviews.odp.
 
Java Memory Model
Java Memory ModelJava Memory Model
Java Memory Model
 
Adv java unit 1 M.Sc CS.pdf
Adv java unit 1 M.Sc CS.pdfAdv java unit 1 M.Sc CS.pdf
Adv java unit 1 M.Sc CS.pdf
 
Java Tutorial
Java TutorialJava Tutorial
Java Tutorial
 
Java_Tutorial_Introduction_to_Core_java.ppt
Java_Tutorial_Introduction_to_Core_java.pptJava_Tutorial_Introduction_to_Core_java.ppt
Java_Tutorial_Introduction_to_Core_java.ppt
 
Java tut1
Java tut1Java tut1
Java tut1
 
Java tut1 Coderdojo Cahersiveen
Java tut1 Coderdojo CahersiveenJava tut1 Coderdojo Cahersiveen
Java tut1 Coderdojo Cahersiveen
 
Java tut1
Java tut1Java tut1
Java tut1
 
Javatut1
Javatut1 Javatut1
Javatut1
 
Java Concurrency and Asynchronous
Java Concurrency and AsynchronousJava Concurrency and Asynchronous
Java Concurrency and Asynchronous
 
Learn To Code: Introduction to java
Learn To Code: Introduction to javaLearn To Code: Introduction to java
Learn To Code: Introduction to java
 
Programming with Threads in Java
Programming with Threads in JavaProgramming with Threads in Java
Programming with Threads in Java
 
Best practices for ansible
Best practices for ansibleBest practices for ansible
Best practices for ansible
 
Java Tutorial | My Heart
Java Tutorial | My HeartJava Tutorial | My Heart
Java Tutorial | My Heart
 
Md09 multithreading
Md09 multithreadingMd09 multithreading
Md09 multithreading
 
Java introduction
Java introductionJava introduction
Java introduction
 
Ase02.ppt
Ase02.pptAse02.ppt
Ase02.ppt
 

More from Isaac Liao (9)

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
 
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
 
Java reference objects basic
Java reference objects   basicJava reference objects   basic
Java reference objects basic
 
Effective java item 80 and 81
Effective java   item 80 and 81Effective java   item 80 and 81
Effective java item 80 and 81
 
Effective java item 80 prefer executors, tasks, and streams to threads
Effective java   item 80  prefer executors, tasks, and  streams to threadsEffective java   item 80  prefer executors, tasks, and  streams to threads
Effective java item 80 prefer executors, tasks, and streams to threads
 
Coding practice
Coding practiceCoding practice
Coding practice
 
Concurrent package classes
Concurrent package classesConcurrent package classes
Concurrent package classes
 
Count downlatch &amp; implementation
Count downlatch &amp; implementationCount downlatch &amp; implementation
Count downlatch &amp; implementation
 
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
 

Recently uploaded

Hall booking system project report .pdf
Hall booking system project report  .pdfHall booking system project report  .pdf
Hall booking system project report .pdf
Kamal Acharya
 
Laundry management system project report.pdf
Laundry management system project report.pdfLaundry management system project report.pdf
Laundry management system project report.pdf
Kamal Acharya
 
Online blood donation management system project.pdf
Online blood donation management system project.pdfOnline blood donation management system project.pdf
Online blood donation management system project.pdf
Kamal Acharya
 
ONLINE VEHICLE RENTAL SYSTEM PROJECT REPORT.pdf
ONLINE VEHICLE RENTAL SYSTEM PROJECT REPORT.pdfONLINE VEHICLE RENTAL SYSTEM PROJECT REPORT.pdf
ONLINE VEHICLE RENTAL SYSTEM PROJECT REPORT.pdf
Kamal Acharya
 

Recently uploaded (20)

Courier management system project report.pdf
Courier management system project report.pdfCourier management system project report.pdf
Courier management system project report.pdf
 
Furniture showroom management system project.pdf
Furniture showroom management system project.pdfFurniture showroom management system project.pdf
Furniture showroom management system project.pdf
 
Scaling in conventional MOSFET for constant electric field and constant voltage
Scaling in conventional MOSFET for constant electric field and constant voltageScaling in conventional MOSFET for constant electric field and constant voltage
Scaling in conventional MOSFET for constant electric field and constant voltage
 
WATER CRISIS and its solutions-pptx 1234
WATER CRISIS and its solutions-pptx 1234WATER CRISIS and its solutions-pptx 1234
WATER CRISIS and its solutions-pptx 1234
 
Arduino based vehicle speed tracker project
Arduino based vehicle speed tracker projectArduino based vehicle speed tracker project
Arduino based vehicle speed tracker project
 
Immunizing Image Classifiers Against Localized Adversary Attacks
Immunizing Image Classifiers Against Localized Adversary AttacksImmunizing Image Classifiers Against Localized Adversary Attacks
Immunizing Image Classifiers Against Localized Adversary Attacks
 
Toll tax management system project report..pdf
Toll tax management system project report..pdfToll tax management system project report..pdf
Toll tax management system project report..pdf
 
Hall booking system project report .pdf
Hall booking system project report  .pdfHall booking system project report  .pdf
Hall booking system project report .pdf
 
2024 DevOps Pro Europe - Growing at the edge
2024 DevOps Pro Europe - Growing at the edge2024 DevOps Pro Europe - Growing at the edge
2024 DevOps Pro Europe - Growing at the edge
 
Laundry management system project report.pdf
Laundry management system project report.pdfLaundry management system project report.pdf
Laundry management system project report.pdf
 
Peek implant persentation - Copy (1).pdf
Peek implant persentation - Copy (1).pdfPeek implant persentation - Copy (1).pdf
Peek implant persentation - Copy (1).pdf
 
Architectural Portfolio Sean Lockwood
Architectural Portfolio Sean LockwoodArchitectural Portfolio Sean Lockwood
Architectural Portfolio Sean Lockwood
 
Natalia Rutkowska - BIM School Course in Kraków
Natalia Rutkowska - BIM School Course in KrakówNatalia Rutkowska - BIM School Course in Kraków
Natalia Rutkowska - BIM School Course in Kraków
 
Quality defects in TMT Bars, Possible causes and Potential Solutions.
Quality defects in TMT Bars, Possible causes and Potential Solutions.Quality defects in TMT Bars, Possible causes and Potential Solutions.
Quality defects in TMT Bars, Possible causes and Potential Solutions.
 
Online blood donation management system project.pdf
Online blood donation management system project.pdfOnline blood donation management system project.pdf
Online blood donation management system project.pdf
 
fundamentals of drawing and isometric and orthographic projection
fundamentals of drawing and isometric and orthographic projectionfundamentals of drawing and isometric and orthographic projection
fundamentals of drawing and isometric and orthographic projection
 
A case study of cinema management system project report..pdf
A case study of cinema management system project report..pdfA case study of cinema management system project report..pdf
A case study of cinema management system project report..pdf
 
RESORT MANAGEMENT AND RESERVATION SYSTEM PROJECT REPORT.pdf
RESORT MANAGEMENT AND RESERVATION SYSTEM PROJECT REPORT.pdfRESORT MANAGEMENT AND RESERVATION SYSTEM PROJECT REPORT.pdf
RESORT MANAGEMENT AND RESERVATION SYSTEM PROJECT REPORT.pdf
 
ONLINE VEHICLE RENTAL SYSTEM PROJECT REPORT.pdf
ONLINE VEHICLE RENTAL SYSTEM PROJECT REPORT.pdfONLINE VEHICLE RENTAL SYSTEM PROJECT REPORT.pdf
ONLINE VEHICLE RENTAL SYSTEM PROJECT REPORT.pdf
 
ENERGY STORAGE DEVICES INTRODUCTION UNIT-I
ENERGY STORAGE DEVICES  INTRODUCTION UNIT-IENERGY STORAGE DEVICES  INTRODUCTION UNIT-I
ENERGY STORAGE DEVICES INTRODUCTION UNIT-I
 

Concurrency

  • 2. Agenda ● Some JVM knowledge ● Some basic concurrency concepts ● Some personal design suggestions
  • 3. Reference ● Some JVM knowledge - Java Concurrency in Practice ● Some basic concurrency concepts - Clean Code ● Some personal design suggestions
  • 4. Won’t be mentioned ● Practices to write concurrency program ● Patterns or anti-patterns in concurrency programming ● java.util.concurrent API example ● More detail in JVM They are big topics, maybe other Tech Talks. Or we can discuss with each other for interesting.
  • 5. Target audience ● Java developers who don’t know about concurrency ● Your code will run in multi-threaded environment
  • 6. Goal ● Be aware concurrency issue while programming ● Get some material or direction to study ● Let concurrency become a famous discussion between developers
  • 7. Why Concurrency ● CPU core size maybe 8-24 But there are thousands of threads… Currency must be a very important topic in development
  • 8. What is Concurrency? … This[Concurrency] means that even if the concurrent units of the program, algorithm, or problem are executed out-of-order or in partial order, the final outcome will remain the same… ~ WIKI
  • 9. What is “out-of-order”? In this paradigm [out-of-order-execution], a processor executes instructions in an order governed by the availability of input data, rather than by their original order in a program. In doing so, the processor can avoid being idle while waiting for the preceding instruction to complete to retrieve data for the next instruction ~ WIKI
  • 10. Is it related to my java code? Java code Reordered (Maybe) public int test() { int a = 1; a++; int b = 2; b++; return a+b; } public int test() { int a = 1, b = 2; // load var and data a++; // read and write b++; // read and write return a+b; // result is same after reordered } // Should be faster in a smater way… // Most important is we don’t know the order
  • 11. It seems not my business, should I care? First of all, the program will be loaded to a CPU core to run. CPU Core1 CPU Core2 Program in memory Program Program
  • 12. It seems not my business, should I care? CPU only care about to run program faster and correct. CPU don’t care about the status between threads without special command. What will happen in main thread? Thread A Thread B a=1; x=b; b=1; y=a;Main Thread 1. Start thread A 2. Start thread B 3. Get x and y int x=0,y=0; int a=0,b=0;
  • 13. It seems not my business, should I care? We can’t expect the results are always identical… Thread A Execution 1. a=1,x=0 2. a=1,x=1 Thread B Execution 1. b=1,y=0 2. b=1,y=1 int x=0,y=0; int a=0,b=0; a=1; x=b; b=1; y=a;Main Thread 1. Start thread A 2. Start thread B 3. Read x and y Result: 1. x=0,y=1 // B finished 2. x=1,y=0 // A finished 3. x=1,y=1 // Both finished 4. x=0,y=0 // Both not finished // and reordered int x=0,y=0; int a=0,b=0;
  • 14. It seems not my business, should I care? ● Result 1: x=0, y=1 ThreadA ThreadB Main Thread Code a=1; x=b; b=1; y=a; Start thread A Start thread B Read x and y Cycle 1 a=1; b=1; Cycle 2 y=a; Get x=0, y=1 Cycle 3 x=b;
  • 15. It seems not my business, should I care? ● Result 2: x=1, y=0 ThreadA ThreadB Main Thread Code a=1; x=b; b=1; y=a; Start thread A Start thread B Read x and y Cycle 1 a=1; b=1; Cycle 2 x=b; Get x=1, y=0 Cycle 3 y=a;
  • 16. It seems not my business, should I care? ● Result 3: x=1, y=1 ThreadA ThreadB Main Thread Code a=1; x=b; b=1; y=a; Start thread A Start thread B Read x and y Cycle 1 a=1; b=1; Cycle 2 x=b; y=a; Cycle 3 Get x=1, y=1
  • 17. It seems not my business, should I care? ● Result 4: x=0, y=0 ThreadA ThreadB Main Thread Code a=1; x=b; b=1; y=a; Start thread A Start thread B Read x and y Cycle 1 x=b; y=a; Cycle 2 a=1; b=1; Cycle 3 Get x=0, y=0
  • 18. Another example more close to our daily work class APManager { ArrayList<AP> aps; // online, offline AP // register, unregister AP } class APStatusListener { ArrayList<AP> aps; // set from APManager public void infiniteCheck() { while (true) { for (AP ap: aps) { if (ap.isOnline()) {...} else {...} } } } }class AP { String status; // online, offline } What problem in APStatusListener?
  • 19. Another example more close to our daily work class APManager { ArrayList<AP> aps; // online, offline AP // register, unregister AP } class APStatusListener { ArrayList<AP> aps; // set from APManager public void infiniteCheck() { while (true) { for (AP ap: aps) { if (ap.isOnline()) {...} else {...} } } } } May be always initial data. No new AP No AP will be removed No status change class AP { String status; // online, offline }
  • 20. How to fix? class APManager { final CopyOnWriteArrayList<AP> aps; // online, offline AP, change AP.status // register, unregister AP } class APStatusListener { List<AP> aps; // set from APManager public void infiniteCheck() { while (true) { for (AP ap: aps) { if (ap.isOnline()) {...} else {...} } } } }class AP { volatile String status; // online, offline }
  • 21. How to fix? class APManager { final CopyOnWriteArrayList<AP> aps; // online, offline AP, change AP.status // register, unregister AP } class APStatusListener { List<AP> aps; // set from APManager public void infiniteCheck() { while (true) { for (AP ap: aps) { if (ap.isOnline()) {...} else {...} } } } } class AP { volatile String status; // online, offline } final => APStatusListener will see initialized aps CopyOnWriteArrayList => APStatusListener see modified element (Of course you can declare aps with List<AP> and CopyOnWriteArrayList<AP> implementation) volatile => APStatusListener see AP status change
  • 22. How it works? After JAVA 5 JVM introduce JSR-133 Java Memory Model who defined happens-before relationship and give final field a special meaning to make things happen.
  • 23. What is happens-before relationship? It’s rules guarantee by JVM, used to define program order. ● Each action in a thread happens before every subsequent action in that thread. ● An unlock on a monitor happens before every subsequent lock on that monitor. ● A write to a volatile field happens before every subsequent read of that volatile. ● A call to start() on a thread happens before any actions in the started thread. ● All actions in a thread happen before any other thread successfully returns from a join() on that thread. ● If an action a happens before an action b, and b happens before an action c, then a happens before c.
  • 24. How about final field? JSR-133 defined that after an object was properly constructed, a final field assigned in constructor can be visible by all threads.
  • 25. How about final field in the JVM before Java 5? The final field in old JVM has no difference between general field except it need assign value in constructor. Developer still need to use synchronization to ensure the variable modification visible in all threads.
  • 26. What is CopyOnWriteArrayList? It's a thread safe ArrayList implementation in java.util.concurrent package, introduced from Java 5. It also leverage Java Memory Model to make sure modifications visible to other threads.
  • 29. Magic in ReentrantLock setState write value to volatile state, make sure all lock holder get latest state. Thus don’t need synchronized tryRelease
  • 30. volatile is not a siliver buttlet ● It can’t perform N++ operation with just N++.. volatile int n = 0; Thread 1 Thread 2 n++; n = 0, n = n+1 n = 0, n = n+1 Get n n = 1; n = 1
  • 31. How to perform N++ atomically? ● syncrhonized (monitor lock) int n = 0; // don’t need volatile if using synchronized public synchronized int incremenetAndGet() { n++; }
  • 32. How to perform N++ atomically? ● AtomicInteger#incrementAndGet
  • 33. How to perform N++ atomically? Unsafe.java
  • 34. How to perform N++ atomically? unsafe.cpp
  • 35. How to perform N++ atomically? LLVM #cmpxchg
  • 36. JVM knowledge summary ● reorder ● JSR-133 Java Memory Model ● happens-before relationship ● volatile for visibility ● LLVM memory barrier
  • 37. Basic concurrency concepts ● Definitions ○ Bound Resource ○ Mutual Execution ○ Starvation ○ Deadlock ○ Livelock ● Execution Models ○ Producer-Consumer ○ Writers-Readers ○ Dining-Philosophers
  • 38. Bound Resource Resources of a fixed size or number used in a concurrent environment.
  • 39. Mutual Execution Only one thread can access shared data or a shared resource at a time. Such as Java Synchronization
  • 40. Starvation One thread or a group of threads is prohibited from proceeding for an excessively long time or forever. Ex. Lower priority tasks may encounter Starvation. (Because always higher priority task)
  • 41. Deadlock Two or more threads wait for each other to finish. Each thread has a resource that the other thread requires and neither can finish until gets the other resource. Thread 1 Thread 2 Lock1 Lock2 acquire acquire waitwait Thread1 and Thread2 all need acquire Lock1 and Lock2 to do something. But the order is different so deadlock may happen. Note: Acquire and release locks in the same order can prevent deadlock
  • 42. Livelock A thread often acts in response to the action of another thread. If the other thread's action is also a response to the action of another thread, then livelock may result. Example: A boy and girl are dating in a restaurant. They are shy. They order a cup of drink to share. They try to take the drink but find another also want to drink so stop back. And again, and again, and again...until someone stop...or never...
  • 43. Execution Models ● Producer-Consumer ● Writers-Readers ● Dining-Philosophers
  • 44. Producer-Consumer ● Behavior a. Producer threads create work in a queue b. Consumer threads acquire work from queue and complete it ● Challenge a. Queue is the bound resource b. Producer need wait for free space in queue c. Consumer need wait until something in queue d. Producer may need notify Consumer queue is not empty e. Consumer may need notify Producer queue is empty
  • 45. Producer-Consumer ● Benefit a. Observer pattern, used to decouple model or process b. Easy to tune thread pool because producer and consumer have different resource occupation style ● Risk a. Needless decoupling cause duplication and hard to define model responsibility b. Too many decoupliing cause system too complex and waste resource ● Example a. Message Queue b. Thread Pool
  • 46. Readers-Writers ● Behavior ○ A shared resource serves as a source of information for Readers ○ Writers may update the shared resource ● Challenge ○ Throughput issue ■ Writer prevent reader read while updating ■ Reader prevent writer write while reading ○ Stale information problem while reader allowing writer update
  • 47. Readers-Writers ● Risk ○ Try to prevent read stale data ■ System load heavy ■ Useless lock ■ User don’t care ● Example ○ Cassandra ○ ElasticSearch
  • 48. Dining Philosophers ● Behavior ○ Number of philosophers sitting around a circle table ○ Philosophers can’t eat when they are thinking ○ Philosophers need pick up forks on either side of them and eat ○ Philosophers can’t eat unless they hold two forks ● Challenge ○ If the philosopher to his right or left is already using one of the forks he need, he must wait until that philosopher finish eating and put forks back down. ○ Deadlock when everyone pick one fork and wait for another finish ○ Livelock when everyone put forks back down and try again later
  • 49. Dining Philosophers ● Challenge ○ Throughput: A philosopher need to wait other finish, although he is not thinking ● Example ○ Programmatically ■ Need hold 2 or more locks to do something ● How to solve ○ A waiter track every forks status, philosopher need ask waiter and pick up forks ○ Prioritize forks, philosophers need follow the priority to pick up/put back forks ○ Philosopher who has no fork get a ticket, use a ticket to replace 2 forks to eat. Finished philosopher give forks to who has a ticket.
  • 50. Personal design suggestion Define the model you are designing is Single-source-of-truth or not. ● Better to use Singleton when it’s Single-source-of-truth. This model must be thread safe ● Build immutable object if it’s the copy of Single-source-of-truth. ● Be aware the copy of Single-source-of-truth maybe stale ● Single Responsibility Principle is basic
  • 51. Example Seat it self (Single-source-of-truth)Seat list 1 Seat list 2 Manager1 Manager2 Employee1 Employee2 We are going to change seats because of re-org. Managers ask some people for change requirements..
  • 52. Example Seat it self (Single-source-of-truth) Seat list 1 Seat list 2 Manager1 Manager2 Employee1 Employee2 Singleton naturally Maybe stale. Need compare and swap We are going to change seats because of re-org. Managers ask some people for change requirements..
  • 53. Study Materials ● Java Concurrency in Practice ● Clean Code ● Effective Java ● JSR-133 Java Memory Model ● JSR-133 FAQ ● LLVM Atomics