SlideShare a Scribd company logo
Concurrent
Programming
05
Sachintha Gunasena MBCS
http://lk.linkedin.com/in/sachinthadtg
Today…
Sachintha Gunasena MBCS
http://lk.linkedin.com/in/sachinthadtg
Today’s Session
• Processes and Threads
• Thread Objects
• Defining and Starting a Thread
• Pausing Execution with Sleep
• Interrupts
• Joins
• The SimpleThreads Example
• Synchronization
• Thread Interference
• Memory Consistency Errors
Processes & Threads
Process
• has a self-contained execution environment
• generally has a complete, private set of basic run-time resources
• each process has its own memory space
• Inter Process Communication (IPC)
• pipes
• sockets
• Most implementations of the Java virtual machine run as a single
process
Thread
• lightweight processes
• provide an execution environment
• requires fewer resources than creating a new process
• exist within a process
• every process has at least one - main thread
• share the process's resources, including memory and
open files
Thread Objects
Thread Objects
• Each thread is associated with an instance of the class ‘Thread’.
• basic strategies for using Thread objects to create a concurrent
application.
• To directly control thread creation and management, simply
instantiate ’Thread’ each time the application needs to initiate
an asynchronous task.
• To abstract thread management from the rest of your
application, pass the application's tasks to an ‘executor’.
• for now we will use Thread objects.
Defining & Running a Thread
• An application that creates an instance of ‘Thread’ must provide the code that will run in
that thread.
• There are two ways to do this:
• Provide a Runnable object.
• The Runnable interface defines a single method, run, meant to contain the code
executed in the thread.
• The Runnable object is passed to the Thread constructor.
• Subclass Thread.
• The Thread class itself implements Runnable, though its run method does nothing.
• An application can subclass Thread, providing its own implementation of run.
Using a Runnable Object
public class HelloRunnable implements Runnable {
public void run() {
System.out.println("Hello from a thread!");
}
public static void main(String args[]) {
(new Thread(new HelloRunnable())).start();
}
}
Using a Subclass of Thread
public class HelloThread extends Thread {
public void run() {
System.out.println("Hello from a thread!");
}
public static void main(String args[]) {
(new HelloThread()).start();
}
}
Runnable Object vs Thread
Subclass
• both invoke thread.start() to start a new thread
• creating a Runnable Object
• can subclass a class other than Thread
• separates Runnable task from Thread object
• more flexible
• applicable to high-level thread management APIs
• creating a Thread subclass
• is easier to use in simple applications
• is limited cause the task class must be a descendant of Thread
Thread Class
• The Thread class defines a number of methods useful for
thread management.
• These include static methods, which
• provide information about, or
• affect the status of,
• the thread invoking the method.
• The other methods are invoked from other threads involved
in managing the thread and Thread object.
Pausing Execution with
Sleep
• Thread.sleep causes the current thread to suspend
execution for a specified period.
• making processor time available to the other threads of an
application or other applications
• The sleep method can also be used for
• pacing (rate of movement/activity), and
• waiting for another thread
• with duties that are understood to have time requirements
Pausing Execution with
Sleep Cont.d
• Two overloaded versions of sleep are provided:
• one that specifies the sleep time to the millisecond
• and one that specifies the sleep time to the nanosecond.
• sleep times are not guaranteed to be precise
• limited by the facilities provided by the underlying OS
• sleep period can be terminated by interrupts
• In any case, you cannot assume that invoking sleep will suspend
the thread for precisely the time period specified.
Sleep Example
public class SleepMessages {
public static void main(String args[])
throws InterruptedException {
String importantInfo[] = {
"Mares eat oats",
"Does eat oats",
"Little lambs eat ivy",
"A kid will eat ivy too"
};
for (int i = 0;
i < importantInfo.length;
i++) {
//Pause for 4 seconds
Thread.sleep(4000);
//Print a message
System.out.println(importantInfo[i]);
}
}
}
Sleep Example Cont.d
• This example uses sleep to print messages at four-
second intervals
• main declares that it throws InterruptedException.
• exception thrown when another thread interrupts the
current thread while sleep is active
• Since this application has not defined another thread to
cause the interrupt, it doesn't bother to catch
InterruptedException
Interrupts
• an indication to a thread that it should stop what it is doing
and do something else.
• It's up to the programmer to decide exactly how a thread
responds to an interrupt,
• but it is very common for the thread to terminate
• a thread sends an interrupt by invoking interrupt on the
Thread object for the thread to be interrupted.
• For the interrupt mechanism to work correctly, the
interrupted thread must support its own interruption.
Supporting Interruption
• How does a thread support its own interruption?
• depends on what it's currently doing.
• If the thread is frequently invoking methods that throw
InterruptedException, it simply returns from the run method
after it catches that exception.
• For example, suppose the central message loop in the
SleepMessages example were in the run method of a
thread's Runnable object.
• Then it might be modified as follows to support interrupts:
Supporting Interruption
Cont.d
for (int i = 0; i < importantInfo.length; i++) {
// Pause for 4 seconds
try {
Thread.sleep(4000);
} catch (InterruptedException e) {
// We've been interrupted: no more messages.
return;
}
// Print a message
System.out.println(importantInfo[i]);
}
Supporting Interruption
Cont.d
• Many methods that throw InterruptedException, such as
sleep, are designed to cancel their current operation and
return immediately when an interrupt is received.
• What if a thread goes a long time without invoking a
method that throws InterruptedException?
• Then it must periodically invoke Thread.interrupted,
• which returns true if an interrupt has been received.
Supporting Interruption
Cont.d
for (int i = 0; i < inputs.length; i++) {
heavyCrunch(inputs[i]);
if (Thread.interrupted()) {
// We've been interrupted: no more crunching.
return;
}
}
Supporting Interruption
Cont.d
• In this simple example, the code simply tests for the interrupt and
exits the thread if one has been received.
• In more complex applications, it might make more sense to throw
an InterruptedException:
• This allows interrupt handling code to be centralized in a catch
clause.
if (Thread.interrupted()) {
throw new InterruptedException();
}
Interrupt Status Flag
• The interrupt mechanism is implemented using an internal flag known
as the interrupt status. Invoking Thread.interrupt sets this flag.
• When a thread checks for an interrupt by invoking the static method
Thread.interrupted, interrupt status is 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.
• However, it's always possible that interrupt status will immediately be
set again, by another thread invoking interrupt.
Joins
• The join method allows one thread to wait for the completion of another.
• If t is a Thread object whose thread is currently executing,
• causes the current thread to pause execution until t's thread terminates.
• Overloads of join allow the programmer to specify a waiting period.
• similar to sleep, join is dependent on the OS for timing
• should not assume that join will wait exactly as long as you specify
• Like sleep, join responds to an interrupt by exiting with an
InterruptedException.
t.join();
Simple Threads Example
• SimpleThreads consists of two threads.
• The first is the main thread that every Java application has.
• main thread creates a new thread from the Runnable object,
MessageLoop, and waits for it to finish.
• If the MessageLoop thread takes too long to finish, the main thread
interrupts it.
• The MessageLoop thread prints out a series of messages.
• If interrupted before it has printed all its messages, the
MessageLoop thread prints a message and exits.
Synchronization
Synchronization
• Threads communicate primarily by sharing access to
• fields
• and the objects reference fields refer to.
• This form of communication is extremely efficient
• but makes two kinds of errors:
• thread interference
• memory consistency errors.
• The tool needed to prevent these errors is synchronization.
Synchronization Cont.d
• However, synchronization can introduce thread
contention
• 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.
Synchronization Cont.d
• Thread Interference
• describes how errors are introduced when multiple threads access shared data.
• Memory Consistency Errors
• describes errors that result from inconsistent views of shared memory.
• Synchronized Methods
• describes a simple idiom that can effectively prevent thread interference and memory
consistency errors.
• Implicit Locks and Synchronization
• describes a more general synchronization idiom, and describes how synchronization is based
on implicit locks.
• Atomic Access
• talks about the general idea of operations that can't be interfered with by other threads.
Thread Interference
class Counter {
private int c = 0;
public void increment() {
c++;
}
public void decrement() {
c--;
}
public int value() {
return c;
}
}
Thread Interference Cont.d
• Counter is designed so that each invocation of increment will
add 1 to c, and each invocation of decrement will subtract 1
from c.
• However, if a Counter object is referenced from multiple
threads, interference between threads may prevent this from
happening as expected.
• Interference happens 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.
Thread Interference Cont.d
• It might not seem possible for operations on instances of Counter to interleave,
since both operations on c are single, simple statements.
• However, even simple statements can translate to multiple steps by the virtual
machine.
• We won't examine the specific steps the virtual machine takes — it is enough to
know that the single expression c++ can be decomposed into three steps:
• Retrieve the current value of c.
• Increment the retrieved value by 1.
• Store the incremented value back in c.
• The expression c-- can be decomposed the same way, except that the second
step decrements instead of increments.
Thread Interference Contd.
• If Thread A invokes increment at about the same time Thread B invokes decrement.
• If the initial value of c is 0, their interleaved actions might follow this sequence:
• Thread A: Retrieve c.
• Thread B: Retrieve c.
• Thread A: Increment retrieved value; result is 1.
• Thread B: Decrement retrieved value; result is -1.
• Thread A: Store result in c; c is now 1.
• Thread B: Store result in c; c is now -1.
• Thread A's result is lost, overwritten by Thread B.
• This particular interleaving is only one possibility.
• Under different circumstances it might be Thread B's result that gets lost, or there could be no error at all.
• Because they are unpredictable, thread interference bugs can be difficult to detect and fix.
Memory Consistence Errors
• occur when different threads have inconsistent views of what should be
the same data.
• causes are complex and beyond the scope of this tutorial.
• programmer does not need a detailed understanding of these causes.
• All that is needed is a strategy for avoiding them.
• The key to avoiding memory consistency errors is understanding the
happens-before relationship.
• This is simply a guarantee that memory writes by one specific statement
are visible to another specific statement.
• To see this, consider the following example.
Memory Consistence Errors
Cont.d
• Suppose a simple int field is defined and initialized:
• Then, shortly afterwards, thread B prints out counter:
• If the two statements had been executed in the same thread, the value printed out would be "1".
• But if the two statements are executed in separate threads, the value printed out might well be "0",
• because there's no guarantee that thread A's change to counter will be visible to thread B
• unless the programmer has established a happens-before relationship between these two
statements.
• There are several actions that create happens-before relationships.
• One of them is synchronization
int counter = 0;
System.out.println(counter);
Memory Consistence Errors
Cont.d
• We've already seen two actions that create happens-before relationships.
• When a statement invokes Thread.start,
• every statement that has a happens-before relationship with that statement
• also has a happens-before relationship with every statement executed by the new
thread.
• effects of the code that led up to the creation of the new thread are visible to the new
thread.
• When a thread terminates and causes a Thread.join in another thread to return,
• then all the statements executed by the terminated thread have a happens-before
relationship with all the statements following the successful join.
• effects of the code in the thread are now visible to the thread that performed the join.
Next…
Next Up…
• Synchronization
• Synchronized Methods
• Intrinsic Locks and Synchronization
• Atomic Access
• Liveness
• Deadlock
• Starvation and Livelock
References
• http://docs.oracle.com/javase/tutorial/essential/
Sachintha Gunasena MBCS
http://lk.linkedin.com/in/sachinthadtg
Thank you.
Sachintha Gunasena MBCS
http://lk.linkedin.com/in/sachinthadtg

