Tuesday, 8 December 2020

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.

No comments:

Post a Comment

links for Data Structure

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