Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does synchronization not 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 cannot understand what difference wrt Synchronization does it make to initialize PrintNumbers in the Runnable MyThread and in the SyncExample class. Please explain.

like image 520
Anup Verma Avatar asked Oct 08 '16 09:10

Anup Verma


People also ask

Why synchronized method is not working in Java?

So in your case, Thread mt1 acquires lock on Object mt1 and Thread mt2 acquires lock on Object mt2 and they do not block each Other as the two threads are working on two different locks. And when two threads modify a shared variable concurrently(not synchronized way), the result is unpredictable.

Can we synchronize two Java processes?

Yes, they can run simultaneously both threads. If you create 2 objects of the class as each object contains only one lock and every synchronized method requires lock. So if you want to run simultaneously, create two objects and then try to run by using of those object reference.

Do synchronized methods synchronize on the object or the class?

Synchronized static methods are synchronized on the class object of the class the synchronized static method belongs to. Since only one class object exists in the Java VM per class, only one thread can execute inside a static synchronized method in the same class.

What are the two types of synchronization?

There are two types of synchronization: full and incremental.


1 Answers

I cannot understand what difference wrt Synchronization does it make to initialize PrintNumbers in the Runnable MyThread and in the SyncExample class.

It doesn't. What does matter is that in your first example, you have only one PrintNumbers instance which both threads share. But in your second example, you have two separate PrintNumbers instances, one for each thread.

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

When both threads share an instance, the two calls to display are serialized. But when the threads each have their own instance, the two calls to display are on separate instances, and thus there is no serialization of the calls, they can overlap.

like image 122
T.J. Crowder Avatar answered Oct 26 '22 22:10

T.J. Crowder