- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
嗨,我正在尝试使用 Java 套接字编程制作多人游戏。我的服务器正在使用多线程环境。问题是,我希望当两个玩家都连接到服务器时,他们就可以继续前进,否则不...这个原因我使用循环运行两次并接受来自两个客户端的连接。在循环中我使用线程对象并调用其等待方法。当循环完成时我调用 obj.notifyALl 方法,以便当前线程和前一个三人应该醒来,他们将开始工作但我遇到了这个异常......欢迎任何想法或建议......
最佳答案
简而言之:您正在等待或通知尚未同步的对象。
The current thread must own this object's monitor. The thread releases ownership of this monitor and waits until another thread notifies threads waiting on this object's monitor to wake up either through a call to the notify method or the notifyAll method. The thread then waits until it can re-obtain ownership of the monitor and resumes execution.
As in the one argument version, interrupts and spurious wakeups are possible, and this method should always be used in a loop:
synchronized (obj) {
while (<condition does not hold>)
obj.wait();
... // Perform action appropriate to condition
}
IllegalMonitorStateException - if the current thread is not the owner of the object's monitor.
恕我直言,您可能应该使用 concurrency library而不是等待/通知。
关于java - java中的IllegalMonitorStateException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12742156/
我是 Java 新手(也是 RoR 开发人员)。 我有一个简单的程序。球由球员共享。球应传给随机玩家。 好的,代码如下: class Ball { private int currentPl
我是否没有正确使用同步: 在下面的代码中我遇到了两个问题: 1.在将方法(designBusiness、createBusiness、sellBusiness)设置为 synchronized 时,就
我不完全理解wait和notify(Object)是如何工作的,结果我被迫瘦身将我的尝试记入以下代码部分。 Main.java: import java.util.ArrayList; class M
我有这个代码 if( id == 0 ||(id % 2) != 0){ System.out.println("test"); synchronized(lo
我是 Java 新手。我刚刚读了《java核心》这本书。我遇到了有关“条件和锁定”的问题。 我从书上敲了一段代码到eclipse中做了一些练习。 当我运行代码时,“sufficientFund.wai
我想模拟一个妈妈叫三个 child 吃饭,这是我的代码: class child extends Thread{ String name; mother mom; public
我正在尝试在新线程中调用方法,该方法将在使用 callable 延迟后返回一些东西。由于,它正在下降IllegalMonitorStateException 当我只创建类的实例,调用方法并且该方法将延
我已经使用 java 线程构建了生产者-消费者。生产者和消费者是两个不同的类,指的是单链表和对象锁。下面的实现有什么问题? Item Produced by Thread-0 Item 1 Item
我在我的代码中遇到了 IllegalMonitorStateException,我不确定为什么会收到它以及如何修复它。我当前的代码是并且错误发生在 try block 中: public stat
请向我解释为什么我的代码在等待函数中抛出 IllegalMonitorStateException ,据我所知,只有在同步部分未完成时才会发生这种情况? private void deliver(in
每当我调用pauseThread()时,它总是抛出IllegalMonitorStateException。 我在文档中注意到我需要拥有对象监视器才能使线程等待。 用这段代码 synchronized
我正在尝试学习java并发编程。请检查我的示例代码并帮助我理解为什么我会收到“java.lang.IllegalMonitorStateException”,即使我在同步上下文中调用了 wait()
我对监视器和条件变量不熟悉。我在监视器中使用锁定和条件变量。 public class Monitor { private final int piNumberOfPhilosopher
所以我正在尝试编写一个程序来打印出以下输出: 44 33 22 11 该程序应该是多线程的,并且必须使用锁来防止竞争条件。它还必须利用 Condition,以便当线程想要打印的数字与变量 thread
我对多线程了解甚少。 我期望下面的程序能够完美运行,但它不起作用并产生以下异常。 Exception in thread "Thread-1" java.lang.IllegalMonitorStat
我不完全理解wait和notify(Object的)的工作方式,因此,我不得不将尝试减少到以下代码部分。 Main.java: import java.util.ArrayList; class
void waitForSignal(){ Object ob =new Object(); synchronized (Thred.currentThread()) {
我收到一个 IllegalMonitorStateException,我觉得我无法阅读更多内容,所以我问是否有人知道我做错了什么。在计数器(门类中线程之间的共享资源)陷入等待状态后,我在程序运行结束时
我有一个程序可以模拟一艘船的大门。它们在线程中运行。这个想法是让它们在 run 方法中的随机时刻运行和暂停,以模拟路过的人。这是由所有线程完成的,同时主线程正在等待通知并检查船是否已满,当线程通知他们
我想在线程中运行的 Vector 中使用同步 我尝试使用两种方法,一种用于等待,另一种用于通知 我想从静态类调用notify方法这是我的线程代码: import java.io.IOException
我是一名优秀的程序员,十分优秀!