2012-12-17 7 views
0

새로운 스레드를 생성하는 ServerClass가 있습니다.다중 생산자/단일 소비자 다중 스레드

각 스레드에는 처리 할 데이터 블록이 제공됩니다. 각 스레드는 'x'데이터 청크를 처리하고 ServerClass에 일부 값을 반환합니다. ServerClass는 수집 된 값의 평균을 계산하고이를 스레드에 전달합니다. 그리고 스레드는 평균값을 사용하여 작업을 재개합니다. 전체 프로세스가 스레드에 의해 처리 될 때까지 동일한 프로세스가 반복됩니다.

필자는 이것을 작성했으며 실행할 때마다 다른 출력을 얻습니다. 누군가 내가 잘못하고있는 것을 설명 할 수 있습니까? lock 코드에서 private final Object lock = new Object(); 나머지 모든 방법이다

ServerClass 

public synchronized void put(double[] weight) 
{ 

     //System.out.println(Counter);  

     weights.add(weight); 

     if(Counter+1 == NoOfThreads) 

     {   

      averageWeights(); 

      notifyAll(); 

      Counter =0; 

     } else 

      try { 

       Counter++; 

       wait(); 

      } catch (InterruptedException e) { 

    e.printStackTrace(); 

     } 

} 

Threads Class 

refer // ServerClass Reference 

updatedweights // Averaged Value 

int slots = data.size()/batchCount; 

      for(int i=0; i<slots;i++) 

      { 

       double[] p = Algo(data,i*batchCount, (i+1)*batchCount, ServerClass.stepSize, labelIndex, refer.updatedweights); 

       refer.put(p); 

      } 
+0

코드에 어떤 문제가 있습니까? – Jayamohan

+0

코드 블록을 사용하여주십시오. – Bizmarck

+0

순환 장벽을 읽으면 여기서 잘 읽어 볼 수 있습니다. – Perception

답변

0
public void put(double[] weight, Threads t) 
{ 
    updatingWVector(weight); 
     if(checkCondition()) 
     {    
      averageWeights();    
      Counter =0; 
      synchronized(lock) { 
       //System.out.println(t.getId()); 
        lock.notifyAll(); 
      } 
     } else 
     { 

       try { 
        updateCounter(); 
        synchronized(lock) { 
         System.out.println(t.getId()); 
         lock.wait(); 
        } 
       } catch (InterruptedException e) { 
        e.printStackTrace(); 
       } 
     } 
} 

synchronized은 블록이다.