java 在java中使用Object作为互斥锁

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6063352/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 14:10:33  来源:igfitidea点击:

using Object as a mutex in java

javamultithreadingmutexwaitnotify

提问by Martin Hansen

Hello there good poeple, I need some help.

你好,好人,我需要一些帮助。

I'm writing a music player which streams music from the web. If I pres the play button before the music is done buffering I want it to wait.

我正在编写一个音乐播放器,它可以从网络上播放音乐。如果我在音乐完成缓冲之前按下播放按钮,我希望它等待。

I tried doing something like this:

我尝试做这样的事情:

Object mutex = new Object();

public void main() {
    startStreaming();
    mutex.notify();
}

private void onClickPlayButton() {
    mutex.wait();
}

The problem is that is the playButton is not pressed the mutex.notify()if throws a "llegalMonitorStateException". How do you normally solve problems like this?

问题是没有按下播放按钮,mutex.notify()如果抛出“ llegalMonitorStateException”。你通常如何解决这样的问题?

EDITTo make clear. My question is: How do I make the button wait for the "startStreamning" method to finish?

编辑说清楚。我的问题是:如何让按钮等待“startStreamning”方法完成?

回答by sgokhales

According to the JavaDoc,

根据 JavaDoc,

IllegalMonitorStateExceptionis thrown "to indicate that a thread has attempted to wait on an object's monitor or to notify other threads waiting on an object's monitor without owning the specified monitor."

抛出IllegalMonitorStateException“以指示线程已尝试在对象监视器上等待或通知其他线程在不拥有指定监视器的情况下在对象监视器上等待。”

In order to call mutex.wait()or mutex.notify(), the calling thread must own a lock on object mutex.

为了调用mutex.wait()or mutex.notify(),调用线程必须拥有对象互斥锁。

This exception is thrown if you call it without a preceding synchronized (mutex) { }

如果您在没有前面的情况下调用它,则会抛出此异常 synchronized (mutex) { }

Check out the nice animation of waitand notifyin this link : How do wait and notify really work?

在此链接中查看wait和的漂亮动画notify如何等待和通知真正起作用?

回答by Erhan Bagdemir

for wait(), notify() call, you need a synchronized code. try this:

对于 wait()、notify() 调用,您需要同步代码。试试这个:

synchronized (this) {
  try {
    this.wait();
   } catch (InterruptedException e) {
     e.printStackTrace();
   }
}


synchronized (this) {
   notify();
}

回答by Mikita Belahlazau

Try use Semaphorewith 0 initial permit. Semaphore mutex = new Semaphore(0);

尝试使用初始许可为 0 的信号量。信号量互斥量 = 新信号量(0);

in main mutex.release();

在主要 mutex.release();

in on click mutex.acquire();

在点击 mutex.acquire();

回答by anfy2002us

from javadoc waitThis method should only be called by a thread that is the owner of this object's monitorand for notifyThis method should only be called by a thread that is the owner of this object's monitor.

来自 javadoc wait此方法只能由作为此对象监视器所有者的线程调用,而对于notify此方法应仅由作为此对象监视器所有者的线程调用。

this means htat you have to synchronize using the mutex when using notify and wait

这意味着 htat 在使用通知和等待时您必须使用互斥锁进行同步

回答by Charlie Martin

You have to waitbefore you notify.

你必须wait在你面前notify

回答by AdrianRM

You have to synchronize on the mutex to call notify and wait

您必须在互斥锁上进行同步才能调用通知并等待

回答by Taylor

You can either look at using more complex lock objects, or simply munch the exception in a try/catch block. The later is definitely "quick and dirty".

您可以考虑使用更复杂的锁对象,也可以简单地在 try/catch 块中处理异常。后者绝对是“又快又脏”。

For a more advance locking objects, take a look at http://download.oracle.com/javase/tutorial/essential/concurrency/newlocks.html

有关更高级的锁定对象,请查看http://download.oracle.com/javase/tutorial/essential/concurrency/newlocks.html