非法监控状态异常 [英] Illegal monitor state exception
问题描述
如何轮询线程传递到另一个线程处理。
在控制器类有一个主要方法和线程池的程序执行众生:
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:
主类控制器
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();
}
}
里面有螺纹的proccesor类中的方法
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();
}
}
我的任务,问题是:
1,控制器应处理两个线程轮询和放大器;处理器和它应该只调用轮询和放大器;处理器线程
1.The controller should handle both the threads Poller & processor and it should only call the poller & processor thread
你们现在我的问题是如何使轮询线程等待3秒,paralelly通知处理器。
2.Now My question is how to make the poller thread wait for 3 seconds and paralelly notify the processor.
我得到的错误如下:
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)
如何在这里实现异步处理?
推荐答案
您需要阅读类似的这首的。不能使用的wait()
未持有对象的监视器。此外,从短短的一瞥,它看起来像你在多线程环境中使用非最终静态成员。尽量使线程安全的。
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屋!