0

ポーリングスレッドを処理のために別のスレッドに渡す方法。プログラムの実行は、メインメソッドとスレッドプールを持つコントローラークラスにあります。

メインクラスのコントローラー

   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();
    }

}

proccesorクラスのスレッドを持つメソッド

    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) {

        }
    }

}

ポーラークラスとコントローラークラス

    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.コントローラーは、ポーラーとプロセッサーの両方のスレッドを処理する必要があり、ポーラーとプロセッサーのスレッドのみを呼び出す必要があります。

2.ここで私の質問は、ポーラースレッドを3秒間待機させ、並列にプロセッサに通知する方法です。

次のようなエラーが発生します。

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)

ここで非同期処理を実現するにはどうすればよいですか?

4

1 に答える 1

2

最初にこのようなものを読む必要があります。wait()オブジェクトのモニターを持たずに使用することはできません。また、一見すると、マルチスレッドコンテキストで非最終静的メンバーを使用しているように見えます。そのスレッドセーフを作成してみてください。

于 2013-02-08T12:21:17.107 に答える