8/9/2019 Multithreadinglec
1/33
Multithreading
8/9/2019 Multithreadinglec
2/33
Multithreading
Executing program with multiple threads in parallel.
Multithreaded program contains two or more parts that can run
concurrently. Each part is called a thread and each part defines a
separate path of execution.
Special form of multitasking
Multitasking
Process-based
Thread-based
8/9/2019 Multithreadinglec
3/33
Process
A program in execution
A computational unit in a system which uses the resources of
system when allocated by OS.
A heavy weight entity
Has own address space variables and data structure in memory.
May use file utility.
Kernel-level controlled entity.
Communicate via OS, files, networks.
May contain multiple threads.
8/9/2019 Multithreadinglec
4/33
Thread
Sequentially executed stream of instructions.
A light weight entity
Multiple threads of a process share common address space and data
of the process.
Does not use file utility.
process level controlled entity
8/9/2019 Multithreadinglec
5/33
Why Multithreading?
Problem solving
May have concurrent interacting components
Can handle each component using separate thread
Simplifies programming for problem
Example:
Web
Server
WEB BROWSER
WEB BROWSER
WEB BROWSER
Web server uses
threads to handle
various web-
browser
Multiple simultaneous
web browser requests
8/9/2019 Multithreadinglec
6/33
Contd..
Better utilization of resources
When a thread is delayed compute other thread
If extra hardware is given, compute threads in parallal
Reduce overall execution time
Example:
WEB BROWSER
WEB BROWSER
WebServer
Web
ServerWEB BROWSER
Multiple simultaneous
web browser requests Handled faster by multiple
Web Servers
8/9/2019 Multithreadinglec
7/33
Java ThreadsJavas multithreading is built upon Thread class and its companioninterface Runnable.
To create a thread
Implement Runnable or
Extend Thread
Specify the work we want the thread to do
Put the work in run( ) method
The Thread class
public class Thread{
public Thread (Runnable R);
public Thread (Runnable R, String name);--------------------------
public void run( );
}
8/9/2019 Multithreadinglec
8/33
More Thread class methods
public class Thread{
public String getName( ); //obtain a threads name
final boolean isAlive( ); //determine if the thread is still running
final void join( ); //wait for a thread to terminate
final void setName(String name); //change the internal name of thread
final void setPriority(int level); //set a threads priority
final int getPriority( ); //obtain the threads current priority
public static Thread currentThread( ); //obtain a reference to the thread in
// which it is called
public static void sleep(long milliseconds); //suspend a thread for a periodof time
8/9/2019 Multithreadinglec
9/33
The main thread
All Java programs have one thread : main thread.
It begins running immediately when a Java program starts up
It is important for two reasons:
It is the thread from which other child threads will be spawned.
Often it must be the last thread to finish its execution because it
performs various shutdown actions.
It is created automatically but can be controlled through a Thread
object
Obtain a reference of it by calling currentThread( ) method.
8/9/2019 Multithreadinglec
10/33
class currentThreadDemo{
public static void main(String args[]){
Thread t=Thread.currentThread( );
System.out.println(Current Thread: +t); //reference stored in local var. t
t.setName(MyThread); //change the name of the thread
System.out .println(After name change +t);
try{
for(int i=5;i>0;i--) { System.out.println(i);
Thread.sleep(1000);} //pausiing for one second;
}catch(InterruptedException e){System.out.println(Main Thread interrupted);}
}
An exception is thrown if
some other thread wantto interrupt the sleeping
thread
}
8/9/2019 Multithreadinglec
11/33
Creating a Thread using Runnable
interface To implement Runnable a class need only implement a singlemethod run( ) which defines the code that constitutes the new thread
run( ) can call other methods, use classes and declare variables just
like main thread can.
run( ) establishes an entry point for another concurrent thread ofexecution within our program. This thread will end when run( )
returns.
After implementing Runnable
instantiate an object of type thread within the class
Call the start method to start the new thread run( ) executes a call to run( )
8/9/2019 Multithreadinglec
12/33
class NewThread implements Runnable{
Thread t;NewThread( ){
t = new Thread(this,DemoThread); //Constructor of thread class toinstantiate Thread classSystem.out.println(Child thread: +t);
t.start( ); }
public void run( ){
try{
for(int i=5;i>0;i--) { System.out.println(Child Thread+i);Thread.sleep(500);}}catch(InterruptedException e){System.out.println(Child Thread interrupted);}
System.out.println(Exiting Child Thread);} }
class NewThreadDemo{
public static void main(String args[]){new NewThread( );try{
for(int i=1;i
8/9/2019 Multithreadinglec
13/33
Creating a thread by extending
Thread class Create a new class that extends Thread Create an instance of that class
The extending method must override the run method
Must also call start( ) method to begin execution of new thread
8/9/2019 Multithreadinglec
14/33
class NewThread extendsThread{
//Thread t; no need to create this reference as Thread is already being inheritedNewThread( ){
Super(DemoThread); //Constructor of super classThread is calledSystem.out.println(Child thread: +this);
start( ); }
public void run( ){
try{
for(int i=5;i>0;i--) { System.out.println(Child Thread+i);Thread.sleep(500);}}catch(InterruptedException e){System.out.println(Child Thread interrupted);}
System.out.println(Exiting Child Thread);} }
class NewThreadDemo{
public static void main(String args[]){new NewThread( );try{
for(int i=1;i
8/9/2019 Multithreadinglec
15/33
Extending Thread class (not
recommended) The Thread class defines several methods that can be overriddenby a subclass.
run( ) is the only one method that must be overridden, the same
method is required when we implement Runnable.
In general the classes should be extended only when they are beingenhanced or modified in some way. So if no otherThreads method
are overridden then implement Runnable.
8/9/2019 Multithreadinglec
16/33
Creating multiple threads
Our program can spawn as many threads as it needs.
Once started all threads share same CPU.
Example:- Two threads are created with different names.
8/9/2019 Multithreadinglec
17/33
class NewThread implements Runnable{
Thread t; String name;NewThread(String threadname ){ name=threadname;
t = new Thread(this,name); //Constructor of thread class toinstantiate Thread classSystem.out.println(New thread: +t);
t.start( ); }
public void run( ){
try{
for(int i=5;i>0;i--) { System.out.println(name+ : +i);Thread.sleep(1000);}}catch(InterruptedException e){System.out.println(name+ Thread interrupted)
System.out.println(name+ Exiting );} }
class MultiThreadDemo{
public static void main(String args[]){new NewThread(ONE ); new NewThread(SECOND );try{
for(int i=1;i
8/9/2019 Multithreadinglec
18/33
Synchronization
Websters: To represent or arrange events to indicate coincidence
or coexistence.
Lewis : To arrange events so that they occur in a specified order.
Serialized access to controlled resources.
Thread Synchronization
On shared memory : shared variables semaphores
On distributed memory:-
within a task semaphores
across the task by passing messages
8/9/2019 Multithreadinglec
19/33
Concurrent Execution
More than one thread exists in system at once
Can execute independently or in cooperation
Asynchronous execution
Threads generally independent
Must occasionally communicate or synchronize
Complex and difficult to manage such interactions
8/9/2019 Multithreadinglec
20/33
Atomic actions
An action which must be started and completed with no possibility of
interruption.
Amachine instruction could need to be atomic.(not all are!)
A lineofC code could need to be atomic.(not all are)
An entire database transaction could need to be atomic.
All Multiprocessing machines provide at least one complex atomic
instruction, from which you can build anything.
A section of code which you have forced to be atomic is a Critical
Section.
8/9/2019 Multithreadinglec
21/33
Critical Section
(BAD Programmer!)
Shared Data
T1 T2
Writer{
-------------------
}
Reader{
-------------------
Lock(DISK)
Unlock(DISK)
}
8/9/2019 Multithreadinglec
22/33
Critical Section
(Good Programmer!)
Shared Data
T1 T2
Writer{
-------------------
Lock(DISK)
Unlock(DISK)
}
Reader{
-------------------
Lock(DISK)
Unlock(DISK)
}
8/9/2019 Multithreadinglec
23/33
Lock Shared Data!
Globals
Shared data structures
Static variables
(really just lexically scoped global variables)
8/9/2019 Multithreadinglec
24/33
Mutual Exclusion
Problem of two threads accessing data simultaneously
Data can be put in inconsistent state
Context switch can occur at anytime, such as before a thread finishes
modifying value
Such data must be accessed in mutually exclusive way Only one thread allowed access at one time
Others must wait until resource is unlocked
Must be managed such that waiting time not unreasonable
8/9/2019 Multithreadinglec
25/33
Mutexes
Thread 1 Thread2
item = create_and_fill_item();
mutex_lock( &m );item->next = list;
list = item;
mutex_unlock(&m);
mutex_lock( &m );
this_item = list;
list = list_next;
mutex_unlock(&m);
8/9/2019 Multithreadinglec
26/33
Java Terminology
Key to synchronization is the concept of monitor(also called
semaphore)
A monitor is an object that is used as an mutually exclusive
lock(mutex).
Only one thread can own a monitor at a time. All the other threads attempting to enter the locked monitor will be
suspended until the first thread exits the monitor
Synchronization can be achieved in two ways.
Using synchronized methods
Using synchronized statement (block)
8/9/2019 Multithreadinglec
27/33
Producer/Consumer relationship
One thread creates data to store in shared object
Second thread reads data from that object
Large potential for data corruption if unsynchronized
8/9/2019 Multithreadinglec
28/33
Interthread Communication
In the synchronized Producer-Consumer problem-
suppose the producer has to wait until the consumer has consumedthe item before it generates more item
In polling system, the Consumer would waste many CPU cycleswhile it waited for Producer to produce.
Once the Producer has finished, it would start polling, wasting moreCPU cycles waiting for the consumer to finish and so on
To avoid polling, Java includes Iinterprocess communicationmechanism via
wait( ): tells the calling thread to give up the monitor until some other
thread enters the same monitor and calls notify( ) notify( ): wakes up the first thread that called wait( ) on the same object
notify( ): wakes up all the threads that called wait( ) on the sameobject. The highest priority thread will run first.
8/9/2019 Multithreadinglec
29/33
// an incorrect implementation of Producer Consumer
class Q{
int n;
Synchronized int get( ){
System.out.println(Got: +n);
return n;}
Synchronized void put(int n){
this.n = n;
System.out.println(Put: +n);
}
}
8/9/2019 Multithreadinglec
30/33
Class Producer implements Runnable{Q q;Producer(Q q){
this.q = q;
new Thread(this, Producer).start( ); }public void run( ){
int i = 0;
while(true) {q.put(i++); } } }
class Consumer implements Runnable{Q q;Consumer(Q q){this.q = q;
new Thread(this, Consumer).start( ); }public void run( ){
while(true) {
q.get( ); } } }class PC {public static void main(String args[ ]) {Q q = new Q( );
new Producer (q);
new Consumer (q);
System.out.println(Press Control+c to stop); } }
8/9/2019 Multithreadinglec
31/33
// a correct implementation of Producer Consumer
class Q{
int n;
boolean mutex = false;
synchronized int get( ){
if(!mutex)try{
wait( );}catch(InterruptedException e){ }
System.out.println(Got: + n);
mutex = false;notify( );
return n; }
synchronized void put(int n){if(mutex)try{wait( );}catch(InterruptedException e) { }
this.n = n;
mutex = trueSystem.out.println(Put: +n);
notify( ); } }
8/9/2019 Multithreadinglec
32/33
Class Producer implements Runnable{Q q;Producer(Q q){
this.q = q;
new Thread(this, Producer).start( ); }public void run( ){
int i = 0;
while(true) {q.put(i++); } } }
class Consumer implements Runnable{Q q;Consumer(Q q){this.q = q;
new Thread(this, Consumer).start( ); }public void run( ){
while(true) {
q.get( ); } } }class PC {public static void main(String args[ ]) {Q q = new Q( );
new Producer (q);
new Consumer (q);
System.out.println(Press Control+c to stop); } }
8/9/2019 Multithreadinglec
33/33
Synchronization Variables in
Shared Memory (Cross Process)
SynchronizationVariable
Thread
Process 1S
Process 2Shared Memory
S
S
S