SlideShare a Scribd company logo
Concurrency
Java Threads & Synchronization
Omindra Kumar Rana
Senior Member of Technical Staff
Cyber G. India Pvt. Ltd.
orana@cygrp.com
Agenda
 Introduction
 Exploring Threads
 Sharing Resources
 Inter-Thread Communication
 Deadlock
 Advanced Issues
 Assignment
Introduction
 Definition
 Motivation
 Process
 Java Thread Memory Model
 Threads compared with Processes
Definition
 Objects provide a way to divide a program into independent
sections. Often, you also need to turn a program into
separate, independently running subtasks.
Each of these independent subtasks is called a thread.
 A piece of code that run in concurrent with other threads.
 Thread is a statically ordered sequence of instructions.
Motivation
 Resource utilization Programs sometimes have to
wait for external operations such as input or output, and
while waiting can do no useful work. It is more efficient to
use that wait time to let another program run.
 Fairness Multiple users and programs may have equal
claims on the machine's resources. It is preferable to let
them share the computer via finer-grained time slicing than
to let one program run to completion and then start
another.
 Convenience Program with multiple tasks.
Process
 A process is a self-contained running program with
its own address space.
 A multitasking operating system is capable of
running more than one process (program) at a
time.
 A thread is a single sequential flow of control
within a process.
 A single process can thus have multiple
concurrently executing threads
 So threads are lightweight processes.
Process
Java Thread Memory Model
 Memory that can be shared between threads is
called shared memory or heap memory. All instance
fields, static fields and array elements are stored
in heap memory.
 Local variables, method parameters and catched
exception parameters are never shared between
threads and stored in local stack and registers.
Threads compared with Processes
 Processes are typically independent, while threads exist as subsets
of a process

Processes carry considerable state information, whereas multiple
threads within a process share state as well as memory and other
resources

Processes have separate address spaces, whereas threads share
their address space
 Processes interact only through system-provided
inter-process communication mechanisms.

Context switching between threads in the same process is typically
faster than context switching between processes.
Java Threads &
Synchronization
 Introduction
 Exploring Threads
 Sharing Resources
 Inter-Thread Communication
 Deadlock
 Advanced Issues
 Assignment
Exploring Threads
 Thread Life Cycle
 Thread Creation
 Priority
 Joining
 Yielding
 Sleeping
 Interrupting
 Daemon Thread
Thread Life Cycle
 New state: The thread is
considered not alive.
 Runnable (Ready-to-run) state: A
thread start its life. On this state
a thread is waiting for a turn on
the processor.
 Running state: the thread is
currently executing
 Dead state: its run() method
completes.
 Blocked: is waiting the resources
that are hold by another thread.
Life circle of multiple-
threads
Thread Creation
Two ways:
 Extending the java.lang.Thread Class
 Implementing the java.lang.Runnable Interface
Which to choose:
 If you extend the Thread Class, that means that subclass
cannot extend any other Class, but if you implement
Runnable interface then you can do this.
 And the class implementing the Runnable interface can
avoid the full overhead of Thread class which can be
excessive.
 Else use Thread
Priority
 The priority of a thread tells the scheduler how
important this thread is.
 The thread scheduler can use the thread
priorities to determine the execution schedule of
threads.
 Priorities are integer values
 Thread.MIN_PRIORITY: 1
 Thread.MAX_PRIORITY: 10
 Thread.NORM_PRIORITY: 5
Joining
 One thread may call join( ) on another thread to
wait for the second thread to complete before
proceeding.
Yielding
 Causes the currently executing thread to pause
and allow other threads to execute.
 This hint (and it is a hint—there’s no guarantee
your implementation will listen to it) takes the
form of the yield() method.
 In general, yield() is useful only in rare situations
and you can’t rely on it to do any serious tuning of
your application
Sleeping
 Causes the currently executing thread to pause for a given
number of milliseconds.
 When you call sleep( ), it must be placed inside a try block
because it’s possible for sleep( ) to be interrupted before it
times out.
 It just stops the execution of the thread for a while.
 There is no guaranty that thread will resume the execution
after the given number of milliseconds.
 Not to use in real-time application.
Interrupting
 Interruption is a mechanism whereby a thread that is
waiting (or sleeping) can be made to prematurely stop
waiting.
 In general, InterruptedException is thrown when another
thread interrupts the thread calling the blocking method.
The other thread interrupts the blocking/sleeping thread by
calling interrupt() on it.
Daemon Thread
 A “daemon” thread is one that is supposed to
provide a general service in the background as
long as the program is running.
 Thus, when all of the non-daemon threads
complete, the program is terminated. Conversely,
if there are any non daemon threads still running,
the program doesn’t terminate.
Java Threads &
Synchronization
 Introduction
 Exploring Threads
 Sharing Resources
 Inter-Thread Communication
 Deadlock
 Advanced Issues
 Assignment
Sharing Resources
 Improperly accessing resources
 Colliding over resources
 Resolving shared resource conflict
 Semaphore
 Mutex (Mutual Exclusion)
 Synchronization
 Atomic Operations
 Volatile Variable
 Critical sections
Improperly accessing
resources
 Consider the example where one task generates
only even numbers.
 Scenario 1: Single Threaded Program
 Scenario 2: Multi Threaded Program
 Problem is not that the object goes through a
