Thursday, 10 December 2020

Multithread-synchornized Monitor object

public void add(int value){

    synchronized(this){

       this.count += value;   

    }

  } 

Notice how the Java synchronized block construct takes an object in parentheses. In the example "this" is used, which is the instance the add method is called on. The object taken in the parentheses by the synchronized construct is called a monitor object.


below using class level lock

public void add(int value){

    synchronized(Data.class){

       this.count += value;   

    }

  } 


In below code thread run one by one, once a thread complete his work then another will start due to lock on same string value "abc", then the compiler might actually use the same String object behind the scenes. 

 void method1(String threadName){
         synchronized ("abc") {
             System.out.println(threadName + ":method 1 running");
             try {
                 Thread.sleep(2000);
             } catch (InterruptedException e) {
                 e.printStackTrace();
             }
             System.out.println(threadName + ":sleep over");
             method2(threadName);
         }
    }

      void method2(String threadName){
        synchronized ("abc"){
        System.out.println(threadName+":method 2 running");
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(threadName+":sleep over");
    }}

Tuesday, 8 December 2020

Multi threading example - synchonized ,static

public class Test {

    public static void main(String[] args) {

        sample obj = new sample();


        t1 tt1 = new t1(obj);

        t2 tt2 = new t2(obj);

        tt1.start();

        tt2.start();

    }

}

//It’s possible that both static synchronized and non static synchronized method can run simultaneously or concurrently because they lock on different object.

class sample{

    public synchronized static void m1(String name){

        System.out.println(name+":m1");

        try {

            Thread.sleep(2000);

        } catch (InterruptedException e) {

            e.printStackTrace();

        }

    }

    public synchronized void m2(String name){

        System.out.println(name+":m2");

        try {

            Thread.sleep(2000);

        } catch (InterruptedException e) {

            e.printStackTrace();

        }

    }

}


class t1 extends  Thread{

    sample s;

    t1(sample s){

        this.s =s;

    }

    @Override

    public void run() {

        s.m1(Thread.currentThread().getName());

        s.m2(Thread.currentThread().getName());

    }

}


class t2 extends Thread{

    sample s;

    t2(sample s){

        this.s =s;

    }

    @Override

    public void run() {

        s.m2(Thread.currentThread().getName());

        s.m1(Thread.currentThread().getName());


    }

}

output: 

Thread-1:m2

Thread-0:m1

wait 2 second

Thread-0:m2

Thread-1:m1 


or

Thread-0:m1

Thread-1:m2

wait 2 second

Thread-0:m2

Thread-1:m1

Multithreading examples

class TwoT1 implements Runnable{

    Data data;

    TwoT1(Data data){

        this.data = data;

    }

    @Override

    public void run() {

        data.method1("t1");

    }

}


class TwoT2 implements Runnable{

    Data data;

    TwoT2(Data data){

        this.data = data;

    }

    @Override

    public void run() {

        data.method2("t2");

    }

}

public class Two {

    public static void main(String[] args) {

        Data data = new Data();

        Thread t1 = new Thread(new TwoT1(data));

        Thread t2 = new Thread(new TwoT2(data));

        t1.start();

        t2.start();

    }

}

 case1:

class Data{

    synchronized void method1(String threadName){

        System.out.println(threadName+":method 1 running");

        method2(threadName);

    }


     void method2(String threadName){

        System.out.println(threadName+":method 2 running");

    }

 

}

sample output1:

t1:method 1 running

t1:method 2 running

t2:method 2 running

sample output2:

t2:method 2 running

t1:method 1 running

t1:method 2 running

sample output3:

t1:method 1 running

t2:method 2 running

t1:method 2 running

output: Running properly both threads will run parallel because method2 not required any lock

case2:

class Data{

    synchronized void method1(String threadName){

        System.out.println(threadName+":method 1 running");

        try {

            Thread.sleep(2000);

        } catch (InterruptedException e) {

            e.printStackTrace();

        }

        method2(threadName);

    }


    synchronized void method2(String threadName){

        System.out.println(threadName+":method 2 running");

    }


    //running proper always execute first t1/t2 thread complete

}

output sample1:

t1:method 1 running

wait for 2 second

t1:method 2 running

t2:method 2 running


output sample2:

t2:method 2 running

t1:method 1 running

wait for 2 second

t1:method 2 running


output: Running proper execute either thread1 complete or thread2 complete if first thread1 get lock then it execute thread1 complete then thread2 get lock, if first thread2 get lock then it execute thread2 complete then thread1.

case3:

class Data{

    synchronized void method1(String threadName){

        System.out.println(threadName+":method 1 running");

        try {

            Thread.sleep(2000);

        } catch (InterruptedException e) {

            e.printStackTrace();

        }

        method2(threadName);

    }


    synchronized static void method2(String threadName){

        System.out.println(threadName+":method 2 running");

    }


    //running proper t1 and t2 will run prallel due to static and non static locks

}

output sample1:

t1:method 1 running

t2:method 2 running

wait 2 second

t1:method 2 running

output sample2:

t2:method 2 running

t1:method 1 running

wait for 2 second

t1:method 2 running

output : running proper thread1 and thread2 execute parallel due to static and non static locks as static is a class level lock and non-static is object level lock

case4:

class Data{

    synchronized static void method1(String threadName){

        System.out.println(threadName+":method 1 running");

        try {

            Thread.sleep(2000);

        } catch (InterruptedException e) {

            e.printStackTrace();

        }

        method2(threadName);

    }


    synchronized static void method2(String threadName){

        System.out.println(threadName+":method 2 running");

    }


    //running proper always execute first t1/t2 thread complete

}

output sample1:

t1:method 1 running

wait for 2 second

t1:method 2 running

t2:method 2 running


output sample2:

t2:method 2 running

t1:method 1 running

wait for 2 second

t1:method 2 running

output: Running proper execute either thread1 complete or thread2 complete if first thread1 get lock then it execute thread1 complete then thread2 get lock, if first thread2 get lock then it execute thread2 complete then thread1 as both method are static means both thread required class level lock.

links for Data Structure

  1) 𝐁𝐞𝐜𝐨𝐦𝐞 𝐌𝐚𝐬𝐭𝐞𝐫 𝐢𝐧 𝐋𝐢𝐧𝐤𝐞𝐝 𝐋𝐢𝐬𝐭:  https://lnkd.in/gXQux4zj 2) 𝐀𝐥𝐥 𝐭𝐲𝐩𝐞𝐬 𝐨𝐟 𝐓𝐫𝐞𝐞 𝐓𝐫𝐚𝐯𝐞𝐫𝐬𝐚𝐥𝐬...