简体   繁体   English

Java - 在多线程中使用可重入锁

[英]Java - Using a Reentrant lock with Multi-Threading

I'm making a program with multi-threading, and I want to use a lock.我正在制作一个多线程程序,我想使用锁。 The problem is the program runs on a thread, and I want to have a Thread that grabs the lock, to pause the first Thread, then be able unlock it when a method is called from a third Thread.问题是程序在一个线程上运行,我想要一个线程来获取锁,暂停第一个线程,然后在从第三个线程调用方法时能够解锁它。 I tried a bunch of different things, but none of them worked.我尝试了很多不同的东西,但没有一个奏效。 How should I do this?我该怎么做?

EDIT:编辑:

Rewrote the question改写问题

As I understand, you want a thread to sleep and wait for the notification from another thread.据我了解,您希望一个线程休眠并等待来自另一个线程的通知。 You should look at Observer and Observable instead of locking.您应该查看Observer 和 Observable而不是锁定。

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

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