multithreading in java
two or more parts run concurrently - multitasking
each part is called a thread
each thread defines separate part of execution
multitasking - process based or thread based
process based - runs 2 or more programs concurrently
thread based - in a single program, 2 or more tasks are
performed simultaneously
difference in process based and thread based
process based thread based
1) more overhead 1) less overhead
2) heavyweight tasks that require 2) lightweight tasks that can
their own address spaces share address spaces
3) context switching is
3) context switching is expensive
inexpensive
4) interprocess communication is 4) interthread communication is
expensive and limited inexpensive
5) not under control of java 5) under control of java
multithreading better than single thread environment -
allows efficient use of CPU keeping the idle time of CPU to
a minimum
thread priorities - decides when to switch from one running
thread to next
rules that decide context switching
voluntarily relinquishing control - yielding, sleeping
or blocking - highest priority thread is given to CPU
multithreading in java 1
preempted by higher priority thread - lower priority
thread that does not yield the processor is preempted by
higher priority thread - preemptive multitasking
multithreading is done using
thread class
runnable interface
methods defined by thread class
method meaning
getName() get a thread’s name
getPriority() get thread’s priority
isAlive() check if thread is running
join() wait for thread to terminate
run() entry point for thread
sleep() suspend thread for a period of time
start() start a thread
setName(String threadName) set thread’s name
main thread -
if a java program is started - main thread executes
immediately
child threads are spawned from here
last thread to finish execution - performs shutdown
actions
controlled using Thread object
call method currentThread() - public static member of
thread
syntax : static Thread currentThread()
multithreading in java 2
ways to start a thread using runnable interface
class MyThread
class MyThread
implements Runnable
implements Runnable
{
{
Thread t2;
public void run()
MyThread()
{
{
t2 = new Thread(this);
}
t2.start();
}
}
public void run()
class prg
{
{
}
public static void
}
main(String[]args)
class prg
{
{
MyThread t = new
public static void
MyThread();
main(String args[])
Thread t2 = new
{
Thread(t);
MyThread t = new
t2.start();
MyThread();
}
}
}
}
explanation : made a explanation : made a
class MyThread which class MyThread which
implements Runnable implements Runnable
interface - indicates interface - indicates
that instances of this that instances of this
class can be executed class can be executed
by a thread by a thread
public void run : Thread t2 declares
declares a method named variable t2 of type
run which is required Thread - used to store
to be implemented when reference to Thread
a class implements the object
runnable interface -
this method contains MyThread() -
the code to be executed constructor of MyThread
when thread is started class
in main class
multithreading in java 3
t2 = new Thread(this)
MyThread t = new creates a new Thread
MyThread() creates an object t2 and
instance of MyThread associates it with
class t which can be current instance of
used to start a new MyThread (which
thread explains use of this
keyword)
Thread t2 = new
Thread(t) - creates a t2.start() starts the
new Thread object t2 thread t2 - invokes run
and associates it with method of MyThread
MyThread instance t by instance
passing t as a
parameter to Thread() public void run :
constructor declares a method named
run which is required
t2.start() starts the to be implemented when
thread - invokes the a class implements the
run method of MyThread runnable interface -
instance t this method contains
the code to be executed
when thread is started
in main class
in main class,
MyThread t = new
MyThread() creates an
instance of MyThread
triggering constructor
and a new thread is
started
creating a thread
1) implementing runnable interface
to implement runnable, a class needs to implement run
method
inside run(), we define the code that constitutes the
new thread
run() establishes an entry point for another concurrent
thread of execution within our program
multithreading in java 4
then instantiate an object of type Thread from within
the class that implements runnable
2) extending thread class
extending class must override run() method - entry point
for new thread
must also call start() method to begin execution of new
thread
class NewThread extends Thread
{
NewThread()
{
super("Demo Thread");
System.out.println("Child thread: " +
this);
start(); // Start the thread
}
public void run()
{
try
{
for(int i = 5; i > 0; i--)
{
System.out.println("Child Thread: " + i);
Thread.sleep(500);
}
}
catch (InterruptedException e)
{
System.out.println("Child interrupted.");
}
System.out.println("Exiting child
thread.");
}
}
class ExtendThread
{
public static void main(String[] args)
{
new NewThread();
try
multithreading in java 5
{
for(int i=5; i>0; i—)
{
System.out.println(”Main thread: “+i);
Thread.sleep(500);
}
}
catch(InterruptedException e)
{
System.out.println(”Main thread
interrupted”);
}
System.out.println(”main thread exiting”);
}
}
super() inside NewThread invokes the Thread
constructor public Thread(String
threadName)
multithreading in java 6