简体   繁体   English

同步块/代码Java上的线程访问

[英]Threads access on Synchronized Block/Code Java

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(); 假设两个线程AB正在访问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. 方法,如果线程Asynchronized块中,则线程B将跳过该块并执行下一个块/语句,或者将等待/阻塞,直到线程A离开synchronized块为止。

2nd What is, why is Singleton.class in synchronized parameter and when it could be null 第二,什么是synchronized参数中的Singleton.class为什么以及何时可以为null

and the below Statement is true? 并且下面的Statement是正确的?

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 . 如果线程A是在method1然后threadB不能进入method2或任何其他同步方法。

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. 1:线程B将等待,直到线程A释放对同步对象的锁定之后,线程A将释放对同步对象的锁定并执行代码。

2: Singleton.class is the object, that represent that class. 2: Singleton.class是表示该类的对象。 You are synchronizing on it, since your _instance -object is null. 您正在对其进行同步,因为_instance -object为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: 在对象上进行同步,即在您调用该方法时,这意味着,如果这样调用,则2个线程将彼此等待:

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: 但是如果您在不同的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 最后一个问题:对,线程B将不会进入方法2,因为同步方法锁定在对象上

Btw. 顺便说一句。

public synchronized void method1(){...}

is equivalent to: 等效于:

public void method1(){
    synchronized(this){
        ...
    }
}

See here for documentation on the synchronized keyword. 请参阅此处以获取有关synced关键字的文档。

Using synchronized on a method will only allow one thread at a time to access the method. 在方法上使用synchronized将一次仅允许一个线程访问该方法。 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. 如果您在同一对象上调用方法,则一次只能有一个线程持有该锁,因此您的声明为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 . 在方法上使用synchronized关键字可能会降低性能,建议改用Java并发API,请参见此处

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM