2016-07-04 3 views
0

내가 자바 사용하여 대기와의 notifyAll 연속 생산자 - 소비자에 대한 코드 아래에 작성했습니다, 실행은 대기의 두 번째 반복 후 실패하고생산자 소비자가

Exception in thread "Thread-1" java.util.NoSuchElementException: No line found 
    at java.util.Scanner.nextLine(Unknown Source) 
    at com.sharedResource.ConsumerProducerQueue.addElements(ConsumerProducerQueue.java:48) 
    at com.customRunnable.ProducerRunnable.run(ProducerRunnable.java:33) 
    at java.lang.Thread.run(Unknown Source) 

내 수업

다음과 같습니다 던지는 통지
package com.custom.datastructures; 

public class Queue { 

    private Link root; 

    private boolean isConsumerWorking = true; 
    private boolean isProducerWorking = true; 
    private int count =0; 

    public Link getRoot() { 
     return root; 
    } 

    public void setRoot(Link root) { 
     this.root = root; 
    } 

    public boolean isConsumerWorking() { 
     return isConsumerWorking; 
    } 

    public void setConsumerWorking(boolean isConsumerWorking) { 
     this.isConsumerWorking = isConsumerWorking; 
    } 

    public boolean isProducerWorking() { 
     return isProducerWorking; 
    } 

    public void setProducerWorking(boolean isProducerWorking) { 
     this.isProducerWorking = isProducerWorking; 
    } 

    public void push(int val) 
    { 
     if(root == null) 
     { 
      this.root = new Link(val); 
      return; 
     } 

     Link tempCurr = root; 
     Link prev = null; 
     while(tempCurr != null) 
     { 
      prev = tempCurr; 
      tempCurr = tempCurr.getNextLink(); 
     } 

     prev.setNextLink(new Link(val)); 
     ++count; 
     return; 
    } 

    public int pop() 
    { 
     if(root == null) 
     { 
      return -1; 
     } 

     Link tempCurr = root; 
     Link next = tempCurr.getNextLink(); 
     root = next; 
     --count; 
     return tempCurr.getVal(); 
    } 

    public boolean isEmpty() 
    { 
     return (root == null); 
    } 

} 

************************** 
package com.sharedResource; 

import java.util.Scanner; 

import com.custom.datastructures.Queue; 

public class ConsumerProducerQueue { 

    private Queue queue = new Queue(); 


    public Queue getQueue() { 
     return queue; 
    } 

    public void setQueue(Queue queue) { 
     this.queue = queue; 
    } 

    public synchronized void push(int val) 
    { 
     queue.push(val); 
    } 

    public synchronized int pop() 
    { 
     return queue.pop(); 
    } 

    public boolean isEmpty() 
    { 
     return queue.isEmpty(); 
    } 

    public synchronized void addElements() 
    { 
     Scanner sc = new Scanner(System.in); 

     while(true) 
     { 
      System.out.println("Please input the value you want to add "); 
      int val = 0; 
      if(sc.hasNextInt()) 
      { 
       val = sc.nextInt(); 
      } 
      else{ 
       System.out.println(sc.nextLine()); 
      } 
      System.out.println("The value entered by user is "+val); 

      if(val == -1) 
      { 
       break; 
      } 

      queue.push(val); 
     } 
     sc.close(); 
     this.notifyAll(); 
    } 

    public synchronized void displayElements() throws InterruptedException 
    { 
     if(queue.isEmpty()) 
     { 
      this.wait(); 
     } 

     while(!queue.isEmpty()) 
     { 
      System.out.println(queue.pop()); 
      System.out.println(); 
     } 

    } 


} 
******************************* 
package com.customRunnable; 

import com.sharedResource.ConsumerProducerQueue; 

public class ConsumerRunnable implements Runnable { 

    private ConsumerProducerQueue cpr = null; 

    public ConsumerProducerQueue getCpr() { 
     return cpr; 
    } 

    public void setCpr(ConsumerProducerQueue cpr) { 
     this.cpr = cpr; 
    } 

    public ConsumerRunnable() { 
     // TODO Auto-generated constructor stub 
     this.cpr = new ConsumerProducerQueue(); 
    } 

    public ConsumerRunnable(ConsumerProducerQueue cpr) 
    { 
     this.cpr = cpr; 
    } 

    @Override 
    public void run() { 
     try { 
      while(true) 
      { 
       cpr.displayElements(); 
      } 

     } catch (InterruptedException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
    } 

} 

***************************** 
package com.customRunnable; 

import java.util.Scanner; 

import com.sharedResource.ConsumerProducerQueue; 

public class ProducerRunnable implements Runnable { 

    private ConsumerProducerQueue cpr = null; 

    public ProducerRunnable() 
    { 
     this.cpr = new ConsumerProducerQueue(); 
    } 

    public ProducerRunnable(ConsumerProducerQueue cpq) 
    { 
     this.cpr = cpq; 
    } 

    public ConsumerProducerQueue getCpr() { 
     return cpr; 
    } 

    public void setCpr(ConsumerProducerQueue cpr) { 
     this.cpr = cpr; 
    } 

    @Override 
    public void run() { 
     while(true) 
     { 
     cpr.addElements(); 
     } 
    } 

} 

********************** 
package com.custom.threadTester; 

import com.customRunnable.ConsumerRunnable; 
import com.customRunnable.ProducerRunnable; 
import com.sharedResource.ConsumerProducerQueue; 

public class ThreadTester2 { 

    public static void main(String[] args) { 
     // TODO Auto-generated method stub 
     ConsumerProducerQueue cpr = new ConsumerProducerQueue(); 
     ConsumerRunnable cr = new ConsumerRunnable(cpr); 
     ProducerRunnable pr = new ProducerRunnable(cpr); 

     Thread t1 = new Thread(cr); 
     Thread t2 = new Thread(pr); 

     t1.start(); 
     t2.start(); 

    } 

} 

Link.java 클래스

package com.custom.datastructures; 

     public class Link { 

      public Link() 
      { 
       this.val = 12; 
      } 

      public Link(int val) 
      { 
       this.val = val; 
      } 

      private int val; 

      private Link nextLink = null; 

      public int getVal() { 
       return val; 
      } 

      public void setVal(int val) { 
       this.val = val; 
      } 

      public Link getNextLink() { 
       return nextLink; 
      } 

      public void setNextLink(Link nextLink) { 
       this.nextLink = nextLink; 
      } 

     } 
,
+0

"Java에서 wait 및 notifyAll을 사용하여 연속 Producer-Consumer 코드를 작성했습니다."- 이유가 무엇입니까? 이것에 대한 자바에서 훨씬 더 좋은 기법이 있습니다! – Fildor

+0

wait 및 notifyall의 기본 사항을 이해하려고 시도 할 때 괭이 스레드는 사용할 때 정확히 실행이 중지되고 다시 시작됩니다. – Ullas

+0

단계별로 단계별로 시도하고 고급 개념을 사용하여 코드를 개선합니다. 학습하십시오. – Ullas

답변

0

Scanner.nextLine()은 읽을 대기중인 다음 행이 없을 때 noSuchElementException을 던집니다. 루프에서 실행 중이므로 Scanner은 모든 입력을 사용하고 다음 행을 찾지 않고 예외를 throw합니다. 해결 방법은 매번 루프를 통해 읽을 때마다 다음 줄을 확인하는 것입니다.

관련 문제