Multithreading in Java
Multithreading is a Java feature that allows concurrent
execution of two or more parts of a program for
maximum utilization of CPU. Each part of such
program is called a thread. So, threads are light-weight
processes within a process.
Threads can be created by using two mechanisms :
1. Extending the Thread class
2. Implementing the Runnable Interface
Life Cycle of a Thread
A thread goes through various stages in its life cycle.
For example, a thread is born, started, runs, and then
dies. The following diagram shows the complete life
cycle of a thread.
Following are the stages of the life cycle –
New – A new thread begins its life cycle in the new state.
It remains in this state until the program starts the
thread. It is also referred to as a born thread.
Runnable – After a newly born thread is started, the
thread becomes runnable. A thread in this state is
considered to be executing its task.
Waiting – Sometimes, a thread transitions to the waiting
state while the thread waits for another thread to
perform a task. A thread transitions back to the runnable
state only when another thread signals the waiting
thread to continue executing.
Timed Waiting – A runnable thread can enter the timed
waiting state for a specified interval of time. A thread in
this state transitions back to the runnable state when
that time interval expires or when the event it is waiting
for occurs.
Terminated (Dead) – A runnable thread enters the
terminated state when it completes its task or otherwise
terminates.
Thread creation by extending the Thread class
We create a class that extends
the java.lang.Thread class. This class overrides the run()
method available in the Thread class. A thread begins its
life inside run() method. We create an object of our new
class and call start() method to start the execution of a
thread. Start() invokes the run() method on the Thread
object.
Example
Java Thread Example by extending Thread
class
1. class Multi extends Thread{
2. public void run(){
3. System.out.println("thread is runni
ng...");
4. }
5. public static void main(String arg
s[]){
6. Multi t1=new Multi();
7. t1.start();
8. }
9. }
Output:thread is running...
Java Thread Example by implementing
Runnable interface
class Multi3 implements Runnable{
public void run(){
System.out.println("thread is running.
..");
}
public static void main(String args[]){
Multi3 m1=new Multi3();
Thread t1 =new Thread(m1);
t1.start();
}
}
Output:thread is running...
If you are not
extending the Thread
class,your class
object would not be
treated as a thread
object.So you need
to explicitely create
Thread class
object.We are
passing the object of
your class that
implements Runnable
so that your class
run() method may
execute.
wait(),notify()and notifyAll() methods
In a multithreaded environment, multiple threads might try to modify the
same resource. If threads aren't managed properly, this will, of course, lead
to consistency issues.
Inter-thread communication or Co-operation is all about allowing
synchronized threads to communicate with each other.
Cooperation (Inter-thread communication) is a mechanism in which a thread
is paused running in its critical section and another thread is allowed to
enter (or lock) in the same critical section to be executed.It is implemented
by following methods of Object class:
o wait()
o notify()
o notifyAll()
1) wait() method
Causes current thread to release the lock and wait until either another
thread invokes the notify() method or the notifyAll() method for this object,
or a specified amount of time has elapsed.
The current thread must own this object's monitor, so it must be called from
the synchronized method only otherwise it will throw exception.
Method Description
public final void waits until
wait()throws object is
InterruptedException notified.
public final void waits for
wait(long the
timeout)throws specified
InterruptedException amount of
time.
2) notify() method
Wakes up a single thread that is waiting on this object's monitor. If any
threads are waiting on this object, one of them is chosen to be awakened.
The choice is arbitrary and occurs at the discretion of the implementation.
Syntax:
public final void notify()
3) notifyAll() method
Wakes up all threads that are waiting on this object's monitor. Syntax:
public final void notifyAll()