Why doesn't sync work in the second code?

Synchronization works correctly in this code:

class PrintNumbers { synchronized public void display() { System.out.println("in display"); for (int i = 0; i < 3; i++) { System.out.println("Thread name : "+ Thread.currentThread().getName() + " i= " + i); try { Thread.sleep(1000); } catch (InterruptedException e) { e.getMessage(); } } System.out.println("out of display"); } } class MyThread implements Runnable { Thread t; PrintNumbers printNumbers; MyThread(PrintNumbers printNumbers, String s) { this.printNumbers = printNumbers; t = new Thread(this,s); t.start(); } public void run() { printNumbers.display(); } } class SyncExample { public static void main(String[] args) { PrintNumbers printNumbers = new PrintNumbers(); new MyThread(printNumbers, "My Thread 1"); new MyThread(printNumbers, "My Thread 2"); } } 

Output:

 in display Thread name : My Thread 1 i= 0 Thread name : My Thread 1 i= 1 Thread name : My Thread 1 i= 2 out of display in display Thread name : My Thread 2 i= 0 Thread name : My Thread 2 i= 1 Thread name : My Thread 2 i= 2 out of display 

but not in this code:

  class PrintNumbers { synchronized public void display() { System.out.println("in display"); for (int i = 0; i < 3; i++) { System.out.println("Thread name : "+ Thread.currentThread().getName() + " i= " + i); try { Thread.sleep(1000); } catch (InterruptedException e) { e.getMessage(); } } System.out.println("out of display"); } } class MyThread implements Runnable { Thread t; PrintNumbers printNumbers; MyThread(String s) { this.printNumbers = new PrintNumbers(); t = new Thread(this,s); t.start(); } public void run() { printNumbers.display(); } } class SyncExample { public static void main(String[] args) { new MyThread("My Thread 1"); new MyThread("My Thread 2"); } } 

Output:

 in display Thread name : My Thread 1 i= 0 in display Thread name : My Thread 2 i= 0 Thread name : My Thread 1 i= 1 Thread name : My Thread 2 i= 1 Thread name : My Thread 2 i= 2 Thread name : My Thread 1 i= 2 out of display out of display 

I can’t understand what the difference in synchronization is. Synchronization makes for initializing PrintNumbers in Runnable MyThread and in the SyncExample class. Please explain.

+5
source share
3 answers

I can’t understand what difference synchronization causes to initialize PrintNumbers in Runnable MyThread and in SyncExample class.

This is not true. The important thing is that in the first example, you have only one PrintNumbers instance that separates both threads. But in the second example, you have two separate instances of PrintNumbers , one for each stream.

Since PrintNumbers#display synchronized in the instance (the synchronized methods of the instance are synchronized on this ), it only synchronizes within the instance, and not across multiple instances.

When both threads share an instance, two calls to display serialized. But when each thread has its own instance, the two display calls are in separate instances and, therefore, the serialization of the calls does not occur, they can overlap.

+5
source

Because in the second code, each thread has its own PrintNumbers object, so they work in parallel. In the first, they share a single PrintNumbers object and work with it synchronously.

PS. Remember that synchronized for non-static methods does synchronization on an object (for static methods in a class).

+4
source

It works in both cases as it should. The difference is that in the first case, you have the only object that is synchronized. In the second you have two. Both of them are called only once, so they synchronize perfectly.

synchronized does not work between objects, only inside one.

+3
source

Source: https://habr.com/ru/post/1257891/


All Articles