state that violates invariance, but that methods
can be called by threads while the object is in that
intermediate unstable state.
Colliding over resources
 If one thread tries to read the data and other
thread tries to update the same data, it leads to
inconsistent state.
 A race condition occurs when the order of
execution of two or more threads may affect
some variable or outcome in the program.
 Race conditions can be considered harmless
provided end result is correct. Otherwise needs to
be handled.
Resolving shared resource conflict
 Solution: Serialize access to shared resources
 Semaphore: Semaphore is an object containing a
value and two operations and used for
communication between threads.
 Java has built-in support to prevent collisions over
resources in the form of the synchronized
keyword.
 It works much like the Semaphore.
Resolving shared resource
conflict…
 Mutex: A mechanism in which a piece of code is running at a time
by means of a lock (also called Monitor or Lock).
Locks in Java are reentrant. Reentrancy means that locks are
acquired on a per-thread basis rather than per-invocation basis.
 Synchronization: When one object pass a message to another
object then both objects are in synchronized state. Synchronization
needs if multiple objects passes the message to specific object.
 Atomic Operation: An atomic operation is one that cannot be
interrupted by the thread scheduler.
 Volatile Variable: Every time the variable is used it must be read
from main memory. Similarly, every time the variable is written, the
value must be stored in main memory.
Synchronization (Cont…)
 Only methods (or blocks) can be synchronized, Classes and variable
cannot be synchronized.
 If two threads wants to execute a synchronized method in a class,
and both threads are using the same instance of the class to invoke
the method then only one thread can execute the method at a time.
 If you need to synchronize one method in a class, synchronize all of
them, but this is not necessary.
 You can synchronize a block of code rather than a method.
 Constructors cannot be synchronized. Code inside the constructors
can be synchronized.
 Rule zero of concurrent programming: never make any assumptions.
Critical Sections
 Piece of code that must be executed by one thread at a time
 Must have solution that guarantees:
 Mutual exclusion (correctness)
 Absence of deadlock/unnecessary delay (no hang up)
 Randomly entry (fairness)
 This is also called a synchronized block.
Java Threads &
Synchronization
 Introduction
 Exploring Threads
 Sharing Resources
 Inter-Thread Communication
 Deadlock
 Advanced Issues
 Assignment
Inter-Thread Communication
 When multiple threads are running inside an application,
most of them will need to communicate with each other in
some form.
 Threads can communicate each other using wait() and
notify()/notifyAll() methods without any race condition.
 Wait-and-notify must be used in conjunction with the
synchronized lock to prevent a race condition.
 Methods wait(), notify() and notifyAll() are part of the base
class Object and not part of Thread, as is sleep(). Why???
Inter-Thread Communication
(Cont…)
 sleep() does not release the lock when it is called
but method wait() does release the lock.
 The only place you can call wait( ), notify( ) or
notifyAll( ) is within a synchronized method.
 Restaurant Example: The waitperson must wait for the
chef to prepare a meal. When the chef has a meal ready, the
chef notifies the waitperson, who then gets the meal and
goes back to waiting.
The chef represents the producer, and the waitperson
represents the consumer.
Java Threads &
Synchronization
 Introduction
 Exploring Threads
 Sharing Resources
 Inter-Thread Communication
 Deadlock
 Other Stuff
 Assignment
Deadlock
 In general, want to be careful about performing
any operations that might take a long time while
holding a lock.
 It is possible for one thread to get stuck waiting
for another thread, which in turn waits for
another thread, etc., until the chain leads back to
a thread waiting on the first one.
Deadlock (Cont…)
 Example 1
Thread1() { Thread2() {
synchronized(a) { synchronized(b) {
synchronized(b) { synchronized(a) {
… …
} }
} }
} }
 // Thread1 holds lock for a, waits for b
 // Thread2 holds lock for b, waits for a
Deadlock (Cont…)
 Example 2
void moveMoney (Account a, Account b, int amount) {
Synchronized (a) {
synchronized (b) {
a.debit (amount);
b.credit (amount);
}
}
}
 Thread1() { moveMoney(a,b,10); }
// holds lock for a, waits for b
 Thread2() { moveMoney(b,a,100); }
// holds lock for b, waits for a
Java Threads &
Synchronization
 Introduction
 Exploring Threads
 Sharing Resources
 Inter-Thread Communication
 Deadlock
 Other Stuff
 Assignment
Other Stuff
 The proper way to stop
 Interrupting a blocked thread
 Thread groups
 ThreadLocal & InheritableThreadLocal
 New Java API for Concurrency
Other Stuff
 The proper way to stop
 Thread class’ method stop( ), suspend( ), and resume( ) are
deprecated.
 stop() method doesn’t release the locks. So use a flag to tell the
thread when to terminate itself by exiting its run( ) method.
 suspend() and resume() methods used to suspending and
resuming threads. Dangerous, can lead to deadlock.

Instead, use wait(), suspend/resume threads, and
notifyAll()
Interrupting a blocked
thread
 There are times when a thread blocks—such as
when it is waiting for input—and it cannot poll a
flag as it does in the previous example. In these
cases, you can use the Thread.interrupt( ) method
to break out of the blocked code.
 As a general guideline, the use of interrupt()
