Umesh Multithreading
Umesh Multithreading
SEMINAR ON
MULTITHREADED
PROGRAMMING USING
GUIDED BY:
JAVA THREADS
SUBMITTED TO:
MISS.A.A.KUJUR
H.O.D. I.T. DEPT.
I.G.E.C. SAGAR
MR RAJESH
AHIRWAR
I.T. DEPT.
SUBMITTED BYI.G.E.C. SAGAR
UMESH KUMAR PRAJAPATI
0601IT091055
V SEM. I.T.
CONTENTS
Introduction
Thread Applications
Defining Threads
Java Threads and States
Priorities
Advanced Issues:
Concurrency Models: master/worker,
pipeline, peer processing
Multithreading Vs multiprocessing
2
..
}
begin
body
end
}
3
A Multithreaded Program
Main Thread
start
Thread A
start
Thread B
start
Thread C
Server Process
Client 1
Process
Server
Threads
Internet
Client 2
Process
Web/Internet Applications:
Serving Many Users
Simultaneously
PC client
Internet
Server
Local Area Network
PDA
6
Editing
Editing
Thread
Thread
Java Threads
Java has built in thread support for
Multithreading
Synchronization
Thread Scheduling
Inter-Thread Communication:
currentThread start setPriority
yield run
getPriority
sleep stop suspend
resume
Create a thread:
MyThread thr1 = new MyThread();
Start Execution of threads:
thr1.start();
Create and Execute:
new MyThread().start();
10
An example
class MyThread extends Thread {
public void run() {
System.out.println(" this thread is running ... ");
}
}
class ThreadEx1 {
public static void main(String [] args ) {
MyThread t = new MyThread();
t.start();
}
}
11
12
An example
class MyThread implements Runnable {
public void run() {
System.out.println(" this thread is running ... ");
}
}
class ThreadEx2 {
public static void main(String [] args ) {
Thread t = new Thread(new MyThread());
t.start();
}
}
13
ready
Time expired/
interrupted
notify()
waiting
wait()
sleeping
resume()
blocked
dispatch
sleep()
running
suspend()
Block on I/O
completion
stop()
dead
14
15
16
Run 1
[raj@mundroo] threads [1:76] java ThreadTest
From ThreadA: i= 1
From ThreadA: i= 2
From ThreadA: i= 3
From ThreadA: i= 4
From ThreadA: i= 5
Exit from A
From ThreadC: k= 1
From ThreadC: k= 2
From ThreadC: k= 3
From ThreadC: k= 4
From ThreadC: k= 5
Exit from C
From ThreadB: j= 1
From ThreadB: j= 2
From ThreadB: j= 3
From ThreadB: j= 4
From ThreadB: j= 5
Exit from B
17
Run2
[raj@mundroo] threads [1:77] java ThreadTest
From ThreadA: i= 1
From ThreadA: i= 2
From ThreadA: i= 3
From ThreadA: i= 4
From ThreadA: i= 5
From ThreadC: k= 1
From ThreadC: k= 2
From ThreadC: k= 3
From ThreadC: k= 4
From ThreadC: k= 5
Exit from C
From ThreadB: j= 1
From ThreadB: j= 2
From ThreadB: j= 3
From ThreadB: j= 4
From ThreadB: j= 5
Exit from B
Exit from A
18
Process Parallelism
int add (int a, int b, int & result)
// function stuff
int sub(int a, int b, int & result)
// function stuff
Data
IS1
pthread
pthreadt1,
t1,t2;
t2;
pthread-create(&t1,
pthread-create(&t1,add,
add,a,b,
a,b,&&r1);
r1);
pthread-create(&t2,
pthread-create(&t2,sub,
sub,c,d,
c,d,&&r2);
r2);
pthread-par
pthread-par(2,
(2,t1,
t1,t2);
t2);
Processor
add
add
Processor
IS2
sub
sub
aa
bb
r1
r1
cc
dd
r2
r2
Data Parallelism
Data
pthread-create(&
pthread-create(&thread1,
thread1,sort,
sort,array,
array,N/2);
N/2);
pthread-create(&
pthread-create(&thread2,
thread2,sort,
sort,array,
array,N/2);
N/2);
pthread-par(2,
thread1,
thread2);
pthread-par(2, thread1, thread2);
Processor
Sort
Sort
IS
Processor
Sort
Sort
SIMD Processing
do
dn/2
dn2/+1
dn
20
Thread Priority
In Java, each thread is assigned priority,
which affects the order in which it is
scheduled for running. The threads so far
had same default priority
(NORM_PRIORITY) and they are served
using FCFS policy.
Java allows users to change priority:
ThreadName.setPriority(intNumber)
MIN_PRIORITY = 1
NORM_PRIORITY=5
MAX_PRIORITY=10
21
22
23
Internet Bank
Server
Local Area Network
Bank
Database
PDA
24
Multithreaded Server
Multithreaded Server
Client
Process
Server Process
Server
Threads
Client Process
User Mode
Kernel Mode
Message Passing
Facility
25
A Multithreaded Program
MathThreads
start
MathSin
start
start
MathCos
join
join
MathTan
join
MathThreads
26
Thread Programming
models
Thread concurrency/operation
models
The master/worker model
The peer model
A thread pipeline
27
Resources
Workers
taskX
taskX
Files
Databases
Master
Input (Stream)
main
main( () )
taskY
taskY
Disks
taskZ
taskZ
Special
Devices
28
Input
Input
Resources
Workers
taskX
taskX
Files
Databases
taskY
taskY
Disks
taskZ
taskZ
Special
Devices
29
A THREAD PIPELINE
Program
Filter Threads
Stage
Stage11
Stage
Stage22
Stage
Stage33
Input (Stream)
Resources
Files
Files
Files
Databases
Databases
Databases
Disks
Special Devices
Disks
Special Devices
Disks
Special Devices
30
Multithreading - Multiprocessors
Process
Process Parallelism
Parallelism
CPU
P1
P1
P2
P2
CPU
P3
P3
CPU
tim
tim
ee
No
No of
of execution
execution processes
processes <=
<= the
the
number
number of
of CPUs
CPUs
31
Multithreading on Uni-processor
Concurrency Vs Parallelism
Process
ProcessConcurrency
Concurrency
P1
P1
P2
P2
CPU
P3
P3
tim
tim
ee
Number
Number of
of Simultaneous
Simultaneous execution
execution units
units >
>
number
number of
of CPUs
CPUs
32
Application
Application
CPU
CPU
CPU
Better Response Times in
Multiple Application
Environments
CPU
CPU
CPU