4.1 Multithreading
4.1 Multithreading
1
What are Threads?
Hardware
Context Local state
Global/ shared state
PC
Registers
Registers
Status
StatusWord
Word
Hard/Software Context
Program
ProgramCounter
Counter
3
OS:
Multi-Processing, Multi-
Threaded
Threaded Libraries, Multi-threaded I/O
Application
Application Application
Application
CPU
CPU
CPU CPU CPU CPU
THREAD
THREAD
STACK
STACK
SHARED
SHARED
MEMOR
MEMOR
YY
THREAD
THREAD
DATA
DATA
Threads within a process THREAD
THREAD
TEXT
TEXT
Independent executables
All threads are parts of a process hence communication
easier and simpler. 5
Levels
Levels ofof
Parallelism:
Parallelism:
Thread
Thread Granularity
Granularity
Code- Code-
Granularity
Granularity
Task
Taski-l
i-l Task
Taskii Task
Taski+1
i+1 Code
CodeItem
Item
Large
Largegrain
grain
(task
(tasklevel)
level)
Program
Program
func1
func1( () ) func2
func2( () ) func3
func3( () )
Task
Task
{{
....
{{
....
{{
.... Medium
.... .... .... Mediumgrain
grain
Control
Control
....
}}
.... ....
}}
.... ....
}}
.... (control
(controllevel)
level)
Data
Data Function
Function(thread)
(thread)
Multiple
MultipleIssue
Issue
aa( (00) )=.. aa( (11)=.. aa( (22)=.. Fine
=..
bb( (00) )=..
)=..
bb( (11)=..
)=..
bb( (22)=.. Finegrain
grain
=.. )=.. )=.. (data
(datalevel)
level)
Loop
Loop
++ xx Load
Load Very
Veryfine
finegrain
grain
(multiple
(multipleissue)
issue)
With hardware6
Multithreading
Multithreading --
Uniprocessors
Uniprocessors
Concurrency Vs
Concurrency Vs Parallelism
Parallelism
Concurrency
Concurrency
P1
P1
P2
P2 CPU
P3
P3
tim
tim
ee
Number
Number of
of Simulatneous
Simulatneous execution
execution units
units >
> no
no
of
of CPUs
CPUs 7
Multithreading
Multithreading --
Multiprocessors
Multiprocessors
Concurrency
Concurrency Vs
Vs Parallelism
Parallelism
CPU
P1
P1
CPU
P2
P2
CPU
P3
P3
tim
tim
ee
No
No of
of execution
execution process
process =
= no
no of
of CPUs
CPUs
8
Threads
9
Threading Mechanisms...
Create a class that extends the Thread class
Create a class that implements the Runnable
interface
10
1st method: Extending Thread
class
1st Method: Extending the Thread class
class MyThread extends Thread
{
public void run()
{
// thread body of execution
}
}
Creating thread:
MyThread thr1 = new MyThread();
Start Execution:
thr1.start();
11
2nd method: Threads by implementing
Runnable interface
class ClassName implements Runnable
{
.....
public void run()
{
// thread body of execution
}
}
Creating Object:
thr1.start();
12
Multi-Threaded
13
Manipulation of Current Thread
// CurrentThreadDemo.java
class CurrentThreadDemo {
public static void main(String arg[]) {
Thread ct = Thread.currentThread();
ct.setName( "My Thread" );
System.out.println("Current Thread : "+ct);
try {
for(int i=5; i>0; i--) {
System.out.println(" " + i);
Thread.sleep(1000);
}
}
catch(InterruptedException e) {
System.out.println("Interrupted."); }
}
}
Run:
Current Thread : Thread[My Thread,5,main]
5
4
3
2
1
14
Creating new Thread...
// ThreadDemo.java
class ThreadDemo implements Runnable
{
ThreadDemo()
{
Thread ct = Thread.currentThread();
System.out.println("Current Thread : "+ct);
Thread t = new Thread(this,"Demo Thread");
t.start();
try
{
Thread.sleep(3000);
}
catch(InterruptedException e)
{
System.out.println("Interrupted.");
}
System.out.println("Exiting main thread.");
}
15
...Creating new Thread.
16
Thread Priority...
// HiLoPri.java
class Clicker implements Runnable {
int click = 0;
private Thread t;
private boolean running = true;
public Clicker(int p)
{
t = new Thread(this);
t.setPriority(p);
}
public void run()
{
while(running)
click++;
}
public void start()
{ t.start(); }
public void stop()
{ running = false; }
}
17
...Thread Priority
class HiLoPri
{
public static void main(String args[])
{
Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
Clicker Hi = new Clicker(Thread.NORM_PRIORITY+2);
Clicker Lo = new Clicker(Thread.NORM_PRIORITY-2);
Lo.start();
Hi.start();
try { Thread.sleep(10000); }
catch (Exception e) { }
Lo.stop();
Hi.stop();
System.out.println(Lo.click + " vs. " + Hi.click);
}
}
Run1: (on Solaris)
0 vs. 956228
Run2: (Window 95)
304300 vs. 4066666
18
Threads Need
Synchronization
19
Monitor model (for
Syncronisation)
Method 1
Method 2
Key
Block 1
Threads
21
...Threads Synchronisation.
23
Queue (no inter-threaded
communication)...
24
...Queue (no inter-threaded
communication).
class PC
{
public static void main(String[] args)
{
Queue Q = new Queue();
new Producer(Q);
new Consumer(Q);
}
}
Run:
Put: 1
Got: 1
Got: 1
Got: 1
Put: 2
Put: 3
Got: 3
^C
25
Queue (interthread communication)...
27
Queue (interthread communication)...
28
...Queue (no interthread
communication).
class PCnew
{
public static void main(String[] args)
{
Queue Q = new Queue();
new Producer(Q);
new Consumer(Q);
}
}
Run:
Put : 0
Got : 0
Put : 1
Got : 1
Put : 2
Got : 2
Put : 3
Got : 3
Put : 4
Got : 4
^C
29
Deadlock...
// DeadLock.java
class A
{
synchronized void foo(B b)
{
String name = Thread.currentThread().getName();
System.out.println(name + " entered A.foo");
try
{
Thread.sleep(1000);
}
catch(Exception e)
{
}
System.out.println(name + " trying to call B.last()");
b.last();
}
synchronized void last()
{
System.out.println("Inside A.last");
}
}
30
Deadlock...
class B
{
synchronized void bar(A a)
{
String name = Thread.currentThread().getName();
System.out.println(name + " entered B.bar");
try
{
Thread.sleep(1000);
}
catch(Exception e)
{
}
System.out.println(name + " trying to call A.last()");
a.last();
}
31
...Deadlock.
32
Threads
Threads in
in Action...
Action...
Cooperative
Cooperative threads
threads -- File
File
Copy
Copy
reader()
reader()
{{ writer()
writer()
-- -- -- -- -- -- -- -- -- buff[0] {{
-- buff[0]
-- -- -- -- -- -- -- -- -- --
lock(buff[i]);
lock(buff[i]); lock(buff[i]);
buff[1] lock(buff[i]);
read(src,buff[i]);
read(src,buff[i]); buff[1] write(src,buff[i]);
write(src,buff[i]);
unlock(buff[i]);
unlock(buff[i]); unlock(buff[i]);
unlock(buff[i]);
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
-- }}
}}
Cooperative
Cooperative Parallel
Parallel
Synchronized
Synchronized Threads
Threads
33
Threads in Action...
Multithreaded Server
Server Process
Client
Process Server
Threads
Client Process
User Mode
Kernel Mode
Message Passing
Facility
34