2016-02-27 162 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,则只需执行一次返回,以便无限循环结束。