More Related Content

What's hot (16)

PPTX
Thread syncronization
priyabogra1
 
PDF
.Net Threading
Erik Ralston
 
PDF
[Java concurrency]02.basic thread synchronization
xuehan zhu
 
PPTX
C# Thread synchronization
Prem Kumar Badri
 
PDF
[Java concurrency]01.thread management
xuehan zhu
 
PPTX
Concurrency Programming in Java - 07 - High-level Concurrency objects, Lock O...
Sachintha Gunasena
 
PPTX
multi threading
Yaswanth Babu Gummadivelli
 
PPT
multithreading
Rajkattamuri
 
PPTX
Advanced Introduction to Java Multi-Threading - Full (chok)
choksheak
 
PPTX
Thread model of java
myrajendra
 
PPT
Java
Khasim Cise
 
PPT
Multithreading
F K
 
PPT
Java
mdfkhan625
 
PPTX
.NET Multithreading/Multitasking
Sasha Kravchuk
 
ODP
Multithreading 101
Tim Penhey
 
PPT
Java Multithreading
Rajkattamuri
 
Thread syncronization
priyabogra1
 
.Net Threading
Erik Ralston
 
[Java concurrency]02.basic thread synchronization
xuehan zhu
 
C# Thread synchronization
Prem Kumar Badri
 
