The document discusses multithreading programming, defining threads as concurrent pieces of code that execute sequentially within a program. It explains how to create threads in Java by either extending the Thread class or implementing the Runnable interface, detailing methods such as start() and run(). Additionally, it outlines the lifecycle of a thread and gives examples of basic thread creation and execution.
Multithreading Programming
Multithreadingis 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
2.
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
3.
A single threadedprogram
class ABC
{
….
public void main(..)
{
…
..
}
}
begin
body
end
3 RAJESHREE KHANDE
4.
A Multithreaded Program
MainThread
Thread A Thread B Thread C
start start
start
Threads may switch or exchange data/results
Main Method
Module
SwitchingSwitching
4 RAJESHREE KHANDE
Thread Class
MultithreadingSystem built upon Thread class it’s method,
it’s interface Runnable.
To create a new Thread , either extends Thread or
implement the Runnable interface
Thread class defines several Methods. Some of the method
are
1) getName() : Obtain a thread Name.
6 RAJESHREE KHANDE
7.
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
8.
Creating Thread
Javathreads may be created by:
1. Extending Thread class
2. Implementing the Runnable interface
Java threads are managed by the JVM.
8 RAJESHREE KHANDE
9.
1. Extending Threadclass
Declare a class as extending the Thread class
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
10.
Extending Thread class
Syntax
class MyThread extends Thread
{
public void run()
{
// thread body of execution
}
}
10 RAJESHREE KHANDE
11.
Create athread:
MyThread thr1 = new MyThread();
Start Execution of threads:
thr1.start();
Create and Execute:
new MyThread().start();
1.Extending Thread class
11 RAJESHREE KHANDE
12.
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 static member of Thread
It’s General Form
static Thread CurrentThread()
12 RAJESHREE KHANDE
13.
Life Cycle ofThread
13
new
runnable non-runnable
dead
wait()
sleep()
suspend()
blocked
notify()
slept
resume()
unblocked
start()
stop()
RAJESHREE KHANDE
14.
2: Threads byimplementing 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
15.
An example
15
class MyThreadimplements 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