should be reserved for situations where you want
to interrupt a thread to signal it to die gracefully.
Thread Group
 A Thread Group holds a collection of threads.
 Threads in a thread group can be dealt with as a
group.

May want to interrupt all threads in a
group
“Thread groups are best viewed as an unsuccessful experiment,
and you may simply ignore their existence.”
Joshua Bloch
Software Architect
Oracle (Sun Microsystems)
ThreadLocal & InheritableThreadLocal
 Another methods for Inter-thread
Communication.
join() method of Thread.
A thread can also stream data through a pipe to another thread
using the classes PipedInputStream, PipedOutputStream,
PipedReader and PipedWriter.
Threads can also use thread-specific variables that keep a different
value for different threads by using the classes ThreadLocal and
InheritableThreadLocal.
New Java Concurrent API
ThreadLocal
 ThreadLocal storage define a mechanism so that variable is
local to thread itself.
 Other threads that define the same variable create their
own copy of the variable. This means that thread local
variables cannot be used to share state between threads.
public class ThreadLocal<T>
{
protected T initialValue ( );
public T get( );
public void set (T value);
public void remove( );
…
}
InheritableThreadLocal
 InheritableThreadLocal is a subclass of
ThreadLocal and allows a thread-specific
value to be inherited from the parent
thread to the child thread.
 There are not any public methods on
InheritableThreadLocal. It has one
protected method childValue();
New Java API for Concurrency
 Time out…
 Discuss later
Java Threads &
Synchronization
 Introduction
 Exploring Threads
 Sharing Resources
 Inter-Thread Communication
 Deadlock
 Other Stuff
 Assignment
Assignment
 Modify Restaurant.java so that multiple Customers will place order
requests with WaitPersons, who give the requests to the Chefs,
who fulfill the orders and notify the appropriate WaitPerson, who
gives it to the appropriate Customer.
 Solve the Dining Philosophers Problem.
 Use the classes PipedInputStream, PipedOutputStream,
PipedReader and PipedWriter for Inter-Thread Communication.
Questions
?

More Related Content

Similar to Programming - Java-Threads-and-Synchronization.ppt (20)

Concurrency with java
Concurrency with javaConcurrency with java
Concurrency with java
Fraboni Ec
 
econtent thread in java.pptx
econtent thread in java.pptxecontent thread in java.pptx
econtent thread in java.pptx
ramyan49
 
04 threads-pbl-2-slots
04 threads-pbl-2-slots04 threads-pbl-2-slots
04 threads-pbl-2-slots
mha4
 
04 threads-pbl-2-slots
04 threads-pbl-2-slots04 threads-pbl-2-slots
04 threads-pbl-2-slots
mha4
 
Java threading
Java threadingJava threading
Java threading
Chinh Ngo Nguyen
 
java.pptxytbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
java.pptxytbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbjava.pptxytbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
java.pptxytbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
jakjak36
 
Java Threads: Lightweight Processes
Java Threads: Lightweight ProcessesJava Threads: Lightweight Processes
Java Threads: Lightweight Processes
Isuru Perera
 
8.-OBJECT-ORIENTED-PROGRAMMING-USING-JAVA-Multithreading.pptx
8.-OBJECT-ORIENTED-PROGRAMMING-USING-JAVA-Multithreading.pptx8.-OBJECT-ORIENTED-PROGRAMMING-USING-JAVA-Multithreading.pptx
8.-OBJECT-ORIENTED-PROGRAMMING-USING-JAVA-Multithreading.pptx
sandhyakiran10
 
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
 
THREADALLABOUTCOMOUTERHOWTOYHISDOTH.pptx
THREADALLABOUTCOMOUTERHOWTOYHISDOTH.pptxTHREADALLABOUTCOMOUTERHOWTOYHISDOTH.pptx
THREADALLABOUTCOMOUTERHOWTOYHISDOTH.pptx
CedricBaynosa
 
Threading in java - a pragmatic primer
Threading in java - a pragmatic primerThreading in java - a pragmatic primer
Threading in java - a pragmatic primer
SivaRamaSundar Devasubramaniam
 
Java Multithreading Interview Questions PDF By ScholarHat
Java Multithreading Interview Questions PDF By ScholarHatJava Multithreading Interview Questions PDF By ScholarHat
Java Multithreading Interview Questions PDF By ScholarHat
Scholarhat
 
84694646456445645645645665656465464sdd.pptx
84694646456445645645645665656465464sdd.pptx84694646456445645645645665656465464sdd.pptx
84694646456445645645645665656465464sdd.pptx
mhp821023
 
Multithreading In Java
Multithreading In JavaMultithreading In Java
Multithreading In Java
parag
 
Threads in java
Threads in javaThreads in java
Threads in java
mukesh singh
 
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
 
Threads
ThreadsThreads
Threads
Then Murugeshwari
 
Chap3 multi threaded programming
Chap3 multi threaded programmingChap3 multi threaded programming
Chap3 multi threaded programming
raksharao
 
Basics of Java Concurrency
Basics of Java ConcurrencyBasics of Java Concurrency
Basics of Java Concurrency
kshanth2101
 
Concurrency with java
Concurrency with javaConcurrency with java
Concurrency with java
Fraboni Ec
 
econtent thread in java.pptx
econtent thread in java.pptxecontent thread in java.pptx
econtent thread in java.pptx
ramyan49
 
04 threads-pbl-2-slots
04 threads-pbl-2-slots04 threads-pbl-2-slots
04 threads-pbl-2-slots
mha4
 
04 threads-pbl-2-slots
04 threads-pbl-2-slots04 threads-pbl-2-slots
04 threads-pbl-2-slots
mha4
 
java.pptxytbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
java.pptxytbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbjava.pptxytbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
java.pptxytbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
jakjak36
 
Java Threads: Lightweight Processes
Java Threads: Lightweight ProcessesJava Threads: Lightweight Processes
Java Threads: Lightweight Processes
Isuru Perera
 
8.-OBJECT-ORIENTED-PROGRAMMING-USING-JAVA-Multithreading.pptx
8.-OBJECT-ORIENTED-PROGRAMMING-USING-JAVA-Multithreading.pptx8.-OBJECT-ORIENTED-PROGRAMMING-USING-JAVA-Multithreading.pptx
8.-OBJECT-ORIENTED-PROGRAMMING-USING-JAVA-Multithreading.pptx
sandhyakiran10
 
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
 
THREADALLABOUTCOMOUTERHOWTOYHISDOTH.pptx
THREADALLABOUTCOMOUTERHOWTOYHISDOTH.pptxTHREADALLABOUTCOMOUTERHOWTOYHISDOTH.pptx
THREADALLABOUTCOMOUTERHOWTOYHISDOTH.pptx
CedricBaynosa
 
Java Multithreading Interview Questions PDF By ScholarHat
Java Multithreading Interview Questions PDF By ScholarHatJava Multithreading Interview Questions PDF By ScholarHat
Java Multithreading Interview Questions PDF By ScholarHat
Scholarhat
 
84694646456445645645645665656465464sdd.pptx
84694646456445645645645665656465464sdd.pptx84694646456445645645645665656465464sdd.pptx
84694646456445645645645665656465464sdd.pptx
mhp821023
 
Multithreading In Java
Multithreading In JavaMultithreading In Java
Multithreading In Java
parag
 
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
 
Chap3 multi threaded programming
Chap3 multi threaded programmingChap3 multi threaded programming
Chap3 multi threaded programming
raksharao
 
Basics of Java Concurrency
Basics of Java ConcurrencyBasics of Java Concurrency
Basics of Java Concurrency
kshanth2101
 

Recently uploaded (20)

Capitol Doctoral Presentation -June 2025.pptx
Capitol Doctoral Presentation -June 2025.pptxCapitol Doctoral Presentation -June 2025.pptx
Capitol Doctoral Presentation -June 2025.pptx
CapitolTechU
 
Rai dyansty Chach or Brahamn dynasty, History of Dahir History of Sindh NEP.pptx
Rai dyansty Chach or Brahamn dynasty, History of Dahir History of Sindh NEP.pptxRai dyansty Chach or Brahamn dynasty, History of Dahir History of Sindh NEP.pptx
Rai dyansty Chach or Brahamn dynasty, History of Dahir History of Sindh NEP.pptx
Dr. Ravi Shankar Arya Mahila P. G. College, Banaras Hindu University, Varanasi, India.
 
How to Manage & Create a New Department in Odoo 18 Employee
How to Manage & Create a New Department in Odoo 18 EmployeeHow to Manage & Create a New Department in Odoo 18 Employee
How to Manage & Create a New Department in Odoo 18 Employee
Celine George
 
Respiratory System , Urinary System
Respiratory  System , Urinary SystemRespiratory  System , Urinary System
Respiratory System , Urinary System
RushiMandali
 
MATERI PPT TOPIK 4 LANDASAN FILOSOFIS PENDIDIKAN
MATERI PPT TOPIK 4 LANDASAN FILOSOFIS PENDIDIKANMATERI PPT TOPIK 4 LANDASAN FILOSOFIS PENDIDIKAN
MATERI PPT TOPIK 4 LANDASAN FILOSOFIS PENDIDIKAN
aditya23173
 
Analysis of Quantitative Data Parametric and non-parametric tests.pptx
Analysis of Quantitative Data Parametric and non-parametric tests.pptxAnalysis of Quantitative Data Parametric and non-parametric tests.pptx
Analysis of Quantitative Data Parametric and non-parametric tests.pptx
Shrutidhara2
 
THERAPEUTIC COMMUNICATION included definition, characteristics, nurse patient...
THERAPEUTIC COMMUNICATION included definition, characteristics, nurse patient...THERAPEUTIC COMMUNICATION included definition, characteristics, nurse patient...
THERAPEUTIC COMMUNICATION included definition, characteristics, nurse patient...
parmarjuli1412
 
Strengthened Senior High School - Landas Tool Kit.pptx
Strengthened Senior High School - Landas Tool Kit.pptxStrengthened Senior High School - Landas Tool Kit.pptx
Strengthened Senior High School - Landas Tool Kit.pptx
SteffMusniQuiballo
 
How to Create Quotation Templates Sequence in Odoo 18 Sales
How to Create Quotation Templates Sequence in Odoo 18 SalesHow to Create Quotation Templates Sequence in Odoo 18 Sales
How to Create Quotation Templates Sequence in Odoo 18 Sales
Celine George
 
How to Create a Rainbow Man Effect in Odoo 18
How to Create a Rainbow Man Effect in Odoo 18How to Create a Rainbow Man Effect in Odoo 18
How to Create a Rainbow Man Effect in Odoo 18
Celine George
 
