标签: illegalmonitorstateexcep

为什么notifyAll()在Integer上同步时会引发IllegalMonitorStateException?

为什么这个测试程序导致了java.lang.IllegalMonitorStateException

public class test {
    static Integer foo = new Integer(1);
    public static void main(String[] args) {
        synchronized(foo) {
            foo++;
            foo.notifyAll();
        }
        System.err.println("Success");
    }
}
Run Code Online (Sandbox Code Playgroud)

结果:

Exception in thread "main" java.lang.IllegalMonitorStateException
        at java.lang.Object.notifyAll(Native Method)
        at test.main(test.java:6)
Run Code Online (Sandbox Code Playgroud)

java synchronization illegalmonitorstateexcep notify

38
推荐指数
1
解决办法
3万
查看次数

什么可以导致同步块内的IllegalMonitorStateException?

我们今天遇到了一个非常令人惊讶的例外 在同步块内部,我们调用wait()并抛出它IllegalMonitorStateException.是什么导致这个?

这是在经过充分测试的开源代码中发生的:http: //svn.apache.org/viewvc/river/jtsk/trunk/src/com/sun/jini/jeri/internal/mux/Mux.java?view=markup #L222

我们消除了明显的原因:

  • 我们是否在正确的变量上同步?是的,它就是muxLock
  • 它是一个可变变量吗?不,muxLock是最后的
  • 我们使用任何可能影响监视器行为的奇怪的"-XX:"JVM标志吗?不,但我们正在通过JNI启动嵌入在C++应用程序中的JVM.
  • 这是一个奇怪的JVM吗?不,这是Sun的1.6.0_25 win/x64 JRE
  • 这是一个已知的JVM错误吗?在http://bugs.sun.com/bugdatabase上找不到任何相关内容

所以,我试图想出更多牵强附会的解释.

  • 一个未被发现的内存不足错误导致监视器状态被搞砸了?我们正在考虑这个问题,但我们还没有看到内存错误的证据.

更新:(根据评论)

我还从stacktrace和断点验证了当抛出异常时线程确实在synchronized块内.事实并非如此,其他一些不相关的代码会发出异常(除非有些东西真的让Eclipse混乱!)

java synchronization illegalmonitorstateexcep

30
推荐指数
1
解决办法
2390
查看次数

抛出:IllegalMonitorStateException

什么可能导致我在此代码中得到IllegalMonitorStateException

synchronized(syncCount){
    syncCount--;
    syncCount.notify();
}
Run Code Online (Sandbox Code Playgroud)

我有点困惑,因为据我所知,运行线程必须在对象上监视通知被调用.在我看来,我的代码不会出错,但不知何故.

java multithreading illegalmonitorstateexcep notify

13
推荐指数
1
解决办法
973
查看次数

awaitTermination函数的IllegalMonitorStateException