[Java concurrency]01.thread management
xuehan zhu
 
Concurrency Programming in Java - 07 - High-level Concurrency objects, Lock O...
Sachintha Gunasena
 
multithreading
Rajkattamuri
 
Advanced Introduction to Java Multi-Threading - Full (chok)
choksheak
 
Thread model of java
myrajendra
 
Multithreading
F K
 
.NET Multithreading/Multitasking
Sasha Kravchuk
 
Multithreading 101
Tim Penhey
 
Java Multithreading
Rajkattamuri
 

Viewers also liked (19)

PDF
javathreads
Arjun Shanka
 
PDF
Java threading
Chinh Ngo Nguyen
 
PDF
FM - JESSIE J
Bianca Barbosa
 
PDF
IGNITE_COMPANY_PROFILE
Rummana Siddiqi
 
PDF
Market research-v2
Vetle Andre Remme Olsen
 
PPTX
High speed disperser manufacturer in india
galaxyprocess
 
PDF
LA INVESTIGACIÓN ACCIÓN EDUCATIVA
YONY RAFAEL HUAMANI
 
PPTX
Java Thread & Multithreading
jehan1987
 
PPTX
Erosion and Deposition
Aia-Issah Orquiola
 
PPTX
Java threads - part 3
Nakraynikov Oleg
 