LDMMIA Free Reiki Yoga S9 Grad Level Intuition II
LDMMIA Free Reiki Yoga S9 Grad Level Intuition IILDMMIA Free Reiki Yoga S9 Grad Level Intuition II
LDMMIA Free Reiki Yoga S9 Grad Level Intuition II
LDM & Mia eStudios
 
EUPHORIA GENERAL QUIZ FINALS | QUIZ CLUB OF PSGCAS | 21 MARCH 2025
EUPHORIA GENERAL QUIZ FINALS | QUIZ CLUB OF PSGCAS | 21 MARCH 2025EUPHORIA GENERAL QUIZ FINALS | QUIZ CLUB OF PSGCAS | 21 MARCH 2025
EUPHORIA GENERAL QUIZ FINALS | QUIZ CLUB OF PSGCAS | 21 MARCH 2025
Quiz Club of PSG College of Arts & Science
 
What are the benefits that dance brings?
What are the benefits that dance brings?What are the benefits that dance brings?
What are the benefits that dance brings?
memi27
 
Unit- 4 Biostatistics & Research Methodology.pdf
Unit- 4 Biostatistics & Research Methodology.pdfUnit- 4 Biostatistics & Research Methodology.pdf
Unit- 4 Biostatistics & Research Methodology.pdf
KRUTIKA CHANNE
 
Exploring Ocean Floor Features for Middle School
Exploring Ocean Floor Features for Middle SchoolExploring Ocean Floor Features for Middle School
Exploring Ocean Floor Features for Middle School
Marie
 
MATERI PPT TOPIK 1 LANDASAN FILOSOFIS PENDIDIKAN
MATERI PPT TOPIK 1 LANDASAN FILOSOFIS PENDIDIKANMATERI PPT TOPIK 1 LANDASAN FILOSOFIS PENDIDIKAN
MATERI PPT TOPIK 1 LANDASAN FILOSOFIS PENDIDIKAN
aditya23173
 
Different pricelists for different shops in odoo Point of Sale in Odoo 17
Different pricelists for different shops in odoo Point of Sale in Odoo 17Different pricelists for different shops in odoo Point of Sale in Odoo 17
Different pricelists for different shops in odoo Point of Sale in Odoo 17
Celine George
 
SEXUALITY , UNWANTED PREGANCY AND SEXUAL ASSAULT .pptx
SEXUALITY , UNWANTED PREGANCY AND SEXUAL ASSAULT .pptxSEXUALITY , UNWANTED PREGANCY AND SEXUAL ASSAULT .pptx
SEXUALITY , UNWANTED PREGANCY AND SEXUAL ASSAULT .pptx
PoojaSen20
 
Diptera: The Two-Winged Wonders, The Fly Squad: Order Diptera.pptx
Diptera: The Two-Winged Wonders, The Fly Squad: Order Diptera.pptxDiptera: The Two-Winged Wonders, The Fly Squad: Order Diptera.pptx
Diptera: The Two-Winged Wonders, The Fly Squad: Order Diptera.pptx
Arshad Shaikh
 
Energy Balances Of Oecd Countries 2011 Iea Statistics 1st Edition Oecd
Energy Balances Of Oecd Countries 2011 Iea Statistics 1st Edition OecdEnergy Balances Of Oecd Countries 2011 Iea Statistics 1st Edition Oecd
Energy Balances Of Oecd Countries 2011 Iea Statistics 1st Edition Oecd
razelitouali
 
Capitol Doctoral Presentation -June 2025.pptx
Capitol Doctoral Presentation -June 2025.pptxCapitol Doctoral Presentation -June 2025.pptx
Capitol Doctoral Presentation -June 2025.pptx
CapitolTechU
 
How to Manage & Create a New Department in Odoo 18 Employee
How to Manage & Create a New Department in Odoo 18 EmployeeHow to Manage & Create a New Department in Odoo 18 Employee
How to Manage & Create a New Department in Odoo 18 Employee
Celine George
 
Respiratory System , Urinary System
Respiratory  System , Urinary SystemRespiratory  System , Urinary System
Respiratory System , Urinary System
RushiMandali
 
MATERI PPT TOPIK 4 LANDASAN FILOSOFIS PENDIDIKAN
MATERI PPT TOPIK 4 LANDASAN FILOSOFIS PENDIDIKANMATERI PPT TOPIK 4 LANDASAN FILOSOFIS PENDIDIKAN
MATERI PPT TOPIK 4 LANDASAN FILOSOFIS PENDIDIKAN
aditya23173
 
Analysis of Quantitative Data Parametric and non-parametric tests.pptx
Analysis of Quantitative Data Parametric and non-parametric tests.pptxAnalysis of Quantitative Data Parametric and non-parametric tests.pptx
Analysis of Quantitative Data Parametric and non-parametric tests.pptx
Shrutidhara2
 
THERAPEUTIC COMMUNICATION included definition, characteristics, nurse patient...
THERAPEUTIC COMMUNICATION included definition, characteristics, nurse patient...THERAPEUTIC COMMUNICATION included definition, characteristics, nurse patient...
THERAPEUTIC COMMUNICATION included definition, characteristics, nurse patient...
parmarjuli1412
 
