在java中使用Object作为互斥锁

发布于 2024-11-08 16:30:11 字数 462 浏览 6 评论 0原文

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

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

我尝试做这样的事情:

Object mutex = new Object();

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

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

问题是如果抛出“llegalMonitorStateException”,则不会按下 mutex.notify() 的 playButton。您通常如何解决此类问题?

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

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?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

冷心人i 2024-11-15 16:30:11

根据 JavaDoc,

抛出

IllegalMonitorStateException
“表明一个线程有
试图等待一个对象的
监视或通知其他线程
等待对象的监视器而不
拥有指定的监视器。”

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

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

查看此链接中的 waitnotify 的精美动画:等待和通知如何真正发挥作用?

According to the JavaDoc,

IllegalMonitorStateException is 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."

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

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

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

2024-11-15 16:30:11

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

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


synchronized (this) {
   notify();
}

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

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


synchronized (this) {
   notify();
}
逆流 2024-11-15 16:30:11

尝试使用 Semaphore 与 0初始许可。
信号量互斥体 = new Semaphore(0);

在主 mutex.release();

中点击 mutex.acquire();

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

in main mutex.release();

in on click mutex.acquire();

感悟人生的甜 2024-11-15 16:30:11

来自javadoc
等待
此方法只能由该对象监视器的所有者的线程调用
以及通知
此方法只能由该对象监视器的所有者的线程调用。

这意味着在使用notify和wait时必须使用互斥体进行同步

from javadoc
wait
This method should only be called by a thread that is the owner of this object's monitor
and for notify
This method should only be called by a thread that is the owner of this object's monitor.

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

与之呼应 2024-11-15 16:30:11

通知之前,您必须等待

You have to wait before you notify.

戏舞 2024-11-15 16:30:11

您必须同步互斥体才能调用通知并等待

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

一影成城 2024-11-15 16:30:11

您可以考虑使用更复杂的锁对象,或者只是在 try/catch 块中咀嚼异常。后者绝对是“又快又脏”。

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

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".

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文