Deadlock,Synchronized and Thread Controlling in java


Deadlock,Synchronized and Thread Controlling in java:


Java can execute Threads in the same memory space,hence it is easy to communicate between two or more threads. Inter thread communications allow threads to each other or wait.Since they are running in the same memory space it is also possible to access the same resources(variables,methods etc) in an object at the same time.


DeadLock:

When two or more threads are waiting to gain control of a resource.(refer here for more details).They want to access the data concurrently.Due to some reason,the condition on which the waiting threads rely on to gain control does not happen,it cause a condition called Deadlock.Assume that the threadA must access method1 before it releases method2 but theadB can not release method1 until it get hold of method2. As because they are mutually exclusive,a deadlock will occur.
It may so also happen that thread1 did not finish writing to a resource before that another thread thread2 is trying to read the same.So thread1 does not leave the control where as thread2 needs the control to read it.

Synchronization:

Idea of Mutex or monitor:

When we declare a method as synchronized,java creates a "monitor" and hands it over the thread that calls the method first time.A monitor is an object which is used as a mutually exclusive lock called mutex. So only one thread can own a monitor at a given point of time.When a thread acquires a lock it is said to have entered the monitor.As long as the thread holds the monitor,no other thread can enter the synchronized section of the code.A monitor is like a key and the thread that holds the key can only open the lock.All other threads trying to enter into the monitor will be suspended until the owner of the monitor exits the monitor.

Implementation of Mutex or monitor:

In java there is nothing called mutex or monitor as such. It is a concept and all java objects have their own implicit monitor associated with them. The keyword Synchronized is used by which method(s)
or block of code or block of statements can be made to protect from simultaneous access.When a class is designed with threads in mind,the class designer decides which methods should not be allowed to execute concurrently.When a class with synchronized method is instantiated , the new object is given its own implicit monitor.The entire duration a thread is a synchronized method,all other threads those try to call any other synchronized method on the same instance would have to wait.In order to exit from the monitor and relinquish the control of the object to next waiting thread, the owner of the monitor needs to return from the method.

We can have a block of code,method marked as synchronized:

synchronized(lock-object)
{
//synchronized code block
}
or
synchronized method1(){
//body of the synchronized method
}
synchronized method2(){
//body of the synchronized method
}
 
When a thread has completed its work of using synchronized method or block of code,it will hand over the monitor to the next thread that is ready to use the same resource.
If we create same priority thread, we can not control the execution of them.

class Thread1 extends Thread{
    public void run(){
        System.out.println("Thraed1 is running");
        try{
            System.out.println("Thraed1 is going to sleep");
            sleep(10000);
            System.out.println("Thraed1 is finished its execution");
        }
        catch(Exception e)
        {
            System.out.println("Some Exception is occured in Thread1");
        }
    }
}
class Thread2 extends Thread{
    public void run(){
        System.out.println("Thraed2 is running");
        System.out.println("Thraed2 is suspended");
        suspend();
        System.out.println("Thraed2 is running again");
    }
}
public class ControlMyThread{
    public static void main(String args[])
    {
        Thread1 th1=new Thread1();
        Thread2 th2=new Thread2();
        th1.start();
        th2.start();
        try{
            System.out.println("Resume Thraed2");
            th2.resume();
            System.out.println("lets wait for 10 seconds");
            th2.sleep(10000);
            System.out.println("Thraed2 is ready for stop");
        }
        catch(Exception e)
        {
            System.out.println("Some Exception is occured ");
        }
    }
}
 
The output for the first time:
$javac ControlMyThread.java
Note: ControlMyThread.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
$java -Xmx128M -Xms16M ControlMyThread
Resume Thraed2
Thraed2 is running
Thraed2 is suspended
Thraed1 is running
lets wait for 10 seconds
Thraed1 is going to sleep

2nd time output:
$javac ControlMyThread.java
Note: ControlMyThread.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
$java -Xmx128M -Xms16M ControlMyThread
Resume Thraed2
Thraed2 is running
Thraed2 is suspended
Thraed1 is running
Thraed1 is going to sleep
lets wait for 10 seconds

However we can control the priority of the thread by using a properties called Thread_Priority. I have written a separate post here to discuss more about controlling of thread using priority.
Deadlock,Synchronized and Thread Controlling in java Deadlock,Synchronized and Thread Controlling in java Reviewed by Animesh Chatterjee on December 17, 2018 Rating: 5

No comments:

Powered by Blogger.