Skip to content

Multiple thread failed to execute the nonsynchronized code in java

If Thread1 get the lock and start executing the synchronized block, During the same time,Thread2 is free to execute the non-synchronized part of the code which is IntStream.range(0, 5).forEach(x -> System.out.println("thread Unsafe Zone,"+name)).

Unexpectedly,below is what actually happening while i run code.

Explaintation-

Suppose,thread1(or thread2) get the lock and execute the synchronized block and then thread2( or thread1) get the lock released by thread1(or thread2) and execute the synchronized block.After both thread complete the execution of synchronized block, then both thread parellelly start executing the non-synchronized part. I run the code multiple time hoping to get the desired output but fail to avail.

Demo class

public class Demo {
    public static void main(String[] args) {

        Display d=new Display("AnyName");
        Thread1 t1=new Thread1(d);
        Thread2 t2=new Thread2(d);
        t1.start();
        t2.start();
    }
}

Thread1 class

public class Thread1 extends Thread {
    Display d;
    Thread1(Display d) {
        this.d = d;
    }
    @Override
    public void run() {
       d.getname("Thread 1");
    }
}

Thread2 class

public class Thread2 extends Thread {
    Display d;

    Thread2(Display d) {
        this.d = d;
    }

    @Override
    public void run() {
            d.getname("Thread 2");
    }
}

Display class

public class Display {
    String name;

    public Display(String name) {
        this.name = name;
    }

    public void getname(String name)  {
        synchronized (this) {
            IntStream.range(0, 5).forEach((idx) -> {
                System.out.println(name);
            });
            this.name=name;
        }

        IntStream.range(0, 5).forEach(x -> System.out.println("thread Unsafe Zone,"+name));// this line
//get executed only after the synchronized method completed by both thread

    }
}

Output:

Thread 2
Thread 2
Thread 2
Thread 2
Thread 2
Thread 1
Thread 1
Thread 1
Thread 1
Thread 1
thread Unsafe Zone,Thread 2
thread Unsafe Zone,Thread 1
thread Unsafe Zone,Thread 2
thread Unsafe Zone,Thread 1
thread Unsafe Zone,Thread 2
thread Unsafe Zone,Thread 1
thread Unsafe Zone,Thread 1
thread Unsafe Zone,Thread 2
thread Unsafe Zone,Thread 1
thread Unsafe Zone,Thread 2

Answer

If Thread1 get the lock and start executing the synchronized block, During the same time,Thread2 is free to execute the non-synchronized part of the code: not exactly.

In execution order the non-synchronized block follows the synchronized block. Therefore you will never see any thread executing the non-synchronized block before the synchronized block.

The best what you can hope for is something like

Thread 1
Thread 1
Thread 1
Thread 1
Thread 1
thread Unsafe Zone,Thread 1
thread Unsafe Zone,Thread 1
thread Unsafe Zone,Thread 1
Thread 2
Thread 2
Thread 2
Thread 2
Thread 2
thread Unsafe Zone,Thread 1
thread Unsafe Zone,Thread 1
thread Unsafe Zone,Thread 2
thread Unsafe Zone,Thread 2
thread Unsafe Zone,Thread 2
thread Unsafe Zone,Thread 2
thread Unsafe Zone,Thread 2

where the thread 2 enters the synchronized block while thread 1 is running in the non-synchronized block.

You can increase the chance for such an outcome by changing your Display.getname() method by calling Thread.yield(); after the synchronized block and by doing more System.out.println() calls:

public void getname(String name)  {
    synchronized (this) {
        IntStream.range(0, 5).forEach((idx) -> {
            System.out.println(name);
        });
        this.name=name;
    }
    Thread.yield();

    IntStream.range(0, 20).forEach(x -> System.out.println("thread Unsafe Zone,"+name));

}