Strengthened Senior High School - Landas Tool Kit.pptx
Strengthened Senior High School - Landas Tool Kit.pptxStrengthened Senior High School - Landas Tool Kit.pptx
Strengthened Senior High School - Landas Tool Kit.pptx
SteffMusniQuiballo
 
How to Create Quotation Templates Sequence in Odoo 18 Sales
How to Create Quotation Templates Sequence in Odoo 18 SalesHow to Create Quotation Templates Sequence in Odoo 18 Sales
How to Create Quotation Templates Sequence in Odoo 18 Sales
Celine George
 
How to Create a Rainbow Man Effect in Odoo 18
How to Create a Rainbow Man Effect in Odoo 18How to Create a Rainbow Man Effect in Odoo 18
How to Create a Rainbow Man Effect in Odoo 18
Celine George
 
LDMMIA Free Reiki Yoga S9 Grad Level Intuition II
LDMMIA Free Reiki Yoga S9 Grad Level Intuition IILDMMIA Free Reiki Yoga S9 Grad Level Intuition II
LDMMIA Free Reiki Yoga S9 Grad Level Intuition II
LDM & Mia eStudios
 
What are the benefits that dance brings?
What are the benefits that dance brings?What are the benefits that dance brings?
What are the benefits that dance brings?
memi27
 
Unit- 4 Biostatistics & Research Methodology.pdf
Unit- 4 Biostatistics & Research Methodology.pdfUnit- 4 Biostatistics & Research Methodology.pdf
Unit- 4 Biostatistics & Research Methodology.pdf
KRUTIKA CHANNE
 
Exploring Ocean Floor Features for Middle School
Exploring Ocean Floor Features for Middle SchoolExploring Ocean Floor Features for Middle School
Exploring Ocean Floor Features for Middle School
Marie
 
MATERI PPT TOPIK 1 LANDASAN FILOSOFIS PENDIDIKAN
MATERI PPT TOPIK 1 LANDASAN FILOSOFIS PENDIDIKANMATERI PPT TOPIK 1 LANDASAN FILOSOFIS PENDIDIKAN
MATERI PPT TOPIK 1 LANDASAN FILOSOFIS PENDIDIKAN
aditya23173
 
Different pricelists for different shops in odoo Point of Sale in Odoo 17
Different pricelists for different shops in odoo Point of Sale in Odoo 17Different pricelists for different shops in odoo Point of Sale in Odoo 17
Different pricelists for different shops in odoo Point of Sale in Odoo 17
Celine George
 
SEXUALITY , UNWANTED PREGANCY AND SEXUAL ASSAULT .pptx
SEXUALITY , UNWANTED PREGANCY AND SEXUAL ASSAULT .pptxSEXUALITY , UNWANTED PREGANCY AND SEXUAL ASSAULT .pptx
SEXUALITY , UNWANTED PREGANCY AND SEXUAL ASSAULT .pptx
PoojaSen20
 
Diptera: The Two-Winged Wonders, The Fly Squad: Order Diptera.pptx
Diptera: The Two-Winged Wonders, The Fly Squad: Order Diptera.pptxDiptera: The Two-Winged Wonders, The Fly Squad: Order Diptera.pptx
Diptera: The Two-Winged Wonders, The Fly Squad: Order Diptera.pptx
Arshad Shaikh
 
Energy Balances Of Oecd Countries 2011 Iea Statistics 1st Edition Oecd
Energy Balances Of Oecd Countries 2011 Iea Statistics 1st Edition OecdEnergy Balances Of Oecd Countries 2011 Iea Statistics 1st Edition Oecd
Energy Balances Of Oecd Countries 2011 Iea Statistics 1st Edition Oecd
razelitouali
 
Ad

