Multithreaded Programming
using Java Threads
Prof. Rajkumar Buyya
Cloud Computing and Distributed Systems (CLOUDS) Laboratory
Dept. of Computer Science and Software Engineering
University of Melbourne, Australia
http://www.cloudbus.org/~raj or http://www.buyya.com
1
Agenda
Introduction
Thread Applications
Defining Threads
Java Threads and States
Priorities
Accessing Shared Resources
Synchronisation
Assignment 1:
Multi-Threaded Math Server
Advanced Issues:
Concurrency Models: master/worker, pipeline, peer processing
Multithreading Vs multiprocessing
2
A single threaded program
class ABC
{
….
public void main(..) begin
{
… body
..
end
}
}
3
A Multithreaded Program
Main Thread
start
start start
Thread A Thread B Thread C
Threads may switch or exchange data/results
4
Single and Multithreaded
Processes
threads are light-weight processes within a process
Single-threaded Process Multiplethreaded Process
Threads of
Execution
Single instruction stream Multiple instruction stream
Common
Address Space
5
Multithreaded Server: For Serving
Multiple Clients Concurrently
Client 1 Process Server Process
Server
Threads
Internet
Client 2 Process
6
Web/Internet Applications:
Serving Many Users Simultaneously
PC client
Internet
Server
Local Area Network
PD
A
7
Modern Applications need Threads (ex1):
Editing and Printing documents in background.
Printing Thread
Editing Thread
8
Multithreaded/Parallel File Copy
reader()
{ writer()
- - - - - - - - - buff[0] {
- - - - - - - - - - -
lock(buff[i]); lock(buff[i]);
read(src,buff[i]); buff[1]
write(src,buff[i]);
unlock(buff[i]); unlock(buff[i]);
- - - - - - - - - - - - - - - - - - -
- }
}
Cooperative Parallel Synchronized
Threads
9
Levels of Parallelism
Code-Granularity
Code Item
Sockets/ Task i-l Task i Task i+1 Large grain
PVM/MPI (task level)
Program
func1 ( ) func2 ( ) func3 ( )
{ { { Medium grain
.... ....
Threads .... ....
....
....
(control level)
} } }
Function (thread)
Fine grain
a ( 0 ) =.. a ( 1 )=.. a ( 2 )=.. (data level)
Compilers b ( 0 ) =.. b ( 1 )=.. b ( 2 )=..
Loop (Compiler)
Very fine grain
CPU + x Load (multiple issue)
With hardware
10
What are Threads?
A piece of code that run in concurrent with
other threads.
Each thread is a statically ordered sequence of
instructions.
Threads are being extensively used express
concurrency on both single and
multiprocessors machines.
Programming a task having multiple threads of
control – Multithreading or Multithreaded
Programming.
11
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
Java Garbage Collector is a low-priority thread.
12
Threading Mechanisms...
Create a class that extends the Thread class
Create a class that implements the Runnable
interface
Thread Runnable Thread
MyThread MyClass
(objects are threads) (objects with run() body)
[a] [b]
13
1st method: Extending Thread
class
Create a class by extending Thread class and override
run() method:
class MyThread extends Thread
{
public void run()
{
// thread body of execution
}
}
Create a thread:
MyThread thr1 = new MyThread();
Start Execution of threads:
thr1.start();
Create and Execute:
new MyThread().start();
14
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();
}
}
15
2nd method: Threads by
implementing Runnable interface
Create a class that implements the interface Runnable and
override run() method:
class MyThread implements Runnable
{
.....
public void run()
{
// thread body of execution
}
}
Creating Object:
MyThread myObject = new MyThread();
Creating Thread Object:
Thread thr1 = new Thread( myObject );
Start Execution:
thr1.start();
16
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();
}
}
17
Life Cycle of Thread
new
start()
I/O completed
ready
Time expired/ resume()
notify() interrupted
sleeping blocked
waiting
dispatch
sleep()
wait() suspend()
running Block on I/O
completion
stop() dead
18
A Program with Three Java Threads
Write a program that creates 3 threads
19
Three threads example
class A extends Thread
{
public void run()
{
for(int i=1;i number of CPUs
44
Multi-Processing (clusters & grids)
and Multi-Threaded Computing
Threaded Libraries, Multi-threaded I/O
Application
Application Application
Application
CPU
CPU
CPU CPU CPU CPU
Better Response Times in Higher Throughput for
Multiple Application Parallelizeable Applications
Environments
45
References
Rajkumar Buyya, Thamarai Selvi,
Xingchen Chu, Mastering OOP with
Java, McGraw Hill (I) Press, New Delhi,
India, 2009.
Sun Java Tutorial – Concurrency:
http://java.sun.com/docs/books/tutorial/esse
ntial/concurrency/
46