我在Java中使用线程时遇到问题(我对Java中的线程经验不多,但在C++中很多,所以我理解线程的基本概念).我在Java中使用了线程的示例代码,接下来是代码:

        ExecutorService executor = Executors.newFixedThreadPool(machines.size());

        for (Machine m : machines) {
            Runnable worker = new restartMachine(m.dataformachine());
            executor.execute(worker);
        }

        executor.shutdown();
        try {
            executor.awaitTermination(15, TimeUnit.MINUTES);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
Run Code Online (Sandbox Code Playgroud)

restartMachine() 正在重新启动某些远程计算机,并且计算机没有以任何方式连接,传递给Runnable的数据是给定计算机的IP地址,以及在该计算机上本地执行的命令.

接下来我正在执行这段代码时遇到错误:

java.lang.IllegalMonitorStateException
 at java.util.concurrent.locks.ReentrantLock$Sync.tryRelease(ReentrantLock.java:155)
 at java.util.concurrent.locks.AbstractQueuedSynchronizer.release(AbstractQueuedSynchronizer.java:1260)
 at java.util.concurrent.locks.ReentrantLock.unlock(ReentrantLock.java:460)
 at java.util.concurrent.ThreadPoolExecutor.awaitTermination(ThreadPoolExecutor.java:1471) 
Run Code Online (Sandbox Code Playgroud)

从上面的代码调用函数awaitTermination()时抛出异常.据我所知,从我见过的各种例子来看,这段代码不应该有任何问题.

public boolean awaitTermination(long timeout, TimeUnit unit)
    throws InterruptedException {
    long nanos = unit.toNanos(timeout);
    final ReentrantLock mainLock = this.mainLock;
    mainLock.lock();
    try {
        for (;;) {
            if (runStateAtLeast(ctl.get(), TERMINATED))
                return true;
            if (nanos <= 0)
                return false;
            nanos = …
Run Code Online (Sandbox Code Playgroud)

java multithreading illegalmonitorstateexcep

10
推荐指数
1
解决办法
910
查看次数

IllegalMonitorStateException notify()和wait()

我有个问题.当我notify()在synchronized块中使用时,我有IllegalMonitorStateException.任何人都可以帮我解决这个问题吗?

我必须这样做,一个线程将发送到第二个线程char,然后这个线程必须等待,第二个线程打印此char.在第二个线程等待之后,第一个再次发送下一个char

Main.java:

import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
public class Main extends JFrame {

    Thread t1, t2;
    Consumer con;

    public Main() {
        con = new Consumer();
        startThreads();
    }

    private synchronized void startThreads() {
        t1 = new Thread(new Producent("grudzien", con));
        t1.start();
        t2 = new Thread(con);
        t2.start();
    }

    public class Producent implements Runnable {

        String m_atom;
        char[] atoms; …
Run Code Online (Sandbox Code Playgroud)

java illegalmonitorstateexcep notify wait

6
推荐指数
1
解决办法
2万
查看次数

那个IllegalMonitorStateException ......线程没有拥有该对象?不可能,只有一个

我有这个类,这是文件锁定实用程序的基本方法(不与OS的锁相互作用).我们的想法是拥有一个静态HashMap,它存储对应用程序使用二进制信号量的File对象的一对引用.首次访问新文件时,该对存储在变量中.问题是,.wait()行抛出抛出:IllegalMonitorStateException,我不明白为什么,因为我已经创建了一个项目来测试这个类只有一个线程,所以这是不可能的线程访问方法不拥有对象,不是吗?

public abstract class FileLocker {

    private static final HashMap<File, Semaphore> locksMap = new HashMap<>();

    public synchronized static final void getLock(final File file) {
        if (!FileLocker.locksMap.containsKey(file)) {
            FileLocker.locksMap.put(file, new Semaphore(1, Boolean.TRUE));
        }
        try {
            FileLocker.locksMap.get(file).wait();
        } catch (final InterruptedException e) {
            SysLogger.log(e, "ERR0", SysLogger.Level.CRASH);
        }
        if (file.isDirectory()) {
            for (final File f : file.listFiles()) {
                if (f.isDirectory()) {
                    FileLocker.getLock(f);
                }
            }
        }
    }

    public synchronized static final void releaseLock(final File file) {
        if (file.isDirectory()) {
            for (final File f : …
Run Code Online (Sandbox Code Playgroud)

java multithreading semaphore illegalmonitorstateexcep

1
推荐指数
1
解决办法
243
查看次数

同步块内的IllegalMonitorStateException

虽然我已经在synchronized块中写了等待.我到了IllegalMonitorStateException.那是什么原因呢?

package trials;

public class WaitNotifyTrial {

    public static void main(String[] args){
        Generator g=new Generator();
        g.start();
        System.out.println("Start");
        synchronized (g) {
                try {
                    g.wait();
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    System.out.println("Printing exception");
                    e.printStackTrace();
                }
                System.out.println(g.value);
        }
    }
}

class Generator extends Thread{

    int value;

    public void run(){
        synchronized (this) {
            value=10;
        }
        notify();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

java multithreading runtime-error illegalmonitorstateexcep

1
推荐指数
1
解决办法
721
查看次数

在Integer上同步时对notify()的IllegalMonitorStateException

我刚接触在Java中使用wait()和notify(),我得到一个IllegalMonitorStateException.

主要代码

public class ThreadTest {

    private static Integer state = 0;
    public static void main(String[] args) {

        synchronized(state) {
            System.out.println("Starting thread");

            Thread t = new Thread(new AnotherTest());
            t.start();

            synchronized(state) {
                state = 0;
                while(state == 0) {
                    try {
                        state.wait(1000);
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
                System.out.println("State is: " + state);
            }
        }
    }   

    public static class AnotherTest implements Runnable {

        @Override
        public void run() {
            synchronized(state) {
                state = 1;
                state.notify(); …
Run Code Online (Sandbox Code Playgroud)

java illegalmonitorstateexcep notify wait

1
推荐指数
1
解决办法
5061
查看次数

非法监控状态两个踏板等待时出现异常

HI编写了一个示例程序,用于测试java中的wait行为.

我的Runnable实现:

class ThreadWait implements Runnable {
    Object lock = new Object();
    ThreadWait(Object lock){
        this.lock = lock;
    }
    @Override
    public void run() {
        try {
            synchronized (lock){
                System.out.println("Started : "+Thread.currentThread().getName());
                wait();
                System.out.println("Completed : "+Thread.currentThread().getName());
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

在我的main喜欢中使用:

Object lock = new Object();
ThreadWait t1 = new ThreadWait(lock);
ThreadWait t2 = new ThreadWait(lock);
Thread a= new Thread(t1);
a.setName("A");
Thread b= new Thread(t2);
b.setName("B");
a.start();
b.start();
Run Code Online (Sandbox Code Playgroud)

运行此程序时,我收到此异常:

Exception in thread "A" Exception …
Run Code Online (Sandbox Code Playgroud)

java multithreading illegalmonitorstateexcep

1
推荐指数
1
解决办法
235
查看次数