Programming - Java-Threads-and-Synchronization.ppt

  • 1. Concurrency Java Threads & Synchronization Omindra Kumar Rana Senior Member of Technical Staff Cyber G. India Pvt. Ltd. [email protected]
  • 2. Agenda  Introduction  Exploring Threads  Sharing Resources  Inter-Thread Communication  Deadlock  Advanced Issues  Assignment
  • 3. Introduction  Definition  Motivation  Process  Java Thread Memory Model  Threads compared with Processes
  • 4. Definition  Objects provide a way to divide a program into independent sections. Often, you also need to turn a program into separate, independently running subtasks. Each of these independent subtasks is called a thread.  A piece of code that run in concurrent with other threads.  Thread is a statically ordered sequence of instructions.
  • 5. Motivation  Resource utilization Programs sometimes have to wait for external operations such as input or output, and while waiting can do no useful work. It is more efficient to use that wait time to let another program run.  Fairness Multiple users and programs may have equal claims on the machine's resources. It is preferable to let them share the computer via finer-grained time slicing than to let one program run to completion and then start another.  Convenience Program with multiple tasks.
  • 6. Process  A process is a self-contained running program with its own address space.  A multitasking operating system is capable of running more than one process (program) at a time.  A thread is a single sequential flow of control within a process.  A single process can thus have multiple concurrently executing threads  So threads are lightweight processes.
  • 8. Java Thread Memory Model  Memory that can be shared between threads is called shared memory or heap memory. All instance fields, static fields and array elements are stored in heap memory.  Local variables, method parameters and catched exception parameters are never shared between threads and stored in local stack and registers.
  • 9. Threads compared with Processes  Processes are typically independent, while threads exist as subsets of a process  Processes carry considerable state information, whereas multiple threads within a process share state as well as memory and other resources  Processes have separate address spaces, whereas threads share their address space  Processes interact only through system-provided inter-process communication mechanisms.  Context switching between threads in the same process is typically faster than context switching between processes.
  • 10. Java Threads & Synchronization  Introduction  Exploring Threads  Sharing Resources  Inter-Thread Communication  Deadlock  Advanced Issues  Assignment
  • 11. Exploring Threads  Thread Life Cycle  Thread Creation  Priority  Joining  Yielding  Sleeping  Interrupting  Daemon Thread
  • 12. Thread Life Cycle  New state: The thread is considered not alive.  Runnable (Ready-to-run) state: A thread start its life. On this state a thread is waiting for a turn on the processor.  Running state: the thread is currently executing  Dead state: its run() method completes.  Blocked: is waiting the resources that are hold by another thread.
  • 13. Life circle of multiple- threads
  • 14. Thread Creation Two ways:  Extending the java.lang.Thread Class  Implementing the java.lang.Runnable Interface Which to choose:  If you extend the Thread Class, that means that subclass cannot extend any other Class, but if you implement Runnable interface then you can do this.  And the class implementing the Runnable interface can avoid the full overhead of Thread class which can be excessive.  Else use Thread
  • 15. Priority  The priority of a thread tells the scheduler how important this thread is.  The thread scheduler can use the thread priorities to determine the execution schedule of threads.  Priorities are integer values  Thread.MIN_PRIORITY: 1  Thread.MAX_PRIORITY: 10  Thread.NORM_PRIORITY: 5
  • 16. Joining  One thread may call join( ) on another thread to wait for the second thread to complete before proceeding.
  • 17. Yielding  Causes the currently executing thread to pause and allow other threads to execute.  This hint (and it is a hint—there’s no guarantee your implementation will listen to it) takes the form of the yield() method.  In general, yield() is useful only in rare situations and you can’t rely on it to do any serious tuning of your application
  • 18. Sleeping  Causes the currently executing thread to pause for a given number of milliseconds.  When you call sleep( ), it must be placed inside a try block because it’s possible for sleep( ) to be interrupted before it times out.  It just stops the execution of the thread for a while.  There is no guaranty that thread will resume the execution after the given number of milliseconds.  Not to use in real-time application.
  • 19. Interrupting  Interruption is a mechanism whereby a thread that is waiting (or sleeping) can be made to prematurely stop waiting.  In general, InterruptedException is thrown when another thread interrupts the thread calling the blocking method. The other thread interrupts the blocking/sleeping thread by calling interrupt() on it.
  • 20. Daemon Thread  A “daemon” thread is one that is supposed to provide a general service in the background as long as the program is running.  Thus, when all of the non-daemon threads complete, the program is terminated. Conversely, if there are any non daemon threads still running, the program doesn’t terminate.
  • 21. Java Threads & Synchronization  Introduction  Exploring Threads  Sharing Resources  Inter-Thread Communication  Deadlock  Advanced Issues  Assignment
  • 22. Sharing Resources  Improperly accessing resources  Colliding over resources  Resolving shared resource conflict  Semaphore  Mutex (Mutual Exclusion)  Synchronization  Atomic Operations  Volatile Variable  Critical sections
  • 23. Improperly accessing resources  Consider the example where one task generates only even numbers.  Scenario 1: Single Threaded Program  Scenario 2: Multi Threaded Program  Problem is not that the object goes through a state that violates invariance, but that methods can be called by threads while the object is in that intermediate unstable state.
  • 24. Colliding over resources  If one thread tries to read the data and other thread tries to update the same data, it leads to inconsistent state.  A race condition occurs when the order of execution of two or more threads may affect some variable or outcome in the program.  Race conditions can be considered harmless provided end result is correct. Otherwise needs to be handled.
  • 25. Resolving shared resource conflict  Solution: Serialize access to shared resources  Semaphore: Semaphore is an object containing a value and two operations and used for communication between threads.  Java has built-in support to prevent collisions over resources in the form of the synchronized keyword.  It works much like the Semaphore.
  • 26. Resolving shared resource conflict…  Mutex: A mechanism in which a piece of code is running at a time by means of a lock (also called Monitor or Lock). Locks in Java are reentrant. Reentrancy means that locks are acquired on a per-thread basis rather than per-invocation basis.  Synchronization: When one object pass a message to another object then both objects are in synchronized state. Synchronization needs if multiple objects passes the message to specific object.  Atomic Operation: An atomic operation is one that cannot be interrupted by the thread scheduler.  Volatile Variable: Every time the variable is used it must be read from main memory. Similarly, every time the variable is written, the value must be stored in main memory.
  • 27. Synchronization (Cont…)  Only methods (or blocks) can be synchronized, Classes and variable cannot be synchronized.  If two threads wants to execute a synchronized method in a class, and both threads are using the same instance of the class to invoke the method then only one thread can execute the method at a time.  If you need to synchronize one method in a class, synchronize all of them, but this is not necessary.  You can synchronize a block of code rather than a method.  Constructors cannot be synchronized. Code inside the constructors can be synchronized.  Rule zero of concurrent programming: never make any assumptions.
  • 28. Critical Sections  Piece of code that must be executed by one thread at a time  Must have solution that guarantees:  Mutual exclusion (correctness)  Absence of deadlock/unnecessary delay (no hang up)  Randomly entry (fairness)  This is also called a synchronized block.
  • 29. Java Threads & Synchronization  Introduction  Exploring Threads  Sharing Resources  Inter-Thread Communication  Deadlock  Advanced Issues  Assignment
  • 30. Inter-Thread Communication  When multiple threads are running inside an application, most of them will need to communicate with each other in some form.  Threads can communicate each other using wait() and notify()/notifyAll() methods without any race condition.  Wait-and-notify must be used in conjunction with the synchronized lock to prevent a race condition.  Methods wait(), notify() and notifyAll() are part of the base class Object and not part of Thread, as is sleep(). Why???
  • 31. Inter-Thread Communication (Cont…)  sleep() does not release the lock when it is called but method wait() does release the lock.  The only place you can call wait( ), notify( ) or notifyAll( ) is within a synchronized method.  Restaurant Example: The waitperson must wait for the chef to prepare a meal. When the chef has a meal ready, the chef notifies the waitperson, who then gets the meal and goes back to waiting. The chef represents the producer, and the waitperson represents the consumer.
  • 32. Java Threads & Synchronization  Introduction  Exploring Threads  Sharing Resources  Inter-Thread Communication  Deadlock  Other Stuff  Assignment
  • 33. Deadlock  In general, want to be careful about performing any operations that might take a long time while holding a lock.  It is possible for one thread to get stuck waiting for another thread, which in turn waits for another thread, etc., until the chain leads back to a thread waiting on the first one.
  • 34. Deadlock (Cont…)  Example 1 Thread1() { Thread2() { synchronized(a) { synchronized(b) { synchronized(b) { synchronized(a) { … … } } } } } }  // Thread1 holds lock for a, waits for b  // Thread2 holds lock for b, waits for a
  • 35. Deadlock (Cont…)  Example 2 void moveMoney (Account a, Account b, int amount) { Synchronized (a) { synchronized (b) { a.debit (amount); b.credit (amount); } } }  Thread1() { moveMoney(a,b,10); } // holds lock for a, waits for b  Thread2() { moveMoney(b,a,100); } // holds lock for b, waits for a
  • 36. Java Threads & Synchronization  Introduction  Exploring Threads  Sharing Resources  Inter-Thread Communication  Deadlock  Other Stuff  Assignment
  • 37. Other Stuff  The proper way to stop  Interrupting a blocked thread  Thread groups  ThreadLocal & InheritableThreadLocal  New Java API for Concurrency
  • 38. Other Stuff  The proper way to stop  Thread class’ method stop( ), suspend( ), and resume( ) are deprecated.  stop() method doesn’t release the locks. So use a flag to tell the thread when to terminate itself by exiting its run( ) method.  suspend() and resume() methods used to suspending and resuming threads. Dangerous, can lead to deadlock.  Instead, use wait(), suspend/resume threads, and notifyAll()
  • 39. Interrupting a blocked thread  There are times when a thread blocks—such as when it is waiting for input—and it cannot poll a flag as it does in the previous example. In these cases, you can use the Thread.interrupt( ) method to break out of the blocked code.  As a general guideline, the use of interrupt() should be reserved for situations where you want to interrupt a thread to signal it to die gracefully.
  • 40. Thread Group  A Thread Group holds a collection of threads.  Threads in a thread group can be dealt with as a group.  May want to interrupt all threads in a group “Thread groups are best viewed as an unsuccessful experiment, and you may simply ignore their existence.” Joshua Bloch Software Architect Oracle (Sun Microsystems)
  • 41. ThreadLocal & InheritableThreadLocal  Another methods for Inter-thread Communication. join() method of Thread. A thread can also stream data through a pipe to another thread using the classes PipedInputStream, PipedOutputStream, PipedReader and PipedWriter. Threads can also use thread-specific variables that keep a different value for different threads by using the classes ThreadLocal and InheritableThreadLocal. New Java Concurrent API
  • 42. ThreadLocal  ThreadLocal storage define a mechanism so that variable is local to thread itself.  Other threads that define the same variable create their own copy of the variable. This means that thread local variables cannot be used to share state between threads. public class ThreadLocal<T> { protected T initialValue ( ); public T get( ); public void set (T value); public void remove( ); … }
  • 43. InheritableThreadLocal  InheritableThreadLocal is a subclass of ThreadLocal and allows a thread-specific value to be inherited from the parent thread to the child thread.  There are not any public methods on InheritableThreadLocal. It has one protected method childValue();
  • 44. New Java API for Concurrency  Time out…  Discuss later
  • 45. Java Threads & Synchronization  Introduction  Exploring Threads  Sharing Resources  Inter-Thread Communication  Deadlock  Other Stuff  Assignment
  • 46. Assignment  Modify Restaurant.java so that multiple Customers will place order requests with WaitPersons, who give the requests to the Chefs, who fulfill the orders and notify the appropriate WaitPerson, who gives it to the appropriate Customer.  Solve the Dining Philosophers Problem.  Use the classes PipedInputStream, PipedOutputStream, PipedReader and PipedWriter for Inter-Thread Communication.