简体   繁体   English

如果两个线程使用不同的监视器,是否可以在同一个 object 上执行相同的同步代码块?

[英]Can two threads execute the same synchronized block of code on the same object if they use different monitors?

I'm newish to Java concurrency and I'm trying to better understand monitors.我对 Java 并发很陌生,我正在努力更好地理解监视器。

Suppose I have one object, with a method that takes some kind of reference argument and uses that argument as a monitor in a synchronized block:假设我有一个 object,其方法采用某种引用参数并将该参数用作同步块中的监视器:

class Entity() {
    public void myMethod(Object monitor) {
        synchronized(monitor) {
            // critical stuff
        }
    }
}

Can two threads enter that section at the same time on the same entity if they use different objects for the monitor?如果两个线程使用不同的对象作为监视器,是否可以同时进入同一实体上的该部分?

final Entity myEntity = new Entity();
for (int i = 0; i < 3; i++) {
    new Thread() {
        public void run() {
            // Can these all run concurrently?
            myEntity.myMethod(new Object());
        }
    }.start();
}

If I understand monitors correctly, then yes, all the threads can enter the synchronized block at the same time, because each monitor acts as a totally different mutex and none of the threads are aware of the other threads in that block.如果我正确理解了监视器,那么是的,所有线程都可以同时进入同步块,因为每个监视器都充当完全不同的互斥锁,并且没有一个线程知道该块中的其他线程。

It's been difficult to find documentation on this because tutorials mostly seem to just use "this" as the monitor.很难找到这方面的文档,因为教程似乎大多只是使用“this”作为监视器。

Can two threads enter that section at the same time on the same entity if they use different objects for the monitor?如果两个线程使用不同的对象作为监视器,是否可以同时进入同一实体上的该部分?

From the oracle tutorial one can read:oracle 教程中可以阅读:

Every object has an intrinsic lock associated with it.每个 object 都有一个与之关联的内在锁。 By convention, a thread that needs exclusive and consistent access to an object's fields has to acquire the object's intrinsic lock before accessing them, and then release the intrinsic lock when it's done with them.按照惯例,需要对对象字段进行排他和一致访问的线程必须在访问对象之前获取对象的内在锁,然后在完成访问时释放内在锁。 A thread is said to own the intrinsic lock between the time it has acquired the lock and released the lock.在获得锁和释放锁之间,线程被称为拥有内在锁。 As long as a thread owns an intrinsic lock, no other thread can acquire the same lock.只要一个线程拥有一个内在锁,其他线程就不能获得相同的锁。 The other thread will block when it attempts to acquire the lock.另一个线程在尝试获取锁时会阻塞。

This informally means that one can synchronize using any Java Object .这非正式地意味着可以使用任何 Java Object进行同步 A block enclosed by a clause synchronized on a single object instance will be executed sequentially, ie, executed by the thread holding the lock of the object being synchronized .在单个 object 实例上由synchronized子句包围的块将按顺序执行,由持有正在同步的 object 锁的线程执行。

Can two threads enter that section at the same time on the same entity if they use different objects for the monitor?如果两个线程使用不同的对象作为监视器,是否可以同时进入同一实体上的该部分?

Yes, multiple threads can execute (in parallel) the same code region wrapped with a synchronized clause as long as each of those threads is synchronizing using different object instances.是的,只要每个线程使用不同的 object 实例进行同步,多个线程就可以(并行)执行使用synchronized子句包装的相同代码区域。

One can also synchronize using the class itself, rather than its instances:也可以使用class本身而不是其实例进行同步:

 synchronized (SomeClass.class){
     System.out.println("Hello World");
 } 

In such cases, all the threads that use the clause synchronized on the class SomeClass , will have to synchronize among each other.在这种情况下,使用 class SomeClasssynchronized子句的所有线程都必须相互同步

One can also use clause synchronized on methods ( eg, public synchronized void method2() );也可以在方法上使用synchronized子句(例如, public synchronized void method2() ); for non-static methods the object being synchronized will be the object to which that method belongs, whereas for static methods ( eg, public static synchronized void method1() ) will be class itself to which that method belongs. for non-static methods the object being synchronized will be the object to which that method belongs, whereas for static methods ( eg, public static synchronized void method1() ) will be class itself to which that method belongs.

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

相关问题 两个线程即使同步也调用相同的代码 - Two threads call same code even if it is synchronized Java中的监视器和同步块(似乎两个线程同时拥有一个监视器) - Monitors and synchronized blocks in Java (it appears two threads own a monitor at the same time) 两个线程如何同时进入同步块? - How could the two threads enter the synchronized block at the same time? 同一个对象的两个不同的同步方法? - two different synchronized methods of the same object? 2个线程如何同时访问一个同步块? - How can 2 threads access a synchronized block at the same time? 两个线程可以分配给不同的处理器并同时执行吗? - Can two threads be assigned to different processors and execute at the same time? 两个线程如何进入两个锁定同一对象的同步块 - How can two threads enter two synchronized blocks which hold a lock on the same object 如何通过Junit测试两个线程无法同时访问的同步对象? - How to Junit test a synchronized object not accessed by two threads at same time? 需要代码来理解同步的 static 和同一实例上的两个不同线程同时访问的非静态方法 - Need code to understand synchronized static and non-static methods being accessed at the same time by two different threads on the same instance 两个线程可以同时访问同步方法吗? - Can two threads access a synchronized method at the same time?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM