Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why Synchronized method allowing multiple thread to run concurrently?

I have following program in same file. I have synchronized the run() method.

class MyThread2 implements Runnable {
    Thread    t;

    MyThread2(String s) {
        t=new Thread(this,s);
        t.start();
    } 

    public synchronized  void  run() {
        for (int i=0;i<3;i++) {
            System.out.println("Thread name : "+ Thread.currentThread).getName());
            try {
                t.sleep(1000);
            }
            catch (InterruptedException e) {
                e.getMessage();
            }
        }
    }
}

class TestSync {
    public static void main(String[] args) {
        MyThread2 m1=new MyThread2("My Thread 1");
        c.fun();
    }
}

class c {
    static void fun() {
        MyThread2 m1=new MyThread2("My Thread 4");
    }
}

output is

Thread name : My Thread 1
Thread name : My Thread 4
Thread name : My Thread 4
Thread name : My Thread 1
Thread name : My Thread 1
Thread name : My Thread 4

My question is why is synchronized method allowing both "My Thread 1" and "My Thread 4" thread access concurrently?

like image 625
user980089 Avatar asked Oct 05 '11 10:10

user980089


People also ask

Why synchronization is necessary in multi threaded programming?

Synchronization in java is the capability to control the access of multiple threads to any shared resource. In the Multithreading concept, multiple threads try to access the shared resources at a time to produce inconsistent results. The synchronization is necessary for reliable communication between threads.

What will happen if a synchronized method is called by two threads?

Two threads cannot access the same synchronized method on the same object instance. One will get the lock and the other will block until the first thread leaves the method. In your example, instance methods are synchronized on the object that contains them. In this case, when you call alphonse.

Can two threads access a synchronized method at the same time?

First, it is not possible for two invocations of synchronized methods on the same object to interleave. When one thread is executing a synchronized method for an object, all other threads that invoke synchronized methods for the same object block (suspend execution) until the first thread is done with the object.

What is the problem with synchronization of thread?

Synchronization can result in hold-wait deadlock where two threads each have the lock of an object, and are trying to acquire the lock of the other thread's object. Synchronization must also be global for a class, and an easy mistake to make is to forget to synchronize a method.


1 Answers

synchronized methods work at the instance level. Each instance of the class gets its own lock. The lock gets acquired every time any synchronized method of the instance is entered. This prevents multiple threads calling synchronized methods on the same instance (note that this also prevents different synchronized methods from getting called on the same instance).

Now, since you have two instances of your class, each instance gets its own lock. There's nothing to prevent the two threads each operating on its own instance concurrently.

If you do want to prevent this, you could have a synchronized(obj) block inside run(), where obj would be some object shared by both instances of your class:

class MyThread2 implements Runnable {
   private static final Object lock = new Object();
   ...
   public void run() {
     synchronized(lock) {
       ...
     }
   }
}
like image 99
NPE Avatar answered Nov 15 '22 14:11

NPE