2016-02-27 85 views
-1

我正在學習多線程。我正在實施生產者和消費者問題。我堅持的情況下,我想要的是,當我按除鍵盤的整數之外的任何東西,所有我的線程應該死亡,並且線程沒有使用內存。請有您的寶貴意見來幫助我實現它。以下是我使用的所有代碼。停止等待線程條件

package com.java.concurrency;

public class ThreadSignaling { 
     private int i = -1; 
     private boolean valueSet = false; 
     private boolean stopFlag = false; 

     public void put(int value) { 
     synchronized (this) { 
      while (valueSet) { 
      if (stopFlag) { 
       System.out.println("Byeeeeeeeeeeeee"); 
       break; 
      } 
      try { 
       this.wait(); 
      } catch (InterruptedException e) { 
       System.out.println("InterruptedException while waiting in put() : " + e); 
      } 
      } 
      this.i = value; 
      this.valueSet = true; 
      System.out.println("Value put : " + this.i); 
      this.notify(); 
     } 
     } 

     public void get() { 
     synchronized (this) { 
      while (!valueSet) { 
      if (stopFlag) { 
       System.out.println("Byeeeeeeeeeeeee"); 
       break; 
      } 
      try { 
       this.wait(); 
      } catch (InterruptedException e) { 
       System.out.println("InterruptedException while waiting in get() : " + e); 
      } 
      } 
      System.out.println("Value get : " + this.i); 
      valueSet = false; 
      this.notify(); 
     } 
     } 

     public void finish() { 
     synchronized (this) { 
      stopFlag = true; 
      this.notifyAll(); 
     } 
     } 

    } 

    public class Producer implements Runnable { 
    private ThreadSignaling sharedObj = null; 
    private final Scanner input = new Scanner(System.in); 

    public Producer(ThreadSignaling obj) { 
    this.sharedObj = obj; 
    } 

    @Override 
    public void run() { 
    int value = -1; 
    System.out.println("Press Ctrl-c to stop... "); 
    while (true) { 
     System.out.println("Enter any integer value : "); 
     if (input.hasNextInt()) { 
     value = input.nextInt(); 
     } else { 
     this.sharedObj.finish(); 
     return; 
     } 
     this.sharedObj.put(value); 
     try { 
     Thread.sleep(500); 
     } catch (InterruptedException e) { 
     System.out.println("InterruptedException while sleeping" + e); 
     } 
    } 
    } 
} 
public class Consumer implements Runnable { 
    private ThreadSignaling sharedObj = null; 

    public Consumer(ThreadSignaling obj) { 
    this.sharedObj = obj; 
    } 

    @Override 
    public void run() { 
    while (true) { 
     this.sharedObj.get(); 
    } 
    } 
} 
public class MainThread { 

    public static void main(String[] args) { 
    ThreadSignaling sharedObj = new ThreadSignaling(); 
    Producer in = new Producer(sharedObj); 
    Consumer out = new Consumer(sharedObj); 
    Thread t1 = new Thread(in); 
    Thread t2 = new Thread(out); 
    t1.start(); 
    t2.start(); 
    } 
} enter code here 
+0

你有什麼問題? – ChiefTwoPencils

+0

我不能停止除了整數之外的任何其他輸入的所有等待線程 –

+1

我希望能夠在你問這裏之前嘗試解決它時,能夠找到你能夠找到的更多見解/細節。 – ChiefTwoPencils

回答

0

您的代碼的問題是您沒有消費者退出條件。消費者的run()方法將永久運行,並在共享對象上重複調用get調用。 您需要做的是讓消費者意識到生產者已經在共享對象中設置了stopFlag。如果stopFlag爲真,則消費者中的循環也應該結束。有幾種方法可以做到這一點:

  • redefine get方法返回stopFlag的值;
  • 定義一個新的方法來返回stopFlag的值;

在這兩種情況下,都要在Consumer.run()中進行測試,如果該值爲true,則只需執行一次返回,以便無限循環結束。