PDF
Qué cambia en el modelo 190 en 2017
Sage España
 
PDF
High–Performance Computing
BRAC University Computer Club
 
PDF
Programming with Threads in Java
koji lin
 
PDF
Java Course 10: Threads and Concurrency
Anton Keks
 
javathreads
Arjun Shanka
 
Java threading
Chinh Ngo Nguyen
 
FM - JESSIE J
Bianca Barbosa
 
IGNITE_COMPANY_PROFILE
Rummana Siddiqi
 
Market research-v2
Vetle Andre Remme Olsen
 
High speed disperser manufacturer in india
galaxyprocess
 
LA INVESTIGACIÓN ACCIÓN EDUCATIVA
YONY RAFAEL HUAMANI
 
Java Thread & Multithreading
jehan1987
 
Erosion and Deposition
Aia-Issah Orquiola
 
Java threads - part 3
Nakraynikov Oleg
 
Qué cambia en el modelo 190 en 2017
Sage España
 
High–Performance Computing
BRAC University Computer Club
 
Programming with Threads in Java
koji lin
 
Java Course 10: Threads and Concurrency
Anton Keks
 
Ad

Similar to Concurrency Programming in Java - 05 - Processes and Threads, Thread Objects, Thread Start, Sleep, Interrupt (20)

PPTX
Object-Oriented-Prog_MultiThreading.pptx
NasreenTaj20
 
PPTX
Concurrency with java
Tony Nguyen
 
PPTX
Concurrency with java
Harry Potter
 
PPTX
Concurrency with java
James Wong
 
PPTX
Concurrency with java
Luis Goldster
 
PPTX
Concurrency with java
Young Alista
 
PPTX
Concurrency with java
Fraboni Ec
 
PPT
Java multithreading
Mohammed625
 
PPTX
MULTI THREADING IN JAVA
VINOTH R
 
PPTX
econtent thread in java.pptx
ramyan49
 
PPTX
U4 JAVA.pptx
madan r
 
PPTX
Multi-Threading in Java power point presenetation
AshokRachapalli1
 
PPTX
unit3 Exception Handling multithreadingppt.pptx
ArunPatrick2
 
PPTX
unit3multithreadingppt-copy-180122162204.pptx
ArunPatrick2
 
PDF
Multithreading Introduction and Lifecyle of thread
Kartik Dube
 
PPT
web programming-Multithreading concept in Java.ppt
mcjaya2024
 
PPTX
1.17 Thread in java.pptx
TREXSHyNE
 
PDF
CSE 3146 M1- MULTI THREADING USING JAVA .pdf
universitypresidency
 
PPTX
Multithreadingppt.pptx
HKShab
 
PPTX
Multi threading
gndu
 
Object-Oriented-Prog_MultiThreading.pptx
NasreenTaj20
 
Concurrency with java
Tony Nguyen
 
Concurrency with java
Harry Potter
 
Concurrency with java
James Wong
 
Concurrency with java
Luis Goldster
 
Concurrency with java
Young Alista
 
Concurrency with java
Fraboni Ec
 
Java multithreading
Mohammed625
 
MULTI THREADING IN JAVA
VINOTH R
 
econtent thread in java.pptx
ramyan49
 
U4 JAVA.pptx
madan r
 
Multi-Threading in Java power point presenetation
AshokRachapalli1
 
unit3 Exception Handling multithreadingppt.pptx
ArunPatrick2
 
unit3multithreadingppt-copy-180122162204.pptx
ArunPatrick2
 
Multithreading Introduction and Lifecyle of thread
Kartik Dube
 
web programming-Multithreading concept in Java.ppt
mcjaya2024
 
1.17 Thread in java.pptx
TREXSHyNE
 
CSE 3146 M1- MULTI THREADING USING JAVA .pdf
universitypresidency
 
Multithreadingppt.pptx
HKShab
 
Multi threading
gndu
 
Ad

More from Sachintha Gunasena (16)

PPTX
Entrepreneurship and Commerce in IT - 14 - Web Marketing Communications
Sachintha Gunasena
 
PPTX
Entrepreneurship and Commerce in IT - 13 - The Internet Audience, consumer be...
Sachintha Gunasena
 
PPTX
Entrepreneurship & Commerce in IT - 12 - Web Payments
Sachintha Gunasena
 
PPTX
Concurrency Programming in Java - 03 - Essentials of Java Part 2
Sachintha Gunasena
 
