0% found this document useful (0 votes)
385 views34 pages

Umesh Multithreading

The document discusses multithreaded programming using Java threads. It defines what threads are and how they allow concurrent execution. It describes two methods for creating threads in Java - extending the Thread class or implementing the Runnable interface. The life cycle of a thread and examples of creating and running multiple threads are provided. Additional topics covered include thread priorities, common threading models like master-worker, and how multithreading can enable concurrency even on single-processor systems.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
385 views34 pages

Umesh Multithreading

The document discusses multithreaded programming using Java threads. It defines what threads are and how they allow concurrent execution. It describes two methods for creating threads in Java - extending the Thread class or implementing the Runnable interface. The life cycle of a thread and examples of creating and running multiple threads are provided. Additional topics covered include thread priorities, common threading models like master-worker, and how multithreading can enable concurrency even on single-processor systems.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 34

A

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

A single threaded program


class ABC
{
.
public void main(..)
{

..
}

begin

body
end

}
3

A Multithreaded Program
Main Thread

start

Thread A

start

Thread B

start

Thread C

Threads may switch or exchange data/results


4

Multithreaded Server: For Serving


Multiple Clients Concurrently

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

Modern Applications need Threads (ex1):


Editing and Printing documents in background.
Printing
Printing Thread
Thread

Editing
Editing
Thread
Thread

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

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

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();

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

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();

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

Life Cycle of Thread


new
start()
I/O completed

ready
Time expired/
interrupted

notify()

waiting
wait()

sleeping

resume()

blocked

dispatch

sleep()

running

suspend()
Block on I/O

completion

stop()

dead
14

Three threads example


class A extends Thread
{
public void run()
{
for(int i=1;i<=5;i++)
{
System.out.println("\t From ThreadA: i= "+i);
}
System.out.println("Exit from A");
}
}
class B extends Thread
{
public void run()
{
for(int j=1;j<=5;j++)
{
System.out.println("\t From ThreadB: j= "+j);
}
System.out.println("Exit from B");
}
}

15

class C extends Thread


{
public void run()
{
for(int k=1;k<=5;k++)
{
System.out.println("\t From ThreadC: k= "+k);
}
System.out.println("Exit from C");
}
}
class ThreadTest
{
public static void main(String args[])
{
new A().start();
new B().start();
new C().start();
}
}

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

MISD and MIMD Processing


19

Data Parallelism
Data

sort( int *array, int count)


//......
//......
pthread-t,
pthread-t,thread1,
thread1,thread2;
thread2;

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

Thread Priority Example


class A extends Thread
{
public void run()
{
System.out.println("Thread A started");
for(int i=1;i<=4;i++)
{
System.out.println("\t From ThreadA: i= "+i);
}
System.out.println("Exit from A");
}
}
class B extends Thread
{
public void run()
{
System.out.println("Thread B started");
for(int j=1;j<=4;j++)
{
System.out.println("\t From ThreadB: j= "+j);
}
System.out.println("Exit from B");
}
}

22

Thread Priority Example (Continue)


class C extends Thread
{
public void run()
{
System.out.println("Thread C started");
for(int k=1;k<=4;k++)
{
System.out.println("\t From ThreadC: k= "+k);
}
System.out.println("Exit from C");
}
}
class ThreadPriority
{
public static void main(String args[])
{
A threadA=new A();
B threadB=new B();
C threadC=new C();
threadC.setPriority(Thread.MAX_PRIORITY);
threadB.setPriority(threadA.getPriority()+1);
threadA.setPriority(Thread.MIN_PRIORITY);
System.out.println("Started Thread A");
threadA.start();
System.out.println("Started Thread B");
threadB.start();
System.out.println("Started Thread C");
threadC.start();
System.out.println("End of main thread");
}
}

23

Online Bank: Serving Many


Customers and Operations
PC client

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

The master/worker model


Program

Resources

Workers
taskX
taskX

Files
Databases

Master
Input (Stream)

main
main( () )

taskY
taskY

Disks
taskZ
taskZ

Special
Devices

28

The peer model


Program

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

Multi-Processing (clusters & grids)


and Multi-Threaded Computing
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
33

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy