Java Multi-threading programming

DrRajeshreeKhande 781 views 15 slides May 16, 2020
Slide 1
Slide 1 of 15
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15

About This Presentation

Multithreading allows concurrent execution of two or more process or threads of a program for maximum utilization of CPU and increase the throughput


Slide Content

Multithreading Programming
Multithreading is conceptual programming where a
program (processes) are divided into two or more
subprograms (processes).
A thread is similar to program that has a single flow of
control
It has beginning , body , an end and executes command
Sequentially
Every program will have at least one thread.
1 RAJESHREE KHANDE

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.
2 RAJESHREE KHANDE

A single threaded program
class ABC
{
….
public void main(..)
{

..
}
}
begin
body
end
3 RAJESHREE KHANDE

A Multithreaded Program
Main Thread
Thread A Thread B Thread C
start
start
start
Threads may switch or exchange data/results
Main Method
Module
Switching
Switching
4 RAJESHREE KHANDE

Printing Thread
Editing Thread
Modern Applications need Threads (ex1):
Editing and Printing documents in background.
5 RAJESHREE KHANDE

Thread Class
Multithreading System built upon Threadclass it’s method,
it’s interface Runnable.
To create a new Thread , either extends Threador
implement the Runnable interface
Thread class defines several Methods. Some of the method
are
1) getName():Obtain a thread Name.
6 RAJESHREE KHANDE

Thread Class
2) getPriority() :Obtain a Thread Priority
3) isAlive() :Determine if the thread is still running
4) join() :Wait for thread to terminate.
5) run() :Entry point for the thread.
6) sleep() :Suspend a thread for a period of time.
7) start() :Start a thread by calling it’s run()
method
7 RAJESHREE KHANDE

Creating Thread
Java threads may be created by:
1. Extending Thread class
2. Implementing the Runnable interface
Java threads are managed by the JVM.
8 RAJESHREE KHANDE

1. Extending Thread class
Declare a classas extending the Threadclass
Create instance of that class
This class must override the run()method which is
entry point for the new thread.
It must also call start()to begin the execution of new
thread.
9 RAJESHREE KHANDE

Extending Thread class
Syntax
class MyThread extends Thread
{
public void run()
{
// thread body of execution
}
}
10 RAJESHREE KHANDE

Create a thread:
MyThread thr1 = new MyThread();
Start Execution of threads:
thr1.start();
Create and Execute:
new MyThread().start();
1.Extending Thread class
11 RAJESHREE KHANDE

The Main Thread
When Java Program start up one thread begins
immediately called Main Thread.
It can be control through a object Thread
For this obtain a reference to it by calling the method
currentThread()which is public staticmember of Thread
It’s General Form
static Thread CurrentThread()
12 RAJESHREE KHANDE

Life Cycle of Thread
13
new
runnable non-runnable
dead
wait()
sleep()
suspend()
blocked
notify()
slept
resume()
unblocked
start()
stop()
RAJESHREE KHANDE

2: Threads by implementing Runnable interface
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();
14 RAJESHREE KHANDE

An example
15
class MyThread implements Runnable
{
public void run()
{
System.out.println(" this thread is running ... ");
}
} // end class MyThread
class ThreadEx2
{
public static void main(String [] args ) {
Thread t = new Thread(new MyThread());
// due to implementing the Runnable interface
// I can call start(), and this will call run().
t.start();
} // end main()
} // end class ThreadEx2
RAJESHREE KHANDE