PPTX
Concurrency Programming in Java - 02 - Essentials of Java Part 1
Sachintha Gunasena
 
PPTX
Entrepreneurship & Commerce in IT - 11 - Security & Encryption
Sachintha Gunasena
 
PPTX
Entrepreneurship & Commerce in IT - 08 - E-Commerce business models and concepts
Sachintha Gunasena
 
PPTX
Entrepreneurship & Commerce in IT - 10 - The Internet today and How to build ...
Sachintha Gunasena
 
PPTX
Entrepreneurship & Commerce in IT - 09 - The internet and the world wide web
Sachintha Gunasena
 
PPTX
Entrepreneurship and Commerce in IT - 07 - Introduction to E-Commerce I - e-c...
Sachintha Gunasena
 
PPT
Entrepreneurship and Commerce in IT - 06 - Funding, Expanding, and Exit Strat...
Sachintha Gunasena
 
PPT
Entrepreneurship and Commerce in IT - 05 - Marketing, Technology and Marketin...
Sachintha Gunasena
 
PPT
Entrepreneurship & Commerce in IT - 01 - Introduction in to Entrepreneurship,...
Sachintha Gunasena
 
PPT
Entrepreneurship & Commerce in IT - 02 - Basic Concepts of Entrepreneurship, ...
Sachintha Gunasena
 
PPT
Entrepreneurship & Commerce in IT - 04 - Marketing Plan, Marketing 7 P's, STP...
Sachintha Gunasena
 
PPT
Entrepreneurship & Commerce in IT - 03 - Writing a Business Plan, Creating a ...
Sachintha Gunasena
 
Entrepreneurship and Commerce in IT - 14 - Web Marketing Communications
Sachintha Gunasena
 
Entrepreneurship and Commerce in IT - 13 - The Internet Audience, consumer be...
Sachintha Gunasena
 
Entrepreneurship & Commerce in IT - 12 - Web Payments
Sachintha Gunasena
 
Concurrency Programming in Java - 03 - Essentials of Java Part 2
Sachintha Gunasena
 
Concurrency Programming in Java - 02 - Essentials of Java Part 1
Sachintha Gunasena
 
Entrepreneurship & Commerce in IT - 11 - Security & Encryption
Sachintha Gunasena
 
Entrepreneurship & Commerce in IT - 08 - E-Commerce business models and concepts
Sachintha Gunasena
 
Entrepreneurship & Commerce in IT - 10 - The Internet today and How to build ...
Sachintha Gunasena
 
Entrepreneurship & Commerce in IT - 09 - The internet and the world wide web
Sachintha Gunasena
 
Entrepreneurship and Commerce in IT - 07 - Introduction to E-Commerce I - e-c...
Sachintha Gunasena
 
Entrepreneurship and Commerce in IT - 06 - Funding, Expanding, and Exit Strat...
Sachintha Gunasena
 
Entrepreneurship and Commerce in IT - 05 - Marketing, Technology and Marketin...
Sachintha Gunasena
 
Entrepreneurship & Commerce in IT - 01 - Introduction in to Entrepreneurship,...
Sachintha Gunasena
 
Entrepreneurship & Commerce in IT - 02 - Basic Concepts of Entrepreneurship, ...
Sachintha Gunasena
 
Entrepreneurship & Commerce in IT - 04 - Marketing Plan, Marketing 7 P's, STP...
Sachintha Gunasena
 
Entrepreneurship & Commerce in IT - 03 - Writing a Business Plan, Creating a ...
Sachintha Gunasena
 

Recently uploaded (20)

PDF
HiHelloHR – Simplify HR Operations for Modern Workplaces
HiHelloHR
 
PDF
유니티에서 Burst Compiler+ThreadedJobs+SIMD 적용사례
Seongdae Kim
 
PPTX
Help for Correlations in IBM SPSS Statistics.pptx
Version 1 Analytics
 
PPTX
Change Common Properties in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
PDF
Empower Your Tech Vision- Why Businesses Prefer to Hire Remote Developers fro...
logixshapers59
 
PPTX
Agentic Automation: Build & Deploy Your First UiPath Agent
klpathrudu
 
PPTX
Empowering Asian Contributions: The Rise of Regional User Groups in Open Sour...
Shane Coughlan
 
PDF
MiniTool Partition Wizard 12.8 Crack License Key LATEST
hashhshs786
 
PPTX
Milwaukee Marketo User Group - Summer Road Trip: Mapping and Personalizing Yo...
bbedford2
 
PPTX
OpenChain @ OSS NA - In From the Cold: Open Source as Part of Mainstream Soft...
Shane Coughlan
 
PDF
How to Hire AI Developers_ Step-by-Step Guide in 2025.pdf
DianApps Technologies
 
PPTX
Tally software_Introduction_Presentation
AditiBansal54083
 
PDF
Digger Solo: Semantic search and maps for your local files
seanpedersen96
 
PDF
Unlock Efficiency with Insurance Policy Administration Systems
Insurance Tech Services
 
PDF
IDM Crack with Internet Download Manager 6.42 Build 43 with Patch Latest 2025
bashirkhan333g
 
PPTX
Tally_Basic_Operations_Presentation.pptx
AditiBansal54083
 
PDF
Odoo CRM vs Zoho CRM: Honest Comparison 2025
Odiware Technologies Private Limited
 
PPTX
Hardware(Central Processing Unit ) CU and ALU
RizwanaKalsoom2
 
PDF
SAP Firmaya İade ABAB Kodları - ABAB ile yazılmıl hazır kod örneği
Salih Küçük
 
PDF
Generic or Specific? Making sensible software design decisions
Bert Jan Schrijver
 
HiHelloHR – Simplify HR Operations for Modern Workplaces
HiHelloHR
 
유니티에서 Burst Compiler+ThreadedJobs+SIMD 적용사례
Seongdae Kim
 
Help for Correlations in IBM SPSS Statistics.pptx
Version 1 Analytics
 
Change Common Properties in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
Empower Your Tech Vision- Why Businesses Prefer to Hire Remote Developers fro...
logixshapers59
 
Agentic Automation: Build & Deploy Your First UiPath Agent
klpathrudu
 
Empowering Asian Contributions: The Rise of Regional User Groups in Open Sour...
Shane Coughlan
 
MiniTool Partition Wizard 12.8 Crack License Key LATEST
hashhshs786
 
Milwaukee Marketo User Group - Summer Road Trip: Mapping and Personalizing Yo...
bbedford2
 
OpenChain @ OSS NA - In From the Cold: Open Source as Part of Mainstream Soft...
Shane Coughlan
 
How to Hire AI Developers_ Step-by-Step Guide in 2025.pdf
DianApps Technologies
 
Tally software_Introduction_Presentation
AditiBansal54083
 
Digger Solo: Semantic search and maps for your local files
seanpedersen96
 
Unlock Efficiency with Insurance Policy Administration Systems
Insurance Tech Services
 
IDM Crack with Internet Download Manager 6.42 Build 43 with Patch Latest 2025
bashirkhan333g
 
Tally_Basic_Operations_Presentation.pptx
AditiBansal54083
 
Odoo CRM vs Zoho CRM: Honest Comparison 2025
Odiware Technologies Private Limited
 
Hardware(Central Processing Unit ) CU and ALU
RizwanaKalsoom2
 
SAP Firmaya İade ABAB Kodları - ABAB ile yazılmıl hazır kod örneği
Salih Küçük
 
Generic or Specific? Making sensible software design decisions
Bert Jan Schrijver
 

