2013-02-15 2 views
0

폴링중인 스레드 풀을 처리하는 동안 다른 스레드는 처리 후 새 들어오는 데이터를 업데이트해야합니다. 주요 방법 및 스레드 풀을 가진 컨트롤러 클래스 프로그램 실행 존재 :처리 스레드 풀 및 wait notifyALL()

메인 클래스 컨트롤러

public static void main(String[] args) throws InterruptedException { 
    RunnableController controller = new RunnableController(); 
    Accumulator acque = new Accumulator(); 
     controller.initializeDb(); 
     controller.initialiseThreads(acque); 
     controller.initialUpdate(acque);  

} 
폴링 클래스

실행 방법

 public void run() { 
    int seqId = 0; 
    List<KpiMessage> list = null; 
    while(true) { 
     try{ 
      list = fullPoll(seqId); 
      if (!list.isEmpty()) { 
      accumulator.manageIngoing(list);    
      } 
     } catch (Exception e){ 
      e.printStackTrace();     
     } 
    } 
} 

    public List<KpiMessage> fullPoll(int lastSeq) throws Exception { 
    Statement st = dbConnection.createStatement(); 
    System.out.println("Polling"); 
ResultSet rs = st.executeQuery("Select * from msg_new_to_bde where ACTION = 804 and SEQ >" + 
    lastSeq + "order by SEQ DESC"); 

    return pojoCol; 
} 

실행 방법 처리 :

 public void run() { 

    try { 
     generate(accumulator.outgoingQueue); 
     accumulator.manageOutgoing(accumulator.outgoingQueue, dbConnection); 
     } catch (Exception e) { 
     e.printStackTrace(); 
    } 
    } 
    } 

D

public boolean isUsed = false; 
    public synchronized void manageIngoing(List<KpiMessage> list){ 

    if(this.isUsed){     
     try { 
      wait(); 
      System.out.println("first wait"); 
     } catch (Exception e1) { 
      e1.printStackTrace(); 
     } 
    } 
    System.out.println("recived pass after update"); 
    this.getIncomingQueue().addAll(list); 
    //incoming queue copied to outgoing queue 
    this.setOutgoingQueue(this.getIncomingQueue());    
    System.out.println("waiting"); 
    System.out.println("new incoming message"); 
    this.isUsed = false; 
    notifyAll(); 

} 

/** 
* Method which handles synchronization using wait and notify for outgoing messages after 
    polling 
* @param outgoingQueue 
* @param dbConnection 
*/ 

    public synchronized void manageOutgoing(Collection<KpiMessage> outgoingQueue, Connection 
dbConnection){ 
    if(!this.isUsed) 
    { 
     try { 
      System.out.println("second wait"); 
      wait(); 
     } catch (InterruptedException e) { 
      e.printStackTrace(); 
     } 
    } 
    this.isUsed = true; 
     DBhandler dbhandler = new DBhandler(); 
    try { 
     dbhandler.updateDb(getOutgoingQueue(), dbConnection); 
    } catch (SQLException e) { 
     e.printStackTrace(); 
    } 
    notifyAll(); 
} 
} 

내 작업 및 질문은 다음과 같습니다 :

1. 컨트롤러가 처리해야 두 스레드 폴러 & 프로세서와 마지막으로

public void updateDb(Collection<KpiMessage> updatedQueue, Connection dbConnection) throws 
    SQLException{ 
    for(KpiMessage pojoClass : updatedQueue){ 
      Statement stmtupd = dbConnection.createStatement(); 
     System.out.println("Updating"); 
    String query = "UPDATE msg_new_to_bde SET KEYINFO1= 'Processed', KEYINFO2 = 'Updated' 
    WHERE ACTION = 804"; 

      stmtupd.executeUpdate(query);**My Execution stops here** 

모든 큐를 maintaing에 대한 누적 클래스 atabase accumulator는 들어오고 나가는 대기열을 처리하고 마지막으로 처리 후 DB를 업데이트하기 위해 업데이트 된 대기열에 입력합니다.

2. 여기 내 클래스는 한 번 폴링하고 업데이트 할 수 없으며 실행이 중지됩니다.

3. 내 wait(), notifyALL() 핸들이 올바른지 확인하십시오.

반복 폴링 및 업데이트를 어떻게 수행합니까?

+1

죄송 합니다만 한꺼번에 여러 가지 질문으로 나누거나 따로 따로 나누어 줄 필요가 있습니다. – Jatin

+0

@JAtin 내가 말한대로 업데이트했습니다 ... – Babu

+0

@Jatin 제 발췌 문장은 업데이트 시점에 정확히 멈 춥니 다. ... – Babu

답변

3

5 가지 질문으로 구성된이 복잡한 설정에서 모든 것에 대해 완전한 답이 없을 것입니다. 이들을 기다리는 동안 java.util.concurrent가 제공해야하는 항목, 특히 읽기 및 쓰기 차단을 지원하는 동시 수집을 읽어야합니다. JDK 클래스가 충분하지 않은 경우에만 wait()notify()을 사용하십시오.

+0

고맙습니다. 제 프로그램에서 업데이트를 그냥 멈추고 스레드가 업데이트를 기다리고 있습니다 ... – Babu

+1

http://docs.oracle에서 Java 동시성에 대한 다소 좋은 소개를 찾을 수 있습니다. com/javase/tutorial/essential/concurrency / – rudolfson

관련 문제