You are on page 1of 26

Parallel Programming Paradigms

Multithreading
Task level parallelism
By
Apex TG India Pvt Ltd
http://www.apextgi.in
1

Serial Vs. Parallel


COUNTER 2

COUNTER
COUNTER 1

Q
Please

Single and Multithreaded


Processes
Single-threaded Process

Multiplethreaded
Threads of Process
Execution

Multiple instruction stream


Single instruction stream Common
Address Space
3

OS:
Multi-Processing, Multi-Threaded
Threaded Libraries, Multi-threaded I/O
Application
Application

Application

Application

CPU
CPU
CPU
Better Response Times in
Multiple Application
Environments

CPU

CPU

CPU

Higher Throughput for


Parallelizeable Applications

Multi-threading, continued...
Multi-threaded OS enables parallel, scalable I/O
Application

Application

Application

OS Kernel

CPU

CPU

CPU

Multiple, independent I/O


requests can be satisfied
simultaneously because all the
major disk, tape, and network
drivers have been multithreaded, allowing any given
driver to run on multiple
CPUs simultaneously.

Basic Process Model

STACK

DATA
DATA
TEXT
TEXT

processes
processes

STACK
Shared
Shared
memory
memory
segments,
segments,
pipes,
pipes,open
open
files
filesor
or
mmapd
mmapd
files
files

Shared
SharedMemory
Memory
maintained
maintainedby
bykernel
kernel

DATA
DATA
TEXT
TEXT

processes
processes
6

What are Threads?


Thread is a piece of code that can execute in

concurrence with other threads.


It is a schedule entity on a processor
Hardware
Context

Registers
Registers
Status
StatusWord
Word

Local state
Global/ shared state
PC
Hard/Software Context

Program
ProgramCounter
Counter

Running

Thread Object
7

Threaded Process Model

THREAD
THREAD
STACK
STACK
SHARED
SHARED
MEMORY
MEMORY

Threads within a process

THREAD
THREAD
DATA
DATA
THREAD
THREAD
TEXT
TEXT

Independent executables
All threads are parts of a process hence communication
easier and simpler.

Levels
Levels of
of Parallelism
Parallelism
Task
Taski-l
i-l

func1
func1( () )
{{
....
....
....
....
}}

Task
Task

Control
Control

Data
Data

MultipleIssue
Issue
Multiple

aa( (00) )=..


=..
bb( (00) )=..
=..

++

Task
Taskii

func2
func2( () )
{{
....
....
....
....
}}

aa( (11)=..
)=..
bb( (11)=..
)=..

xx

Task
Taski+1
i+1

func3
func3( () )
{{
....
....
....
....
}}

aa( (22)=..
)=..
bb( (22)=..
)=..

Load
Load

Code-Granularity
Code-Granularity
Code
CodeItem
Item
Large
Largegrain
grain
(task
(tasklevel)
level)
Program
Program
Medium
Mediumgrain
grain
(control
(controllevel)
level)
Function
Function(thread)
(thread)
Fine
Finegrain
grain
(data
(datalevel)
level)
Loop
Loop
Very
Veryfine
finegrain
grain
(multiple
(multipleissue)
issue)
With
Withhardware
hardware

Java

Multithreading in Java

10

Java - An Introduction

Java - The new programming language from Sun


Microsystems
Java -Allows anyone to publish a web page with
Java code in it
Java - CPU Independent language
Created for consumer electronics
Java - James , Arthur Van , and others
Java -The name that survived a patent search
Oak -The predecessor of Java
Java is C++ -- ++
11

Object Oriented Languages


-A comparison
Feature
Encapsulation
Inheritance
Multiple Inherit.
Polymorphism
Binding (Early or Late)
Concurrency
Garbage Collection
Genericity
Class Libraries

C++
Yes
Yes
Yes
Yes
Both
Poor
No
Yes
Yes

Objective
C
Yes
Yes
Yes
Yes
Both
Poor
Yes
No
Yes

Ada
Yes
No
No
Yes
Early
Difficult
No
Yes
Limited

Java
Yes
Yes
No
Yes
Late
Yes
Yes
No
Yes

12

Sun defines Java as:

Simple and Powerful


Safe
Object Oriented
Robust
Architecture Neutral and Portable
Interpreted and High Performance
Threaded
Dynamic

13

Java Integrates
Power of Compiled Languages
and
Flexibility of Interpreted Languages

14

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
15

Ways of Multithreading in Java

Create a class that extends the Thread class


Create a class that implements the Runnable interface

1st Method: Extending the Thread class

class MyThread
{
public void
{
// thread
}
}
Creating thread:
MyThread thr1 =
Start Execution:
thr1.start();

extends Thread
run()
body of execution

new MyThread();

16

2nd method: Threads by implementing


Runnable interface
class ClassName implements Runnable
{
.....
public void run()
{
// thread body of execution
}
}
Creating Object:
ClassName myObject = new ClassName();
Creating Thread Object:
Thread thr1 = new Thread( myObject );
Start Execution:
thr1.start();

17

Thread Class Members...


public class java.lang.Thread extends java.lang.Object
implements java.lang.Runnable
{
// Fields
public final static int MAX_PRIORITY;
public final static int MIN_PRIORITY;
public final static int NORM_PRIORITY;
// Constructors
public Thread();
public Thread(Runnable target);
public Thread(Runnable target, String name);
public Thread(String name);
public Thread(ThreadGroup group, Runnable target);
public Thread(ThreadGroup group, Runnable target, String
public Thread(ThreadGroup group, String name);
// Methods
public static int activeCount();
public void checkAccess();
public int countStackFrames();
public static Thread currentThread();
public void destroy();
public static void dumpStack();
public static int enumerate(Thread tarray[]);
public final String getName();

name);

18

...Thread Class Members.


public
public
public
public
public
public
public
public
public
public
public
public
public
public
public
public
public
public
public
public
public
public
public
}

final int getPriority(); // 1 to 10 priority-pre-emption at mid.


final ThreadGroup getThreadGroup();
void interrupt();
static boolean interrupted();
final boolean isAlive();
final boolean isDaemon();
boolean isInterrupted();
final void join();
final void join(long millis);
final void join(long millis, int nanos);
final void resume();
void run();
final void setDaemon(boolean on);
final void setName(String name);
final void setPriority(int newPriority);
static void sleep(long millis);
static void sleep(long millis, int nanos);
void start();
final void stop();
final void stop(Throwable obj);
final void suspend();
String toString();
static void yield();

19

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

20

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.");
}

21

...Creating new Thread.


public void run()
{
try
{
for(int i=5; i>0; i--)
{
System.out.println(" " + i);
Thread.sleep(1000);
}
}
catch(InterruptedException e)
{
System.out.println("Child interrupted.");
}
System.out.println("Exiting child thread.");
}
public static void main(String args[]) {
new ThreadDemo();
}
}
Run:
Current Thread : Thread[main,5,main]
5
4
3
Exiting main thread.
2
1
Exiting child thread.

22

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;
}
}

23

...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

24

The Java monitor model


Method 1
Method 2
Key
Block 1
Threads
Monitor (synchronised) solves race-condition problem
25

26

You might also like