|
It is basically the mechanism which helps two or more threads to share all the available resources in a sequential manner thus making sure that one resource is been used by only one single thread at a time thus avoiding the problem of collision or conflict between the threads and it is done in Java with the concept of “monitor” also named as “semaphore”.
The basic working mechanism of monitor works in a way that it works like a lock and this monitor can be owned by only one thread at a time and until the time this monitor or lock is been released by that particular thread the other threads are said to be in the waiting stage to acquire for that monitor or lock.
In Java we can enter into any monitor or lock by calling the method that has been modified with the synchronized keyword.
General form of the synchronized statement is as
synchronized(object) {
// statements to be synchronized
}
where object is a reference to the object being synchronized.
A synchronized block ensures that a call to a method that is a member of object occurs only after the current thread has successfully entered object’s monitor.
Interthread communication
Java implements a very efficient interprocess communication which reduces the CPU’s idle time to a very great extent. It is been implemented through wait ( ), notify ( ) and notifyAll ( ) methods. Since these methods are implemented as final methods they are present in all the classes.
The basic functionality of each one of them is as under:
■ wait( ) acts as a intimation to the calling thread to give up the monitor and go to sleep until some
other thread enters the same monitor and calls notify( ).
■ notify( ) is used as intimator to wake up the first thread that called wait( ) on the same object.
■ notifyAll( ) as the term states wakes up all the threads that called wait( ) on the same object.
The highest priority thread will run first.
public class WaitNotifyAllExample {
public static void main(String[] args) {
try {
Object o = new Object();
Thread thread1 = new Thread(new MyOwnRunnable("A", o));
Thread thread2 = new Thread(new MyOwnRunnable("B", o));
Thread thread3 = new Thread(new MyOwnRunnable("C", o));
// synchronized keyword acquires lock on the object.
synchronized (o) {
thread1.start();
// wait till the first thread completes execution.
// thread should acquire the lock on the object
// before calling wait method on it. Otherwise it will
// throw java.lang.IllegalMonitorStateException
o.wait();
thread2.start();
// wait till the second thread completes execution
o.wait();
thread3.start();
}
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
}
class MyOwnRunnable implements Runnable {
private String threadName;
private Object o;
public MyOwnRunnable(String name, Object o) {
threadName = name;
this.o = o;
}
public void run() {
synchronized (o) {
for (int i = 0; i < 1000; i++) {
System.out.println("Thread " + threadName + " Count : " + i);
}
// notify all threads waiting for the object o.
// thread should acquire the lock on the object
// before calling notify or notifyAll method on it.
// Otherwise it will throw java.lang.IllegalMonitorStateException
o.notifyAll();
}
}
}
Deadlock
Deadlock as the name illustrates it is a special type of error that each programmer should make sure to avoid in a multitasking environment. This is a very difficult error to be debugged because it occurs once at a time when two threads time-slice in just the right way and other reason for a deadlock to occur is when it involves more than two words and two synchronized objects at a time.
It is illustrated in a better way with the help of an example.
Suspending, Resuming and stopping threads:
Suspending a thread in java is quite an easy task and similarly we can resume that particular thread also in a very simple way.
The thread can be suspended using suspend ( ) method, resumed with resume ( ) and stopped using stop ( ).
Suspend at times create serious troubles as it may happen that if a thread is suspended at a time when it has acquired some resources and now the other threads will be waiting for those resources hence causing the situation of a deadlock , so it is tried to be avoided.
The resume () is also avoided as it is the counterpart pf suspend ( ) and can’t be implemented without suspend ( ).
Stop( ) too sometimes ends up into serious trouble situations so Java 2 came up with a better alternative of run ( ) which will regularly keep a check as to when a thread should resume, suspend or stop its own execution and thus is based on the setting of a flag on each thread which determines the state as running means that the thread should be allowed to run, suspend denoted to be suspended and stop denoting the termination of that particular thread.
|