Multithreaded
programming
Executing several programs
simultaneously
A thread is a program with single
flow of control
Java supports multithreading.
Single threaded program
Multithreaded program
A program that contains multiple flow of control is called multithreading. here there are four threads with one main and three others.The main thread is designed to create and start the other 3 threads.Once initiated by the main thread, the other threads run concurrently and share resources .These threads are called lightweight thread
Creating threads
Are implemented in the form of object that contains a method
called run()
The run method is invoked by initiating the start() method.
A new thread can be created by
By extending the Thread Class
By implementing the Runnable
Interface
Extending the Thread
Class
By extending the class java.lang.Thread
It includes
Declare the class as extending the
thread class
Implement the run() method
Create a thread object and call the
start()
Declaring the class
Implementing the run() Method
Starting the new thread
Program
Stopping and blocking a thread
Stopping a thread
◦ By calling stop() method
◦ Causes the thread moves to dead state
◦ Also moves to dead state at the end
Blocking a thread
◦ Temporarily suspending or blocking
Life cycle of a thread
New born state
Runnable state
Running state
Blocked state
Dead state
A thread is
always in one of these five states
Transition
diagram of a thread
New born state
When we create a thread, it is born
and is in new born state
The thread is not yet scheduled for
running
◦ Can be scheduled for running by
start() method
◦ Kill it by stop() method
Runnable state
Thread is ready for execution and
waiting for availability
Will be having a queue of threads
waiting
Given timeslots RR fashion if no
priority
Relinquish control to another thread
is done by yield()method
Running state
Processor has given its time to the
thread of execution
A running thread may relinquish its
control when
◦ It has been suspended using suspend()
-Revived by resume()
◦ It has made to sleep using the sleep(time)
◦ it has hold to wait until some event
to occur -Done by using wait() and notify()
Blocked state
When it is prevented from entering
into runnable state
Happens when the thread is in
◦ Suspended
◦ Sleeping or
◦ waiting
Dead state
A running thread ends its life
◦ By the end of the run() normally
◦ Premature death by stop()
Program
Output
Thread Priority
We can set priority by setPriority()
ThreadName.setPriority(intNumber);
the intNumber is an integer value
which sets the priority
It has several priority constants
MIN_PRIORITY=1
NORM_PRIORITY=5
MAX_PRIORITY=10
intNumber may assume any value b/w 1
& 10
program
Implementing
Runnable interface
Declare the class as implementing the
Runnable interface
Implement the run() method
Create a thread by defining an object
that is instantiated from this “runnable”class as the target of the thread
Call the thread’s start()method
to run the thread
Class X implements Runnable
{
Public void run()
{
For(int i=1;i<10;i++)
{
System.out.println(“Thread
X”+i);
}
System.out.println(”End of the thread”);
}
}
Class RunnableTest
{
public
static void main(String args[])
{
X runnable =new X();
Thread threadX=new Thread(runnable)’
thread.start();
System.out.println(”End of the main
thread”);
}
}
Output
End of mainThread
thread:1
thread:2
thread:3
thread:4
thread:5
thread:6
thread:7
thread:8
thread:9
thread:10
End of threadX
No comments:
Post a Comment