简体   繁体   English

了解多线程

[英]Understanding multi-threading

I just have a question in regards to threads that run concurrently and the lock they have on an object. 关于并发运行的线程以及它们对对象的锁定,我只是有一个问题。 From what I understand is that the thread that calls the wait() method will go on a waiting list and allows another thread from a blocked list to take over the lock on and object(within synchronized code). 据我所知,调用wait()方法的线程将进入等待列表,并允许来自阻塞列表的另一个线程接管锁定和对象(在同步代码中)。 If this thread that now has the lock on the object calls the notify() method it wakes up the thread that called wait() and it is moved to the blocked list. 如果现在对该对象具有锁定的此线程调用notify()方法,则它会唤醒调用wait()的线程,并将其移动到阻止列表。

What happens to the thread that calls the notify() method. 调用notify()方法的线程会发生什么。 Does it still have a lock on the object or does it now go on a waiting list? 它是否仍然锁定对象或现在是否在等待列表?

regards 问候

Only one thread can hold the lock for an object. 只有一个线程可以保持对象的锁定。 The wait() and notify() methods must be called while the thread holds the lock on the object you call these methods on; 当线程持有对您调用这些方法的对象的锁时,必须调用wait()notify()方法; if they don't (for example, because you didn't synchronize on the object), you'll get an IllegalMonitorStateException . 如果他们不这样做(例如,因为你没有在对象上同步),你将得到一个IllegalMonitorStateException

When you call wait() , then the thread gives up the lock and goes on a waiting list (stops executing). 当你调用wait() ,线程放弃锁定并进入等待列表(停止执行)。 When wait() returns, the thread will have obtained the lock again. wait()返回时,线程将再次获得锁定。 However, the thread that calls notify() is still holding the lock, so the waiting thread will not resume before the notifying thread exits the synchronized block or method so that it releases the lock on the object. 但是,调用notify()的线程仍然保持锁定,因此在通知线程退出synchronized块或方法之前,等待线程不会恢复,以便释放对象的锁定。

By calling notify() the thread is not giving up the lock on the object. 通过调用notify() ,线程不会放弃对象的锁定。

A possible sequence of events would be: 一系列可能的事件将是:

  • Thread 1 goes into a synchronized block, obtaining the lock for the object 线程1进入synchronized块,获得对象的锁定
  • Thread 1 calls wait() on the object, giving up the lock, stops executing 线程1调用对象上的wait() ,放弃锁定,停止执行
  • Thread 2 goes into a synchronized block, obtaining the lock for the object 线程2进入synchronized块,获得对象的锁定
  • Thread 2 calls notify() on the object, but still holds the lock 线程2调用对象上的notify() ,但仍保持锁定
  • Thread 1 is awakened and tries to obtain the lock, but it can't because Thread 2 still has it (so Thread 1 has to wait for the lock) 线程1被唤醒并尝试获取锁定,但它不能,因为线程2仍然拥有它(因此线程1必须等待锁定)
  • Thread 2 exits the synchronized block and releases the lock 线程2退出synchronized块并释放锁定
  • Thread 1 can now obtain the lock and returns from wait() 线程1现在可以获取锁并从wait()返回

The notifying thread still owns the lock. 通知线程仍然拥有锁。 See doc section 17.14 (bottom of the page): 请参阅文档部分17.14(页面底部):

The notify method should be called for an object only when the current thread has already locked the object's lock. 仅当当前线程已锁定对象的锁定时,才应为对象调用notify方法。 If the wait set for the object is not empty, then some arbitrarily chosen thread is removed from the wait set and re-enabled for thread scheduling. 如果对象的等待集不为空,则从等待集中删除一些任意选择的线程并重新启用以进行线程调度。 (Of course, that thread will not be able to proceed until the current thread relinquishes the object's lock.) (当然,在当前线程放弃对象的锁定之前,该线程将无法继续。)

No, it will release the lock by leaving a synchronized block or returning from a synchronized method. 不,它将通过保留同步块或从同步方法返回来释放锁定。 It will not go back to the waiting list until calling wait() again. 在再次调用wait()之前,它不会返回等待列表。

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

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