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

PPTX
Concurrency Programming in Java - 06 - Thread Synchronization, Liveness, Guar...
PPT
Java Performance, Threading and Concurrent Data Structures
PPT
Threads And Synchronization in C#
PDF
Sync, async and multithreading
PPTX
Threading in C#
PPTX
Concurrency with java
PPTX
.NET: Thread Synchronization Constructs
PPTX
Concurrency Programming in Java - 01 - Introduction to Concurrency Programming
Concurrency Programming in Java - 06 - Thread Synchronization, Liveness, Guar...
Java Performance, Threading and Concurrent Data Structures
Threads And Synchronization in C#
Sync, async and multithreading
Threading in C#
Concurrency with java
.NET: Thread Synchronization Constructs
Concurrency Programming in Java - 01 - Introduction to Concurrency Programming

What's hot (16)

PPTX
Thread syncronization
PDF
.Net Threading
PDF
[Java concurrency]02.basic thread synchronization
PPTX
C# Thread synchronization
PDF
[Java concurrency]01.thread management
PPTX
Concurrency Programming in Java - 07 - High-level Concurrency objects, Lock O...
PPTX
PPT
multithreading
PPTX
Advanced Introduction to Java Multi-Threading - Full (chok)
PPTX
Thread model of java
PPT
PPT
Multithreading
 
PPT
PPTX
.NET Multithreading/Multitasking
ODP
Multithreading 101
PPT
Java Multithreading
Thread syncronization
.Net Threading
[Java concurrency]02.basic thread synchronization
C# Thread synchronization
[Java concurrency]01.thread management
Concurrency Programming in Java - 07 - High-level Concurrency objects, Lock O...
multithreading
Advanced Introduction to Java Multi-Threading - Full (chok)
Thread model of java
Multithreading
 
.NET Multithreading/Multitasking
Multithreading 101
Java Multithreading
Ad

Viewers also liked (19)

PDF
javathreads
PDF
Java threading
PDF
FM - JESSIE J
PDF
IGNITE_COMPANY_PROFILE
PDF
Market research-v2
PPTX
High speed disperser manufacturer in india
PDF
LA INVESTIGACIÓN ACCIÓN EDUCATIVA
PPTX
Java Thread & Multithreading
PPTX
Erosion and Deposition
PPTX
Java threads - part 3
PDF
Qué cambia en el modelo 190 en 2017
PDF
High–Performance Computing
PDF
Programming with Threads in Java
PDF
Java Course 10: Threads and Concurrency
javathreads
Java threading
FM - JESSIE J
IGNITE_COMPANY_PROFILE
Market research-v2
High speed disperser manufacturer in india
LA INVESTIGACIÓN ACCIÓN EDUCATIVA
Java Thread & Multithreading
Erosion and Deposition
Java threads - part 3
Qué cambia en el modelo 190 en 2017
High–Performance Computing
Programming with Threads in Java
Java Course 10: Threads and Concurrency
Ad

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

PPTX
Object-Oriented-Prog_MultiThreading.pptx
PPTX
Concurrency with java
PPTX
Concurrency with java
PPTX
Concurrency with java
PPTX
Concurrency with java
PPTX
Concurrency with java
PPTX
Concurrency with java
PPT
Java multithreading
PPTX
MULTI THREADING IN JAVA
PPTX
econtent thread in java.pptx
PPTX
U4 JAVA.pptx
PPTX
Multi-Threading in Java power point presenetation
PPTX
unit3 Exception Handling multithreadingppt.pptx
PPTX
unit3multithreadingppt-copy-180122162204.pptx
PDF
Multithreading Introduction and Lifecyle of thread
PPT
web programming-Multithreading concept in Java.ppt
PPTX
1.17 Thread in java.pptx
PDF
CSE 3146 M1- MULTI THREADING USING JAVA .pdf
PPTX
Multithreadingppt.pptx
PPTX
Multi threading
Object-Oriented-Prog_MultiThreading.pptx
Concurrency with java
Concurrency with java
Concurrency with java
Concurrency with java
Concurrency with java
Concurrency with java
Java multithreading
MULTI THREADING IN JAVA
econtent thread in java.pptx
U4 JAVA.pptx
Multi-Threading in Java power point presenetation
unit3 Exception Handling multithreadingppt.pptx
unit3multithreadingppt-copy-180122162204.pptx
Multithreading Introduction and Lifecyle of thread
web programming-Multithreading concept in Java.ppt
1.17 Thread in java.pptx
CSE 3146 M1- MULTI THREADING USING JAVA .pdf
Multithreadingppt.pptx
Multi threading

More from Sachintha Gunasena (16)

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

Recently uploaded (20)

PDF
Jenkins: An open-source automation server powering CI/CD Automation
PPTX
CRUISE TICKETING SYSTEM | CRUISE RESERVATION SOFTWARE
PDF
Comprehensive Salesforce Implementation Services.pdf
PDF
Multi-factor Authentication (MFA) requirement for Microsoft 365 Admin Center_...
PDF
How to Confidently Manage Project Budgets
PDF
Convert Thunderbird to Outlook into bulk
PDF
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
PDF
IEEE-CS Tech Predictions, SWEBOK and Quantum Software: Towards Q-SWEBOK
PDF
ShowUs: Pharo Stream Deck (ESUG 2025, Gdansk)
PDF
Perfecting Gamer’s Experiences with Performance Testing for Gaming Applicatio...
PPTX
ManageIQ - Sprint 268 Review - Slide Deck
PPTX
Benefits of DCCM for Genesys Contact Center
PDF
How to Choose the Most Effective Social Media Agency in Bangalore.pdf
PPTX
Introduction to Artificial Intelligence
PDF
Become an Agentblazer Champion Challenge Kickoff
PDF
Understanding NFT Marketplace Development_ Trends and Innovations.pdf
PDF
How to Migrate SBCGlobal Email to Yahoo Easily
PDF
QAware_Mario-Leander_Reimer_Architecting and Building a K8s-based AI Platform...
PDF
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
PPTX
Materi-Enum-and-Record-Data-Type (1).pptx
Jenkins: An open-source automation server powering CI/CD Automation
CRUISE TICKETING SYSTEM | CRUISE RESERVATION SOFTWARE
Comprehensive Salesforce Implementation Services.pdf
Multi-factor Authentication (MFA) requirement for Microsoft 365 Admin Center_...
How to Confidently Manage Project Budgets
Convert Thunderbird to Outlook into bulk
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
IEEE-CS Tech Predictions, SWEBOK and Quantum Software: Towards Q-SWEBOK
ShowUs: Pharo Stream Deck (ESUG 2025, Gdansk)
Perfecting Gamer’s Experiences with Performance Testing for Gaming Applicatio...
ManageIQ - Sprint 268 Review - Slide Deck
Benefits of DCCM for Genesys Contact Center
How to Choose the Most Effective Social Media Agency in Bangalore.pdf
Introduction to Artificial Intelligence
Become an Agentblazer Champion Challenge Kickoff
Understanding NFT Marketplace Development_ Trends and Innovations.pdf
How to Migrate SBCGlobal Email to Yahoo Easily
QAware_Mario-Leander_Reimer_Architecting and Building a K8s-based AI Platform...
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
Materi-Enum-and-Record-Data-Type (1).pptx

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
  • 41. Thank you. Sachintha Gunasena MBCS http://lk.linkedin.com/in/sachinthadtg