Concurrency Programming in Java - 05 - Processes and Threads, Thread Objects, Thread Start, Sleep, Interrupt

  • 3. Today’s Session • Processes and Threads • Thread Objects • Defining and Starting a Thread • Pausing Execution with Sleep • Interrupts • Joins • The SimpleThreads Example • Synchronization • Thread Interference • Memory Consistency Errors
  • 5. Process • has a self-contained execution environment • generally has a complete, private set of basic run-time resources • each process has its own memory space • Inter Process Communication (IPC) • pipes • sockets • Most implementations of the Java virtual machine run as a single process
  • 6. Thread • lightweight processes • provide an execution environment • requires fewer resources than creating a new process • exist within a process • every process has at least one - main thread • share the process's resources, including memory and open files
  • 8. Thread Objects • Each thread is associated with an instance of the class ‘Thread’. • basic strategies for using Thread objects to create a concurrent application. • To directly control thread creation and management, simply instantiate ’Thread’ each time the application needs to initiate an asynchronous task. • To abstract thread management from the rest of your application, pass the application's tasks to an ‘executor’. • for now we will use Thread objects.
  • 9. Defining & Running a Thread • An application that creates an instance of ‘Thread’ must provide the code that will run in that thread. • There are two ways to do this: • Provide a Runnable object. • The Runnable interface defines a single method, run, meant to contain the code executed in the thread. • The Runnable object is passed to the Thread constructor. • Subclass Thread. • The Thread class itself implements Runnable, though its run method does nothing. • An application can subclass Thread, providing its own implementation of run.
  • 10. Using a Runnable Object public class HelloRunnable implements Runnable { public void run() { System.out.println("Hello from a thread!"); } public static void main(String args[]) { (new Thread(new HelloRunnable())).start(); } }
  • 11. Using a Subclass of Thread public class HelloThread extends Thread { public void run() { System.out.println("Hello from a thread!"); } public static void main(String args[]) { (new HelloThread()).start(); } }
  • 12. Runnable Object vs Thread Subclass • both invoke thread.start() to start a new thread • creating a Runnable Object • can subclass a class other than Thread • separates Runnable task from Thread object • more flexible • applicable to high-level thread management APIs • creating a Thread subclass • is easier to use in simple applications • is limited cause the task class must be a descendant of Thread
  • 13. Thread Class • The Thread class defines a number of methods useful for thread management. • These include static methods, which • provide information about, or • affect the status of, • the thread invoking the method. • The other methods are invoked from other threads involved in managing the thread and Thread object.
  • 14. Pausing Execution with Sleep • Thread.sleep causes the current thread to suspend execution for a specified period. • making processor time available to the other threads of an application or other applications • The sleep method can also be used for • pacing (rate of movement/activity), and • waiting for another thread • with duties that are understood to have time requirements
  • 15. Pausing Execution with Sleep Cont.d • Two overloaded versions of sleep are provided: • one that specifies the sleep time to the millisecond • and one that specifies the sleep time to the nanosecond. • sleep times are not guaranteed to be precise • limited by the facilities provided by the underlying OS • sleep period can be terminated by interrupts • In any case, you cannot assume that invoking sleep will suspend the thread for precisely the time period specified.
  • 16. Sleep Example public class SleepMessages { public static void main(String args[]) throws InterruptedException { String importantInfo[] = { "Mares eat oats", "Does eat oats", "Little lambs eat ivy", "A kid will eat ivy too" }; for (int i = 0; i < importantInfo.length; i++) { //Pause for 4 seconds Thread.sleep(4000); //Print a message System.out.println(importantInfo[i]); } } }
  • 17. Sleep Example Cont.d • This example uses sleep to print messages at four- second intervals • main declares that it throws InterruptedException. • exception thrown when another thread interrupts the current thread while sleep is active • Since this application has not defined another thread to cause the interrupt, it doesn't bother to catch InterruptedException
  • 18. Interrupts • an indication to a thread that it should stop what it is doing and do something else. • It's up to the programmer to decide exactly how a thread responds to an interrupt, • but it is very common for the thread to terminate • a thread sends an interrupt by invoking interrupt on the Thread object for the thread to be interrupted. • For the interrupt mechanism to work correctly, the interrupted thread must support its own interruption.
  • 19. Supporting Interruption • How does a thread support its own interruption? • depends on what it's currently doing. • If the thread is frequently invoking methods that throw InterruptedException, it simply returns from the run method after it catches that exception. • For example, suppose the central message loop in the SleepMessages example were in the run method of a thread's Runnable object. • Then it might be modified as follows to support interrupts:
  • 20. Supporting Interruption Cont.d for (int i = 0; i < importantInfo.length; i++) { // Pause for 4 seconds try { Thread.sleep(4000); } catch (InterruptedException e) { // We've been interrupted: no more messages. return; } // Print a message System.out.println(importantInfo[i]); }
  • 21. Supporting Interruption Cont.d • Many methods that throw InterruptedException, such as sleep, are designed to cancel their current operation and return immediately when an interrupt is received. • What if a thread goes a long time without invoking a method that throws InterruptedException? • Then it must periodically invoke Thread.interrupted, • which returns true if an interrupt has been received.
  • 22. Supporting Interruption Cont.d for (int i = 0; i < inputs.length; i++) { heavyCrunch(inputs[i]); if (Thread.interrupted()) { // We've been interrupted: no more crunching. return; } }
  • 23. Supporting Interruption Cont.d • In this simple example, the code simply tests for the interrupt and exits the thread if one has been received. • In more complex applications, it might make more sense to throw an InterruptedException: • This allows interrupt handling code to be centralized in a catch clause. if (Thread.interrupted()) { throw new InterruptedException(); }
  • 24. Interrupt Status Flag • The interrupt mechanism is implemented using an internal flag known as the interrupt status. Invoking Thread.interrupt sets this flag. • When a thread checks for an interrupt by invoking the static method Thread.interrupted, interrupt status is 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. • However, it's always possible that interrupt status will immediately be set again, by another thread invoking interrupt.
  • 25. Joins • The join method allows one thread to wait for the completion of another. • If t is a Thread object whose thread is currently executing, • causes the current thread to pause execution until t's thread terminates. • Overloads of join allow the programmer to specify a waiting period. • similar to sleep, join is dependent on the OS for timing • should not assume that join will wait exactly as long as you specify • Like sleep, join responds to an interrupt by exiting with an InterruptedException. t.join();
  • 26. Simple Threads Example • SimpleThreads consists of two threads. • The first is the main thread that every Java application has. • main thread creates a new thread from the Runnable object, MessageLoop, and waits for it to finish. • If the MessageLoop thread takes too long to finish, the main thread interrupts it. • The MessageLoop thread prints out a series of messages. • If interrupted before it has printed all its messages, the MessageLoop thread prints a message and exits.
  • 28. Synchronization • Threads communicate primarily by sharing access to • fields • and the objects reference fields refer to. • This form of communication is extremely efficient • but makes two kinds of errors: • thread interference • memory consistency errors. • The tool needed to prevent these errors is synchronization.
  • 29. Synchronization Cont.d • However, synchronization can introduce thread contention • 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.
  • 30. Synchronization Cont.d • Thread Interference • describes how errors are introduced when multiple threads access shared data. • Memory Consistency Errors • describes errors that result from inconsistent views of shared memory. • Synchronized Methods • describes a simple idiom that can effectively prevent thread interference and memory consistency errors. • Implicit Locks and Synchronization • describes a more general synchronization idiom, and describes how synchronization is based on implicit locks. • Atomic Access • talks about the general idea of operations that can't be interfered with by other threads.
  • 31. Thread Interference class Counter { private int c = 0; public void increment() { c++; } public void decrement() { c--; } public int value() { return c; } }
  • 32. Thread Interference Cont.d • Counter is designed so that each invocation of increment will add 1 to c, and each invocation of decrement will subtract 1 from c. • However, if a Counter object is referenced from multiple threads, interference between threads may prevent this from happening as expected. • Interference happens 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.
  • 33. Thread Interference Cont.d • It might not seem possible for operations on instances of Counter to interleave, since both operations on c are single, simple statements. • However, even simple statements can translate to multiple steps by the virtual machine. • We won't examine the specific steps the virtual machine takes — it is enough to know that the single expression c++ can be decomposed into three steps: • Retrieve the current value of c. • Increment the retrieved value by 1. • Store the incremented value back in c. • The expression c-- can be decomposed the same way, except that the second step decrements instead of increments.
  • 34. Thread Interference Contd. • If Thread A invokes increment at about the same time Thread B invokes decrement. • If the initial value of c is 0, their interleaved actions might follow this sequence: • Thread A: Retrieve c. • Thread B: Retrieve c. • Thread A: Increment retrieved value; result is 1. • Thread B: Decrement retrieved value; result is -1. • Thread A: Store result in c; c is now 1. • Thread B: Store result in c; c is now -1. • Thread A's result is lost, overwritten by Thread B. • This particular interleaving is only one possibility. • Under different circumstances it might be Thread B's result that gets lost, or there could be no error at all. • Because they are unpredictable, thread interference bugs can be difficult to detect and fix.
  • 35. Memory Consistence Errors • occur when different threads have inconsistent views of what should be the same data. • causes are complex and beyond the scope of this tutorial. • programmer does not need a detailed understanding of these causes. • All that is needed is a strategy for avoiding them. • The key to avoiding memory consistency errors is understanding the happens-before relationship. • This is simply a guarantee that memory writes by one specific statement are visible to another specific statement. • To see this, consider the following example.
  • 36. Memory Consistence Errors Cont.d • Suppose a simple int field is defined and initialized: • Then, shortly afterwards, thread B prints out counter: • If the two statements had been executed in the same thread, the value printed out would be "1". • But if the two statements are executed in separate threads, the value printed out might well be "0", • because there's no guarantee that thread A's change to counter will be visible to thread B • unless the programmer has established a happens-before relationship between these two statements. • There are several actions that create happens-before relationships. • One of them is synchronization int counter = 0; System.out.println(counter);
  • 37. Memory Consistence Errors Cont.d • We've already seen two actions that create happens-before relationships. • When a statement invokes Thread.start, • every statement that has a happens-before relationship with that statement • also has a happens-before relationship with every statement executed by the new thread. • effects of the code that led up to the creation of the new thread are visible to the new thread. • When a thread terminates and causes a Thread.join in another thread to return, • then all the statements executed by the terminated thread have a happens-before relationship with all the statements following the successful join. • effects of the code in the thread are now visible to the thread that performed the join.
  • 39. Next Up… • Synchronization • Synchronized Methods • Intrinsic Locks and Synchronization • Atomic Access • Liveness • Deadlock • Starvation and Livelock
  • 40. References • http://docs.oracle.com/javase/tutorial/essential/ Sachintha Gunasena MBCS http://lk.linkedin.com/in/sachinthadtg
  • 41. Thank you. Sachintha Gunasena MBCS http://lk.linkedin.com/in/sachinthadtg