非法监视状态异常 [英] Illegal monitor state exception

查看:269
本文介绍了非法监视状态异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将轮询线程传递给另一个线程进行处理。
一个控制器类中的程序执行,它有一个main方法和一个线程池:



主类Controller

  public static void main(String [] args)throws InterruptedException {
RunnableController controller = new RunnableController();

System.out.println(incomingQueue.size());

controller.initializeDb();
controller.initialiseThreads();
System.out.println(Polling);
controller.initialUpdate();

}

具有轮询类线程的方法

  private void initialiseThreads()
{
try {

threadExecutorRead = Executors.newFixedThreadPool (10);
PollingSynchronizer reader = new PollingSynchronizer(incomingQueue,dbConnection);
threadExecutorRead.submit(reader);

} catch(Exception e){
e.printStackTrace();
}

}

处理程序类

  private void initialUpdate()
{
RunnableController.outgoingQueue = incomingQueue;
if((RunnableController.outgoingQueue)!= null){
try {
threadExecutorFetch = Executors.newFixedThreadPool(10);
MessageProcessor updater = new MessageProcessor(outgoingQueue,dbConnection);
threadExecutorFetch.submit(updater);
DBhandler dbhandler = new DBhandler();
dbhandler.updateDb(getOutgoingQueue());

} catch(Exception e){

}
}

}
/ pre>

Poller类和控制器类

  public void run(){// Poller类中的线程
int seqId = 0;
while(true){
列表< KpiMessage> list = null;
try {
list = fullPoll(seqId);
if(!list.isEmpty()){
seqId = list.get(0).getSequence();
incomingQueue.addAll(list);
this.outgoingQueue = incomingQueue;
System.out.println(等待);
System.out.println(new incoming message);
while(true){
wait(3000);
notify();
}
}
} catch(Exception e1){
e1.printStackTrace();
}
}
}


public void run(){//处理器类中的第二个线程
synchronized(this){
RunnableController.setOutgoingQueue(generate(outgoingQueue));
}
notify();
}
}

我的任务和问题是: strong>



1.控制器应处理线程Poller&处理器,它应该只调用poller&处理器线程



2.现在我的问题是如何让轮询线程等待3秒钟,paralelly通知处理器。



我收到错误如下:

  java.lang.IllegalMonitorStateException 
在java.lang.Object.wait(本机方法)
在PollingSynchronizer.run(PollingSynchronizer.java:76)
在java.util.concurrent.Executors $ RunnableAdapter.call(未知来源)
at java.util.concurrent.FutureTask $ Sync.innerRun(Unknown Source)
在java.util.concurrent.FutureTask.run(未知来源)
在java.util.concurrent.ThreadPoolExecutor.runWorker (未知来源)
在java.util.concurrent.ThreadPoolExecutor $ Worker.run(未知来源)
在java.lang.Thread.run(未知来源)

如何在这里实现异步处理

解决方案

您需要阅读这是第一个。您不能使用 wait()而不保持对象的显示器。此外,从简单的一眼看来,它看起来像在多线程上下文中使用非最终的静态成员。尝试使线程安全。


How to pass the polling thread to another thread for processing. The program execution beings in a controller class which has a main method and a thread pool:

The main class Controller

   public static void main(String[] args) throws InterruptedException {
    RunnableController controller = new RunnableController();

    System.out.println(incomingQueue.size());

    controller.initializeDb();
    controller.initialiseThreads();
    System.out.println("Polling");
    controller.initialUpdate(); 

}

The method which has the thread for Polling class

 private void initialiseThreads()
{       
    try {

        threadExecutorRead = Executors.newFixedThreadPool(10);
PollingSynchronizer reader = new PollingSynchronizer(incomingQueue,dbConnection);   
        threadExecutorRead.submit(reader);

    }catch (Exception e){
        e.printStackTrace();
    }

}

The method which has the thread for the proccesor class

    private void initialUpdate()
{
    RunnableController.outgoingQueue = incomingQueue;
    if((RunnableController.outgoingQueue)!= null){
        try {
            threadExecutorFetch = Executors.newFixedThreadPool(10);
        MessageProcessor updater = new MessageProcessor(outgoingQueue, dbConnection);
            threadExecutorFetch.submit(updater);
            DBhandler dbhandler = new DBhandler();
            dbhandler.updateDb(getOutgoingQueue());

        } catch (Exception e) {

        }
    }

}

The Poller Class and the controller class

    public void run() {// Thread in the Poller class 
    int seqId = 0;
    while(true) {
        List<KpiMessage> list = null;
        try {
            list = fullPoll(seqId);
            if (!list.isEmpty()) {
                seqId = list.get(0).getSequence();
                incomingQueue.addAll(list);
                this.outgoingQueue = incomingQueue;             
                System.out.println("waiting");
                System.out.println("new incoming message");
                while(true){
                        wait(3000);
                        notify();
                }
            }
        } catch (Exception e1) {
            e1.printStackTrace();
        }
    }
}


          public void run() {// Second thread in the Processor Class
    synchronized (this){
        RunnableController.setOutgoingQueue(generate(outgoingQueue));
    }
    notify();
}   
 } 

My task and Question is :

1.The controller should handle both the threads Poller & processor and it should only call the poller & processor thread

2.Now My question is how to make the poller thread wait for 3 seconds and paralelly notify the processor.

I get the error as follows:

java.lang.IllegalMonitorStateException
    at java.lang.Object.wait(Native Method)
    at PollingSynchronizer.run(PollingSynchronizer.java:76)
    at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)
    at java.util.concurrent.FutureTask$Sync.innerRun(Unknown Source)
    at java.util.concurrent.FutureTask.run(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)

How to achieve asynchronous processing here?

解决方案

You need to read something like this first. You cannot use wait() without holding the object's monitor. Also, from a short glance, it looks like you use a non-final static member in a multithreading context. Try to make that thread-safe.

这篇关于非法监视状态异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