2013-02-08 4 views
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 클래스 용 thread를 가지는 메소드

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) 

어떻게 여기 비동기 처리를 달성하기 위해 다음과 같이

나는 오류를 얻을?

+0

'PollingSynchronizer'의 라인 76 무엇입니까? 어쩌면 당신은 그 클래스를 추가해야합니다. – Zhedar

+0

@Zhedar 실행 메소드에서 폴러 클래스의 스레드로 표시 한 Poller 클래스 ... – Babu

+1

지연은 한 번 처리 되었습니까? 아니면 폴러의 처리 루프마다 한 번 처리해야합니까? – Perception

답변

2

먼저 this과 같은 것을 읽어야합니다. 객체 모니터를 보유하지 않고 wait()을 사용할 수 없습니다. 또한, 잠깐 보았을 때 멀티 스레딩 컨텍스트에서 비 최종 정적 멤버를 사용하는 것처럼 보입니다. 스레드로부터 안전하게 만들어보십시오.

관련 문제