SlideShare a Scribd company logo
Java Concurrency
Starter Kit
What we cover in this ebook ?
1. multithreading
2. thread creation
3. thread system dependency
4. thread states
5. yielding vs spleeping
6. runnable interface
7. Interrupted Exception
8. local variables
9. volatile variables
10. volatile thread safety
11. thread interruption
12. join method
13. synchronization
14. thread interference
15. memory consistency errors
16. synchronized method
1/16 Explain multithreading in Java ?
Threads in Java appear to run concurrently, so it
provides simulation for simultaneous activities.
The processor runs each thread for a short time and
switches among the threads to simulate
sim-ultaneous execution (context-switching)
and it make appears that each thread has its own
processor.
By using this feature,users can make it appear as if
multiple tasks are occurring simultaneously when, in
fact, each is running for only a brief time before the
context is switched to the next thread.
We can do other things while waiting for slow I/O
operations -> better interaction with users
2/16 What are the possibilities for Thread creation ?
Each thread is associated with an instance of
the class Thread.
- direct control of thread creation and
management, simply instantiate the
Thread each time the application needs
to initiate an asynchronous task
To avoid need to control the thread
management, easiest is to pass the thread to a
thread executor
It is usually the operating System, which decides which thread to run when and for how much time.
But there is no guarantee when it will be run and for how long.
3/16 Are Threads in Java System dependent ?
4/16 What are a Thread states ?
When a Thread is created ( Thread t = new Thread( ...);,
it is put in [New] state
Once it is started ( t.start(); ), it is in [Runnable] (but not
running) state.
JVM only looks at threads in [Runnable] state to
schedule them for execution. When the JVM executes a
Thread, it is said to be in [Running] state.
Thread acquires the intrinsic lock when it enters a
synchronized method. Thread inside the synchronized
method is set as the owner of the lock and is in
[RUNNABLE] state.
Any thread that attempts to enter the locked method
becomes [BLOCKED].
5/16 Difference between Yielding and Sleeping ?
yield method - it returns Thread to the ready state.
sleep method - it returns Thread to the waiting state.
6/16 Why do we need Thread class even in case we
execute thread using runnable interface ?
Thread class holds the definition of start method
( This is the method that starts execution of new thread and then calls run
method within the scope of new thread ).
Interfaces don't hold any definition and so does runnable.
So it makes it necessary the usage of Thread class , whatever implementation you choose.
When your class extends the thread class, it carries the definition of start method from parent Thread
class onto itself and hence new yourClass.start() helps starting a new thread and then executing run
method in that new thread scope.
When you implement runnable interface , you are just making it sure to the JVM that you have
implemented the required method ( run() ) which the Thread start method will look for upon executing
start method.
7/16 How to handle InterruptedException
(unchecked exception) properly ?
InterruptedException is thrown by Thread.interrupt()
Example: if your Runnable objects are executed using an ExecutorService and shutdownNow() is called
on the service
Use the InterruptedException to exit whatever looping you're doing, or you can catch the exception and
restore the interrupt flag so that the next check of the interrupt flag (using
Thread.currentThread().isInterrupted()) can see that the thread has been interrupted:
8/16 Are local objects and variables thread safe ?
Local variables are stored in each thread's own stack , so they are:
- never shared between threads.
- thread safe.
But Local object references :
- the reference itself is not shared among threads , stored on thread stack
- the object itself is stored on a shared heap
If an object created locally never escapes the method it was created in -> it is thread safe.
In fact you can also pass it on to other methods and objects as long as none of these methods
or objects make the passed object available to other threads.
9/16 What is Volatile variable ?
Reads operation and write operation (separately) are atomic for :
- reference variables and for most primitive variables (all types except long and
double).
- all variables declared volatile (including long and double variables).
9/16 What are Volatile variable ?
In case one thread reads and writes the value of a volatile variable,
and other threads only read the variable, -> then the reading threads
are guaranteed to see the latest value written to the volatile variable. (
Without making the variable volatile, this would not be guaranteed)
The value of volatile variable will never be cached thread-locally:
all reads and writes will go straight to "main memory" , which is slower
vs cpu cache
If two threads are both reading and writing to a shared variable, then
using the volatile keyword for that is not enough (no lock). You need
to use synchronization in that case to guarantee that the reading and
writing of the variable is atomic.
10/16 Is volatile variable thread safe ?
Accessing a volatile variable never holds a lock, so it is not suitable for cases where we want to
read-update-write as an one atomic operation
Volatile fixes thread-visibility problem but it does not deal with atomicity.
Volatile will prevent compiler to reorder the instruction which involves write and subsequent
read of a volatile variable.
But k++ is not a single machine instruction rather it is three machine instructions:
- copy the value to register
- increment it
- place it back
So even though you declare variable to volatile it will not make this operation atomic that
means another thread can see a intermediate result.
11/16 What is thread interruption ?
The interrupt mechanism is implemented using an internal flag
known as the interrupt status.
Invoking Thread.interrupt() sets this flag.
When a thread is interrupted by invoking the static method
Thread.interrupted() -> interrupts and makes interrupt status
cleared.
The non-static isInterrupted() method, which is used by one thread
to query the interrupt status of another, does not change the
interrupt status flag.
By convention, any method that exits by throwing an
InterruptedException clears interrupt status when it does so.
12/16 What is thread join method ?
The join method allows one thread to wait for the
completion of another.
If t is a Thread object whose thread is currently
executing,
t.join();
t.join(1000);
//wait 1 sec, after 1 sec continue on
causes the current thread to pause execution until t
thread terminates.
13/16 Why do we need to synchronize code ?
Synchronization can introduce thread contention, which occurs when
two or more threads try to access the same resource simultaneously
and cause the Java runtime to execute one or more threads more
slowly, or even suspend their execution.
Starvation and livelock are forms of thread contention.
synchronized keyword can be applied only to a
- non-abstract method
- method block of code
- static or instance initialization block
When two operations, running in different threads, but acting on the same data, interleave.
This means that the two operations consist of multiple steps, and the sequences of steps overlap.
If two threads call the increment() method on the same Counter instance simultaneously,
We could see either : 00 or 01 Since access to count is not synchronized, there is no guarantee that
changes made by thread 1 will even be visible to thread 2.
Thus, both the threads may print 0 and increment it to 1 even if they run one after the other.
//Decomposition of count++
1 Retrieve the current value of count.
2 Increment the retrieved value by 1.
3 Store the incremented value back in count.
14/16 What is thread Interference ?
15/16 What is the memory consistency error ?
Can occur when different threads have inconsistent views of what should be the same data.
All that is needed is a strategy for avoiding them = create happens-before relationships,
One of them is synchronization
16/16 How does Synchronized method work ?
- A synchronized method is similar to any other method except that only one thread per each
instance can be in this method at once.
- A thread can enter a synchronized method only after it acquires a lock.
Note that acquiring a lock is necessary only for entering synchronized methods,
there is no need to get a lock (of the object on which it is trying to execute the method)
for entering a normal method.
- This means if Thread T1 is in a synchronized method sM1() then
Thread T2 can enter any other non-synchronized method.
But T2 CANNOT enter sM1() or any other synchronized method because there is only one
lock with one object which is already taken by T1, so if they want to access they are blocked.
16/16 When do we use Synchronized method ?
When no thread contention -> can use synchronized for performance
When any contention -> use StampedLock as it is better in throughput
16/16 What happens when the thread wants to call 2
synchronized methods ?
A thread can re-acquire a lock. This means once it enters a synchronized method sm1() it can call
any other synchronized method within it. -> no deadlock.
When a synchronized method exits,
it automatically establishes a happens-before relationship with any subsequent invocation of a
synchronized method for the same object.
This guarantees that changes to the state of the object are visible to all threads.
Ad

More Related Content

What's hot (20)

201 core java interview questions oo ps interview questions - javatpoint
201 core java interview questions   oo ps interview questions - javatpoint201 core java interview questions   oo ps interview questions - javatpoint
201 core java interview questions oo ps interview questions - javatpoint
ravi tyagi
 
Dev labs alliance top 20 basic java interview question for sdet
Dev labs alliance top 20 basic java interview question for sdetDev labs alliance top 20 basic java interview question for sdet
Dev labs alliance top 20 basic java interview question for sdet
devlabsalliance
 
Java Interview Questions by NageswaraRao
Java Interview Questions by NageswaraRaoJava Interview Questions by NageswaraRao
Java Interview Questions by NageswaraRao
JavabynataraJ
 
37 Java Interview Questions
37 Java Interview Questions37 Java Interview Questions
37 Java Interview Questions
Arc & Codementor
 
8 most expected java interview questions
8 most expected java interview questions8 most expected java interview questions
8 most expected java interview questions
Poonam Kherde
 
Extreme Interview Questions
Extreme Interview QuestionsExtreme Interview Questions
Extreme Interview Questions
Ehtisham Ali
 
Java object oriented programming - OOPS
Java object oriented programming - OOPSJava object oriented programming - OOPS
Java object oriented programming - OOPS
rithustutorials
 
Java interview questions 2
Java interview questions 2Java interview questions 2
Java interview questions 2
Sherihan Anver
 
Basic java important interview questions and answers to secure a job
Basic java important interview questions and answers to secure a jobBasic java important interview questions and answers to secure a job
Basic java important interview questions and answers to secure a job
Garuda Trainings
 
Core Java interview questions-ppt
Core Java interview questions-pptCore Java interview questions-ppt
Core Java interview questions-ppt
Mayank Kumar
 
Java interview questions
Java interview questionsJava interview questions
Java interview questions
G C Reddy Technologies
 
20 most important java programming interview questions
20 most important java programming interview questions20 most important java programming interview questions
20 most important java programming interview questions
Gradeup
 
Interview Questions and Answers for Java
Interview Questions and Answers for JavaInterview Questions and Answers for Java
Interview Questions and Answers for Java
Garuda Trainings
 
50+ java interview questions
50+ java interview questions50+ java interview questions
50+ java interview questions
SynergisticMedia
 
Most Asked Java Interview Question and Answer
Most Asked Java Interview Question and AnswerMost Asked Java Interview Question and Answer
Most Asked Java Interview Question and Answer
TOPS Technologies
 
Questions of java
Questions of javaQuestions of java
Questions of java
Waseem Wasi
 
JAVA VIVA QUESTIONS_CODERS LODGE.pdf
JAVA VIVA QUESTIONS_CODERS LODGE.pdfJAVA VIVA QUESTIONS_CODERS LODGE.pdf
JAVA VIVA QUESTIONS_CODERS LODGE.pdf
nofakeNews
 
Java questions for interview
Java questions for interviewJava questions for interview
Java questions for interview
Kuntal Bhowmick
 
Java questions for viva
Java questions for vivaJava questions for viva
Java questions for viva
Vipul Naik
 
Java Faqs useful for freshers and experienced
Java Faqs useful for freshers and experiencedJava Faqs useful for freshers and experienced
Java Faqs useful for freshers and experienced
yearninginjava
 
201 core java interview questions oo ps interview questions - javatpoint
201 core java interview questions   oo ps interview questions - javatpoint201 core java interview questions   oo ps interview questions - javatpoint
201 core java interview questions oo ps interview questions - javatpoint
ravi tyagi
 
Dev labs alliance top 20 basic java interview question for sdet
Dev labs alliance top 20 basic java interview question for sdetDev labs alliance top 20 basic java interview question for sdet
Dev labs alliance top 20 basic java interview question for sdet
devlabsalliance
 
Java Interview Questions by NageswaraRao
Java Interview Questions by NageswaraRaoJava Interview Questions by NageswaraRao
Java Interview Questions by NageswaraRao
JavabynataraJ
 
37 Java Interview Questions
37 Java Interview Questions37 Java Interview Questions
37 Java Interview Questions
Arc & Codementor
 
8 most expected java interview questions
8 most expected java interview questions8 most expected java interview questions
8 most expected java interview questions
Poonam Kherde
 
Extreme Interview Questions
Extreme Interview QuestionsExtreme Interview Questions
Extreme Interview Questions
Ehtisham Ali
 
Java object oriented programming - OOPS
Java object oriented programming - OOPSJava object oriented programming - OOPS
Java object oriented programming - OOPS
rithustutorials
 
Java interview questions 2
Java interview questions 2Java interview questions 2
Java interview questions 2
Sherihan Anver
 
Basic java important interview questions and answers to secure a job
Basic java important interview questions and answers to secure a jobBasic java important interview questions and answers to secure a job
Basic java important interview questions and answers to secure a job
Garuda Trainings
 
Core Java interview questions-ppt
Core Java interview questions-pptCore Java interview questions-ppt
Core Java interview questions-ppt
Mayank Kumar
 
20 most important java programming interview questions
20 most important java programming interview questions20 most important java programming interview questions
20 most important java programming interview questions
Gradeup
 
Interview Questions and Answers for Java
Interview Questions and Answers for JavaInterview Questions and Answers for Java
Interview Questions and Answers for Java
Garuda Trainings
 
50+ java interview questions
50+ java interview questions50+ java interview questions
50+ java interview questions
SynergisticMedia
 
Most Asked Java Interview Question and Answer
Most Asked Java Interview Question and AnswerMost Asked Java Interview Question and Answer
Most Asked Java Interview Question and Answer
TOPS Technologies
 
Questions of java
Questions of javaQuestions of java
Questions of java
Waseem Wasi
 
JAVA VIVA QUESTIONS_CODERS LODGE.pdf
JAVA VIVA QUESTIONS_CODERS LODGE.pdfJAVA VIVA QUESTIONS_CODERS LODGE.pdf
JAVA VIVA QUESTIONS_CODERS LODGE.pdf
nofakeNews
 
Java questions for interview
Java questions for interviewJava questions for interview
Java questions for interview
Kuntal Bhowmick
 
Java questions for viva
Java questions for vivaJava questions for viva
Java questions for viva
Vipul Naik
 
Java Faqs useful for freshers and experienced
Java Faqs useful for freshers and experiencedJava Faqs useful for freshers and experienced
Java Faqs useful for freshers and experienced
yearninginjava
 

Similar to Java Concurrency Starter Kit (20)

Java And Multithreading
Java And MultithreadingJava And Multithreading
Java And Multithreading
Shraddha
 
Thread priorities in java
Thread priorities in javaThread priorities in java
Thread priorities in java
Ducat India
 
Java Multithreading and Concurrency
Java Multithreading and ConcurrencyJava Multithreading and Concurrency
Java Multithreading and Concurrency
Rajesh Ananda Kumar
 
Thread
ThreadThread
Thread
Juhi Kumari
 
Programming - Java-Threads-and-Synchronization.ppt
Programming - Java-Threads-and-Synchronization.pptProgramming - Java-Threads-and-Synchronization.ppt
Programming - Java-Threads-and-Synchronization.ppt
smychung
 
Multi threading
Multi threadingMulti threading
Multi threading
gndu
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
Kavitha713564
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
Kavitha713564
 
Multithreading
MultithreadingMultithreading
Multithreading
sagsharma
 
Java Threads
Java ThreadsJava Threads
Java Threads
Hamid Ghorbani
 
Multithreading Introduction and Lifecyle of thread
Multithreading Introduction and Lifecyle of threadMultithreading Introduction and Lifecyle of thread
Multithreading Introduction and Lifecyle of thread
Kartik Dube
 
Multi-threaded Programming in JAVA
Multi-threaded Programming in JAVAMulti-threaded Programming in JAVA
Multi-threaded Programming in JAVA
Vikram Kalyani
 
Md09 multithreading
Md09 multithreadingMd09 multithreading
Md09 multithreading
Rakesh Madugula
 
MSBTE Computer Engineering JPR java. multi. threading.pptx
MSBTE Computer Engineering  JPR java.  multi. threading.pptxMSBTE Computer Engineering  JPR java.  multi. threading.pptx
MSBTE Computer Engineering JPR java. multi. threading.pptx
kunalgaikwad1705
 
Concurrency
ConcurrencyConcurrency
Concurrency
Sandeep Chawla
 
84694646456445645645645665656465464sdd.pptx
84694646456445645645645665656465464sdd.pptx84694646456445645645645665656465464sdd.pptx
84694646456445645645645665656465464sdd.pptx
mhp821023
 
Multithreading and concurrency in android
Multithreading and concurrency in androidMultithreading and concurrency in android
Multithreading and concurrency in android
Rakesh Jha
 
Slide 7 Thread-1.pptx
Slide 7 Thread-1.pptxSlide 7 Thread-1.pptx
Slide 7 Thread-1.pptx
ajmalhamidi1380
 
Threads in Java
Threads in JavaThreads in Java
Threads in Java
HarshaDokula
 
Linux Internals - Part III
Linux Internals - Part IIILinux Internals - Part III
Linux Internals - Part III
Emertxe Information Technologies Pvt Ltd
 
Java And Multithreading
Java And MultithreadingJava And Multithreading
Java And Multithreading
Shraddha
 
Thread priorities in java
Thread priorities in javaThread priorities in java
Thread priorities in java
Ducat India
 
Java Multithreading and Concurrency
Java Multithreading and ConcurrencyJava Multithreading and Concurrency
Java Multithreading and Concurrency
Rajesh Ananda Kumar
 
Programming - Java-Threads-and-Synchronization.ppt
Programming - Java-Threads-and-Synchronization.pptProgramming - Java-Threads-and-Synchronization.ppt
Programming - Java-Threads-and-Synchronization.ppt
smychung
 
Multi threading
Multi threadingMulti threading
Multi threading
gndu
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
Kavitha713564
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
Kavitha713564
 
Multithreading
MultithreadingMultithreading
Multithreading
sagsharma
 
Multithreading Introduction and Lifecyle of thread
Multithreading Introduction and Lifecyle of threadMultithreading Introduction and Lifecyle of thread
Multithreading Introduction and Lifecyle of thread
Kartik Dube
 
Multi-threaded Programming in JAVA
Multi-threaded Programming in JAVAMulti-threaded Programming in JAVA
Multi-threaded Programming in JAVA
Vikram Kalyani
 
MSBTE Computer Engineering JPR java. multi. threading.pptx
MSBTE Computer Engineering  JPR java.  multi. threading.pptxMSBTE Computer Engineering  JPR java.  multi. threading.pptx
MSBTE Computer Engineering JPR java. multi. threading.pptx
kunalgaikwad1705
 
84694646456445645645645665656465464sdd.pptx
84694646456445645645645665656465464sdd.pptx84694646456445645645645665656465464sdd.pptx
84694646456445645645645665656465464sdd.pptx
mhp821023
 
Multithreading and concurrency in android
Multithreading and concurrency in androidMultithreading and concurrency in android
Multithreading and concurrency in android
Rakesh Jha
 
Ad

More from Mark Papis (8)

9 crucial Java Design Principles you cannot miss
9 crucial Java Design Principles you cannot miss9 crucial Java Design Principles you cannot miss
9 crucial Java Design Principles you cannot miss
Mark Papis
 
Java Streams Interview short reminder with examples
Java Streams Interview short reminder with examplesJava Streams Interview short reminder with examples
Java Streams Interview short reminder with examples
Mark Papis
 
Java vs Web security Cheat Sheet
Java vs Web security Cheat SheetJava vs Web security Cheat Sheet
Java vs Web security Cheat Sheet
Mark Papis
 
Java technical stack Cheat Sheet
Java technical stack Cheat SheetJava technical stack Cheat Sheet
Java technical stack Cheat Sheet
Mark Papis
 
Spring cheat sheet
Spring cheat sheetSpring cheat sheet
Spring cheat sheet
Mark Papis
 
Java JVM Memory Cheat Sheet
Java JVM Memory Cheat SheetJava JVM Memory Cheat Sheet
Java JVM Memory Cheat Sheet
Mark Papis
 
Java inheritance cheat sheet
Java inheritance cheat sheetJava inheritance cheat sheet
Java inheritance cheat sheet
Mark Papis
 
Java Collections comparison Cheat Sheet
Java Collections comparison Cheat SheetJava Collections comparison Cheat Sheet
Java Collections comparison Cheat Sheet
Mark Papis
 
9 crucial Java Design Principles you cannot miss
9 crucial Java Design Principles you cannot miss9 crucial Java Design Principles you cannot miss
9 crucial Java Design Principles you cannot miss
Mark Papis
 
Java Streams Interview short reminder with examples
Java Streams Interview short reminder with examplesJava Streams Interview short reminder with examples
Java Streams Interview short reminder with examples
Mark Papis
 
Java vs Web security Cheat Sheet
Java vs Web security Cheat SheetJava vs Web security Cheat Sheet
Java vs Web security Cheat Sheet
Mark Papis
 
Java technical stack Cheat Sheet
Java technical stack Cheat SheetJava technical stack Cheat Sheet
Java technical stack Cheat Sheet
Mark Papis
 
Spring cheat sheet
Spring cheat sheetSpring cheat sheet
Spring cheat sheet
Mark Papis
 
Java JVM Memory Cheat Sheet
Java JVM Memory Cheat SheetJava JVM Memory Cheat Sheet
Java JVM Memory Cheat Sheet
Mark Papis
 
Java inheritance cheat sheet
Java inheritance cheat sheetJava inheritance cheat sheet
Java inheritance cheat sheet
Mark Papis
 
Java Collections comparison Cheat Sheet
Java Collections comparison Cheat SheetJava Collections comparison Cheat Sheet
Java Collections comparison Cheat Sheet
Mark Papis
 
Ad

Recently uploaded (20)

DevOpsDays SLC - Platform Engineers are Product Managers.pptx
DevOpsDays SLC - Platform Engineers are Product Managers.pptxDevOpsDays SLC - Platform Engineers are Product Managers.pptx
DevOpsDays SLC - Platform Engineers are Product Managers.pptx
Justin Reock
 
Crazy Incentives and How They Kill Security. How Do You Turn the Wheel?
Crazy Incentives and How They Kill Security. How Do You Turn the Wheel?Crazy Incentives and How They Kill Security. How Do You Turn the Wheel?
Crazy Incentives and How They Kill Security. How Do You Turn the Wheel?
Christian Folini
 
An Overview of Salesforce Health Cloud & How is it Transforming Patient Care
An Overview of Salesforce Health Cloud & How is it Transforming Patient CareAn Overview of Salesforce Health Cloud & How is it Transforming Patient Care
An Overview of Salesforce Health Cloud & How is it Transforming Patient Care
Cyntexa
 
RTP Over QUIC: An Interesting Opportunity Or Wasted Time?
RTP Over QUIC: An Interesting Opportunity Or Wasted Time?RTP Over QUIC: An Interesting Opportunity Or Wasted Time?
RTP Over QUIC: An Interesting Opportunity Or Wasted Time?
Lorenzo Miniero
 
IT488 Wireless Sensor Networks_Information Technology
IT488 Wireless Sensor Networks_Information TechnologyIT488 Wireless Sensor Networks_Information Technology
IT488 Wireless Sensor Networks_Information Technology
SHEHABALYAMANI
 
Zilliz Cloud Monthly Technical Review: May 2025
Zilliz Cloud Monthly Technical Review: May 2025Zilliz Cloud Monthly Technical Review: May 2025
Zilliz Cloud Monthly Technical Review: May 2025
Zilliz
 
ACE Aarhus - Team'25 wrap-up presentation
ACE Aarhus - Team'25 wrap-up presentationACE Aarhus - Team'25 wrap-up presentation
ACE Aarhus - Team'25 wrap-up presentation
DanielEriksen5
 
Everything You Need to Know About Agentforce? (Put AI Agents to Work)
Everything You Need to Know About Agentforce? (Put AI Agents to Work)Everything You Need to Know About Agentforce? (Put AI Agents to Work)
Everything You Need to Know About Agentforce? (Put AI Agents to Work)
Cyntexa
 
UiPath AgentHack - Build the AI agents of tomorrow_Enablement 1.pptx
UiPath AgentHack - Build the AI agents of tomorrow_Enablement 1.pptxUiPath AgentHack - Build the AI agents of tomorrow_Enablement 1.pptx
UiPath AgentHack - Build the AI agents of tomorrow_Enablement 1.pptx
anabulhac
 
Cybersecurity Threat Vectors and Mitigation
Cybersecurity Threat Vectors and MitigationCybersecurity Threat Vectors and Mitigation
Cybersecurity Threat Vectors and Mitigation
VICTOR MAESTRE RAMIREZ
 
Who's choice? Making decisions with and about Artificial Intelligence, Keele ...
Who's choice? Making decisions with and about Artificial Intelligence, Keele ...Who's choice? Making decisions with and about Artificial Intelligence, Keele ...
Who's choice? Making decisions with and about Artificial Intelligence, Keele ...
Alan Dix
 
Limecraft Webinar - 2025.3 release, featuring Content Delivery, Graphic Conte...
Limecraft Webinar - 2025.3 release, featuring Content Delivery, Graphic Conte...Limecraft Webinar - 2025.3 release, featuring Content Delivery, Graphic Conte...
Limecraft Webinar - 2025.3 release, featuring Content Delivery, Graphic Conte...
Maarten Verwaest
 
MULTI-STAKEHOLDER CONSULTATION PROGRAM On Implementation of DNF 2.0 and Way F...
MULTI-STAKEHOLDER CONSULTATION PROGRAM On Implementation of DNF 2.0 and Way F...MULTI-STAKEHOLDER CONSULTATION PROGRAM On Implementation of DNF 2.0 and Way F...
MULTI-STAKEHOLDER CONSULTATION PROGRAM On Implementation of DNF 2.0 and Way F...
ICT Frame Magazine Pvt. Ltd.
 
React Native for Business Solutions: Building Scalable Apps for Success
React Native for Business Solutions: Building Scalable Apps for SuccessReact Native for Business Solutions: Building Scalable Apps for Success
React Native for Business Solutions: Building Scalable Apps for Success
Amelia Swank
 
Developing System Infrastructure Design Plan.pptx
Developing System Infrastructure Design Plan.pptxDeveloping System Infrastructure Design Plan.pptx
Developing System Infrastructure Design Plan.pptx
wondimagegndesta
 
Kit-Works Team Study_아직도 Dockefile.pdf_김성호
Kit-Works Team Study_아직도 Dockefile.pdf_김성호Kit-Works Team Study_아직도 Dockefile.pdf_김성호
Kit-Works Team Study_아직도 Dockefile.pdf_김성호
Wonjun Hwang
 
machines-for-woodworking-shops-en-compressed.pdf
machines-for-woodworking-shops-en-compressed.pdfmachines-for-woodworking-shops-en-compressed.pdf
machines-for-woodworking-shops-en-compressed.pdf
AmirStern2
 
Integrating FME with Python: Tips, Demos, and Best Practices for Powerful Aut...
Integrating FME with Python: Tips, Demos, and Best Practices for Powerful Aut...Integrating FME with Python: Tips, Demos, and Best Practices for Powerful Aut...
Integrating FME with Python: Tips, Demos, and Best Practices for Powerful Aut...
Safe Software
 
Top 5 Qualities to Look for in Salesforce Partners in 2025
Top 5 Qualities to Look for in Salesforce Partners in 2025Top 5 Qualities to Look for in Salesforce Partners in 2025
Top 5 Qualities to Look for in Salesforce Partners in 2025
Damco Salesforce Services
 
Why Slack Should Be Your Next Business Tool? (Tips to Make Most out of Slack)
Why Slack Should Be Your Next Business Tool? (Tips to Make Most out of Slack)Why Slack Should Be Your Next Business Tool? (Tips to Make Most out of Slack)
Why Slack Should Be Your Next Business Tool? (Tips to Make Most out of Slack)
Cyntexa
 
DevOpsDays SLC - Platform Engineers are Product Managers.pptx
DevOpsDays SLC - Platform Engineers are Product Managers.pptxDevOpsDays SLC - Platform Engineers are Product Managers.pptx
DevOpsDays SLC - Platform Engineers are Product Managers.pptx
Justin Reock
 
Crazy Incentives and How They Kill Security. How Do You Turn the Wheel?
Crazy Incentives and How They Kill Security. How Do You Turn the Wheel?Crazy Incentives and How They Kill Security. How Do You Turn the Wheel?
Crazy Incentives and How They Kill Security. How Do You Turn the Wheel?
Christian Folini
 
An Overview of Salesforce Health Cloud & How is it Transforming Patient Care
An Overview of Salesforce Health Cloud & How is it Transforming Patient CareAn Overview of Salesforce Health Cloud & How is it Transforming Patient Care
An Overview of Salesforce Health Cloud & How is it Transforming Patient Care
Cyntexa
 
RTP Over QUIC: An Interesting Opportunity Or Wasted Time?
RTP Over QUIC: An Interesting Opportunity Or Wasted Time?RTP Over QUIC: An Interesting Opportunity Or Wasted Time?
RTP Over QUIC: An Interesting Opportunity Or Wasted Time?
Lorenzo Miniero
 
IT488 Wireless Sensor Networks_Information Technology
IT488 Wireless Sensor Networks_Information TechnologyIT488 Wireless Sensor Networks_Information Technology
IT488 Wireless Sensor Networks_Information Technology
SHEHABALYAMANI
 
Zilliz Cloud Monthly Technical Review: May 2025
Zilliz Cloud Monthly Technical Review: May 2025Zilliz Cloud Monthly Technical Review: May 2025
Zilliz Cloud Monthly Technical Review: May 2025
Zilliz
 
ACE Aarhus - Team'25 wrap-up presentation
ACE Aarhus - Team'25 wrap-up presentationACE Aarhus - Team'25 wrap-up presentation
ACE Aarhus - Team'25 wrap-up presentation
DanielEriksen5
 
Everything You Need to Know About Agentforce? (Put AI Agents to Work)
Everything You Need to Know About Agentforce? (Put AI Agents to Work)Everything You Need to Know About Agentforce? (Put AI Agents to Work)
Everything You Need to Know About Agentforce? (Put AI Agents to Work)
Cyntexa
 
UiPath AgentHack - Build the AI agents of tomorrow_Enablement 1.pptx
UiPath AgentHack - Build the AI agents of tomorrow_Enablement 1.pptxUiPath AgentHack - Build the AI agents of tomorrow_Enablement 1.pptx
UiPath AgentHack - Build the AI agents of tomorrow_Enablement 1.pptx
anabulhac
 
Cybersecurity Threat Vectors and Mitigation
Cybersecurity Threat Vectors and MitigationCybersecurity Threat Vectors and Mitigation
Cybersecurity Threat Vectors and Mitigation
VICTOR MAESTRE RAMIREZ
 
Who's choice? Making decisions with and about Artificial Intelligence, Keele ...
Who's choice? Making decisions with and about Artificial Intelligence, Keele ...Who's choice? Making decisions with and about Artificial Intelligence, Keele ...
Who's choice? Making decisions with and about Artificial Intelligence, Keele ...
Alan Dix
 
Limecraft Webinar - 2025.3 release, featuring Content Delivery, Graphic Conte...
Limecraft Webinar - 2025.3 release, featuring Content Delivery, Graphic Conte...Limecraft Webinar - 2025.3 release, featuring Content Delivery, Graphic Conte...
Limecraft Webinar - 2025.3 release, featuring Content Delivery, Graphic Conte...
Maarten Verwaest
 
MULTI-STAKEHOLDER CONSULTATION PROGRAM On Implementation of DNF 2.0 and Way F...
MULTI-STAKEHOLDER CONSULTATION PROGRAM On Implementation of DNF 2.0 and Way F...MULTI-STAKEHOLDER CONSULTATION PROGRAM On Implementation of DNF 2.0 and Way F...
MULTI-STAKEHOLDER CONSULTATION PROGRAM On Implementation of DNF 2.0 and Way F...
ICT Frame Magazine Pvt. Ltd.
 
React Native for Business Solutions: Building Scalable Apps for Success
React Native for Business Solutions: Building Scalable Apps for SuccessReact Native for Business Solutions: Building Scalable Apps for Success
React Native for Business Solutions: Building Scalable Apps for Success
Amelia Swank
 
Developing System Infrastructure Design Plan.pptx
Developing System Infrastructure Design Plan.pptxDeveloping System Infrastructure Design Plan.pptx
Developing System Infrastructure Design Plan.pptx
wondimagegndesta
 
Kit-Works Team Study_아직도 Dockefile.pdf_김성호
Kit-Works Team Study_아직도 Dockefile.pdf_김성호Kit-Works Team Study_아직도 Dockefile.pdf_김성호
Kit-Works Team Study_아직도 Dockefile.pdf_김성호
Wonjun Hwang
 
machines-for-woodworking-shops-en-compressed.pdf
machines-for-woodworking-shops-en-compressed.pdfmachines-for-woodworking-shops-en-compressed.pdf
machines-for-woodworking-shops-en-compressed.pdf
AmirStern2
 
Integrating FME with Python: Tips, Demos, and Best Practices for Powerful Aut...
Integrating FME with Python: Tips, Demos, and Best Practices for Powerful Aut...Integrating FME with Python: Tips, Demos, and Best Practices for Powerful Aut...
Integrating FME with Python: Tips, Demos, and Best Practices for Powerful Aut...
Safe Software
 
Top 5 Qualities to Look for in Salesforce Partners in 2025
Top 5 Qualities to Look for in Salesforce Partners in 2025Top 5 Qualities to Look for in Salesforce Partners in 2025
Top 5 Qualities to Look for in Salesforce Partners in 2025
Damco Salesforce Services
 
Why Slack Should Be Your Next Business Tool? (Tips to Make Most out of Slack)
Why Slack Should Be Your Next Business Tool? (Tips to Make Most out of Slack)Why Slack Should Be Your Next Business Tool? (Tips to Make Most out of Slack)
Why Slack Should Be Your Next Business Tool? (Tips to Make Most out of Slack)
Cyntexa
 

Java Concurrency Starter Kit

  • 2. What we cover in this ebook ? 1. multithreading 2. thread creation 3. thread system dependency 4. thread states 5. yielding vs spleeping 6. runnable interface 7. Interrupted Exception 8. local variables 9. volatile variables 10. volatile thread safety 11. thread interruption 12. join method 13. synchronization 14. thread interference 15. memory consistency errors 16. synchronized method
  • 3. 1/16 Explain multithreading in Java ? Threads in Java appear to run concurrently, so it provides simulation for simultaneous activities. The processor runs each thread for a short time and switches among the threads to simulate sim-ultaneous execution (context-switching) and it make appears that each thread has its own processor. By using this feature,users can make it appear as if multiple tasks are occurring simultaneously when, in fact, each is running for only a brief time before the context is switched to the next thread. We can do other things while waiting for slow I/O operations -> better interaction with users
  • 4. 2/16 What are the possibilities for Thread creation ? Each thread is associated with an instance of the class Thread. - direct control of thread creation and management, simply instantiate the Thread each time the application needs to initiate an asynchronous task To avoid need to control the thread management, easiest is to pass the thread to a thread executor
  • 5. It is usually the operating System, which decides which thread to run when and for how much time. But there is no guarantee when it will be run and for how long. 3/16 Are Threads in Java System dependent ?
  • 6. 4/16 What are a Thread states ? When a Thread is created ( Thread t = new Thread( ...);, it is put in [New] state Once it is started ( t.start(); ), it is in [Runnable] (but not running) state. JVM only looks at threads in [Runnable] state to schedule them for execution. When the JVM executes a Thread, it is said to be in [Running] state. Thread acquires the intrinsic lock when it enters a synchronized method. Thread inside the synchronized method is set as the owner of the lock and is in [RUNNABLE] state. Any thread that attempts to enter the locked method becomes [BLOCKED].
  • 7. 5/16 Difference between Yielding and Sleeping ? yield method - it returns Thread to the ready state. sleep method - it returns Thread to the waiting state.
  • 8. 6/16 Why do we need Thread class even in case we execute thread using runnable interface ? Thread class holds the definition of start method ( This is the method that starts execution of new thread and then calls run method within the scope of new thread ). Interfaces don't hold any definition and so does runnable. So it makes it necessary the usage of Thread class , whatever implementation you choose. When your class extends the thread class, it carries the definition of start method from parent Thread class onto itself and hence new yourClass.start() helps starting a new thread and then executing run method in that new thread scope. When you implement runnable interface , you are just making it sure to the JVM that you have implemented the required method ( run() ) which the Thread start method will look for upon executing start method.
  • 9. 7/16 How to handle InterruptedException (unchecked exception) properly ? InterruptedException is thrown by Thread.interrupt() Example: if your Runnable objects are executed using an ExecutorService and shutdownNow() is called on the service Use the InterruptedException to exit whatever looping you're doing, or you can catch the exception and restore the interrupt flag so that the next check of the interrupt flag (using Thread.currentThread().isInterrupted()) can see that the thread has been interrupted:
  • 10. 8/16 Are local objects and variables thread safe ? Local variables are stored in each thread's own stack , so they are: - never shared between threads. - thread safe. But Local object references : - the reference itself is not shared among threads , stored on thread stack - the object itself is stored on a shared heap If an object created locally never escapes the method it was created in -> it is thread safe. In fact you can also pass it on to other methods and objects as long as none of these methods or objects make the passed object available to other threads.
  • 11. 9/16 What is Volatile variable ? Reads operation and write operation (separately) are atomic for : - reference variables and for most primitive variables (all types except long and double). - all variables declared volatile (including long and double variables).
  • 12. 9/16 What are Volatile variable ? In case one thread reads and writes the value of a volatile variable, and other threads only read the variable, -> then the reading threads are guaranteed to see the latest value written to the volatile variable. ( Without making the variable volatile, this would not be guaranteed) The value of volatile variable will never be cached thread-locally: all reads and writes will go straight to "main memory" , which is slower vs cpu cache If two threads are both reading and writing to a shared variable, then using the volatile keyword for that is not enough (no lock). You need to use synchronization in that case to guarantee that the reading and writing of the variable is atomic.
  • 13. 10/16 Is volatile variable thread safe ? Accessing a volatile variable never holds a lock, so it is not suitable for cases where we want to read-update-write as an one atomic operation Volatile fixes thread-visibility problem but it does not deal with atomicity. Volatile will prevent compiler to reorder the instruction which involves write and subsequent read of a volatile variable. But k++ is not a single machine instruction rather it is three machine instructions: - copy the value to register - increment it - place it back So even though you declare variable to volatile it will not make this operation atomic that means another thread can see a intermediate result.
  • 14. 11/16 What is thread interruption ? The interrupt mechanism is implemented using an internal flag known as the interrupt status. Invoking Thread.interrupt() sets this flag. When a thread is interrupted by invoking the static method Thread.interrupted() -> interrupts and makes interrupt status cleared. The non-static isInterrupted() method, which is used by one thread to query the interrupt status of another, does not change the interrupt status flag. By convention, any method that exits by throwing an InterruptedException clears interrupt status when it does so.
  • 15. 12/16 What is thread join method ? The join method allows one thread to wait for the completion of another. If t is a Thread object whose thread is currently executing, t.join(); t.join(1000); //wait 1 sec, after 1 sec continue on causes the current thread to pause execution until t thread terminates.
  • 16. 13/16 Why do we need to synchronize code ? Synchronization can introduce thread contention, which occurs when two or more threads try to access the same resource simultaneously and cause the Java runtime to execute one or more threads more slowly, or even suspend their execution. Starvation and livelock are forms of thread contention. synchronized keyword can be applied only to a - non-abstract method - method block of code - static or instance initialization block
  • 17. When two operations, running in different threads, but acting on the same data, interleave. This means that the two operations consist of multiple steps, and the sequences of steps overlap. If two threads call the increment() method on the same Counter instance simultaneously, We could see either : 00 or 01 Since access to count is not synchronized, there is no guarantee that changes made by thread 1 will even be visible to thread 2. Thus, both the threads may print 0 and increment it to 1 even if they run one after the other. //Decomposition of count++ 1 Retrieve the current value of count. 2 Increment the retrieved value by 1. 3 Store the incremented value back in count. 14/16 What is thread Interference ?
  • 18. 15/16 What is the memory consistency error ? Can occur when different threads have inconsistent views of what should be the same data. All that is needed is a strategy for avoiding them = create happens-before relationships, One of them is synchronization
  • 19. 16/16 How does Synchronized method work ? - A synchronized method is similar to any other method except that only one thread per each instance can be in this method at once. - A thread can enter a synchronized method only after it acquires a lock. Note that acquiring a lock is necessary only for entering synchronized methods, there is no need to get a lock (of the object on which it is trying to execute the method) for entering a normal method. - This means if Thread T1 is in a synchronized method sM1() then Thread T2 can enter any other non-synchronized method. But T2 CANNOT enter sM1() or any other synchronized method because there is only one lock with one object which is already taken by T1, so if they want to access they are blocked.
  • 20. 16/16 When do we use Synchronized method ? When no thread contention -> can use synchronized for performance When any contention -> use StampedLock as it is better in throughput
  • 21. 16/16 What happens when the thread wants to call 2 synchronized methods ? A thread can re-acquire a lock. This means once it enters a synchronized method sm1() it can call any other synchronized method within it. -> no deadlock. When a synchronized method exits, it automatically establishes a happens-before relationship with any subsequent invocation of a synchronized method for the same object. This guarantees that changes to the state of the object are visible to all threads.
  翻译: