简体   繁体   English

如何使用Lock指定要在Java中锁定的对象

[英]How to specify an object to be locked in java using Lock

Using the synchronized(intrinsic locking) keyword for locking, we could do something like: 使用synced(intrinsiclocking)关键字进行锁定,我们可以执行以下操作:

public void addSum(int a) {
   synchronized(q) {
     q.add(a); // q is say a Queue
   }
}

In the above code say when an object tries to call the addSum() method iexaddSum(10), the lock will be held on 'q' and not x. 在上面的代码中,当对象尝试调用addSum()方法iexaddSum(10)时,锁将保留在“ q”而不是x上。 So using synchronization we could lock an object which is other than the actual calling object(Class). 因此,使用同步我们可以锁定与实际调用对象(类)不同的对象。

Below I'm using Lock from java concurrent package, is there a way to specify which object should the lock be on (ie like in the above code snippet using synchronized it was specified that the lock/synchronization should be on 'q'). 下面我从Java并发包中使用Lock,有一种方法可以指定应该在哪个对象上锁定(例如,在上面的使用同步代码的代码段中,已指定锁定/同步应该在“ q”上)。 However below when I'm using Lock, I haven't specified on which object should the lock be on. 但是下面在我使用Lock时,我没有指定应该在哪个对象上启用该锁。 Can it be done? 能做到吗

public void addSum(int a) {
   lock.tryLock();
   q.add(a);
   lock.unlock();
 }

I did refer - http://docs.oracle.com/javase/tutorial/essential/concurrency/newlocks.html . 我确实参考了-http://docs.oracle.com/javase/tutorial/essential/concurrency/newlocks.html However was looking for much smaller example to clear my concept. 但是,正在寻找更小的例子来阐明我的概念。

No, Lock objects don't work the same way as synchronized . 不, Lock对象的工作方式与synchronized工作方式不同。 synchronized cannot start within a method invocation and reach outside that method invocation. synchronized不能在方法调用内启动,也不能到达该方法调用之外。 The pattern you've shown 您显示的图案

lock.tryLock();
q.add(a);
lock.unlock();

would only be possible if the opposite were true. 只有在相反情况成立的情况下才有可能。 Lock objects typically work by flipping on/off a switch/flag atomically, indicating they've acquired or released the lock. Lock对象通常通过原子地打开/关闭开关/标志来工作,表明它们已获取或释放了锁定。

I think you misunderstand what the word "lock" means. 我认为您误解了“锁”一词的含义。 Suppose this method is called: 假设此方法称为:

void foobar() {
    synchronized(x) {
        y.doSomething();
    }
}

We say that x is "locked" while the thread is in the y.doSomething() call, but that does not prevent other threads from accessing fields or updating fields of x . 我们说x在线程处于y.doSomething()调用中时被“锁定”,但这并不能防止其他线程访问x字段或更新x字段。 The synchronized keyword means one thing, and one thing only. synchronized关键字意味着一件事,而只有一件事。

The JVM will not allow two threads to synchronize on the same object at the same time. JVM不允许两个线程同时在同一个对象上同步。

That's all it means. 这就是全部 How you use it is up to you. 如何使用取决于您自己。 My example is using it to prevent y.doSomething() from being called in more than one thread at the same time, but it only works if every call to y.doSomething() is protected in the same way, and it only works if x always refers to the same object. 我的示例使用它来防止y.doSomething()在多个线程中调用y.doSomething() ,但只有在每次对y.doSomething()调用都以相同的方式受到保护的情况下,它才有效,并且仅当x始终引用相同的对象。

The java.util.concurrent.ReentrantLock class works much the same way. java.util.concurrent.ReentrantLock类的工作方式大致相同。 The only guarantee that the JVM makes is that no two threads can "lock" the same ReentrantLock object at the same time. JVM的唯一保证是没有两个线程可以同时“锁定”相同的ReentrantLock对象。 That's all it does. 这就是全部。 The rest is up to you. 其余的取决于您。


PS, Your second example does not test the value returned by lock.tryLock() . PS,您的第二个示例不测试lock.tryLock()返回的值。 That's a mistake. 错了 If lock.tryLock() returns false, that means it failed to lock the lock. 如果lock.tryLock()返回false,则表示无法锁定该锁。

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

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