1

I was reading Synchronized working. Here is the example:

public class Singleton{

private static volatile Singleton _instance;

public static Singleton getInstance(){
   if(_instance == null){
            synchronized(Singleton.class){
              if(_instance == null)
              _instance = new Singleton();
            }
   }
   return _instance;
}

Let Suppose two Thread A and B are accessing getInstance(); method, If thread A is in synchronized block then thread B will skip that block and execute next block/statement or will wait/blocked until Thread A leave the synchronized block.

2nd What is, why is Singleton.class in synchronized parameter and when it could be null

and the below Statement is true?

Intrinsic locks are on the object:

class A
{
   public synchronized void method1(){...}
   public synchronized void method2(){...}
}

If thread A is in method1 then threadB cannot enter method2 or any other synchronized method .

2
  • Deleted my initial answer. Looks like Java has different semantics than C#, which I was translating to this situation. See stackoverflow.com/a/2462215/141172 Commented Oct 4, 2015 at 17:49
  • @EricJ. can you confirm that is it true? If thread A is in method1 then threadB cannot enter method2 or any other synchronized method . Commented Oct 4, 2015 at 17:51

2 Answers 2

1

1: Thread B will wait, until Thread A will release the lock on the synchronized object and execute the code, after it will aquire the lock on the synchronized object.

2: Singleton.class is the object, that represent that class. You are synchronizing on it, since your _instance-object is null.

public synchronized void method1(){...}

is synchronizing on the Object, on that you call that method, that means, 2 Threads will wait for each other, if you call it like this:

final A a = new A();
new Thread(new Runnable(){
    public void run(){
        a.method1();
    }
}).start();
a.method1();

but both threads will be executed parallel, if you call it on different Objects:

A a = new A();
final A b = new A();
new Thread(new Runnable(){
    public void run(){
        b.method1();
    }
}).start();
a.method1();

last question: right, Thread B will not enter method 2, since the synchronized method locks on the Object

Btw.

public synchronized void method1(){...}

is equivalent to:

public void method1(){
    synchronized(this){
        ...
    }
}
Sign up to request clarification or add additional context in comments.

10 Comments

Johnny why you use final keyword?
@NewBie without final you wouldn't be able to "see" that variable in the Runnable. Thats way the variable will be stored in the heap and not the stack and will be available outside the Method-Stack
and Johny as you said, Singleton.class is object then what is the mean of this statement? Only important thing to note here is that if object used to lock synchronized block of code, Singleton.class in below example is null then Java synchronized block will throw a NullPointerException. then we will face Exception each time(instance creation)? this statement is from my given link.
@NewBie no, Singleton.class is an object, that is representing the class. It is NOT an Object of type Singleton. In Java there is alway 1 Object, that is representing the class. E.g. if you would make a static synchronized method, that method would lock on that Class-Object. And it is not null
one another nice point. Can you give me link about Class-Object? I want to learn more how it useful and why we require it etc... or you mean about Object that used to access static-fields?
|
0

See here for documentation on the synchronized keyword.

Using synchronized on a method will only allow one thread at a time to access the method. All other threads will block and be queued for execution. When you use this keyword, the instance object is used as a lock to synchronize the execution. If you are calling methods on the same object only one thread can hold the lock at a time so your statement is true.

Using the synchronized keyword on a method can be performance-degrading, and it's recommended to use the Java concurrency API instead, see here.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.