Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/unix/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 根据条件停止等待线程_Java_Multithreading - Fatal编程技术网

Java 根据条件停止等待线程

Java 根据条件停止等待线程,java,multithreading,Java,Multithreading,我正在学习多线程。我正在实施生产者和消费者问题。我被困在这样一个场景中:当我按下键盘上除integer以外的任何键时,我的所有线程都会死掉,线程没有使用内存。请您提供宝贵的意见,帮助我实现这一目标。下面是我正在使用的所有代码 包com.java.concurrency public class ThreadSignaling { private int i = -1; private boolean valueSet = false; private

我正在学习多线程。我正在实施生产者和消费者问题。我被困在这样一个场景中:当我按下键盘上除integer以外的任何键时,我的所有线程都会死掉,线程没有使用内存。请您提供宝贵的意见,帮助我实现这一目标。下面是我正在使用的所有代码

包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

代码的问题在于您没有消费者的退出条件。使用者的
run()
方法将永远运行,并且在对共享对象重复执行
get
调用时。 您需要做的是让消费者知道生产者已在共享对象中设置了
stopfag
。如果
stopFlag
为真,则使用者中的循环也应完成。有几种方法可以做到这一点:

  • 重新定义
    get
    方法返回stopFlag的值
  • 定义一个新方法,只返回stopFlag的值

在这两种情况下,在Consumer.run()中进行测试,如果该值为true,只要做一个返回,无限循环就结束了。

你的问题是什么?除了integerI,我无法停止任何其他输入上所有等待的线程。我希望在你提出问题之前,能对你在尝试解决它时能找到的内容有更多的了解/细节。我实现了一个简单的生产者-消费者问题。我通过生产者将用户输入带入变量中,消费者通过同一个变量使用它。我想要的是,当用户提供任何其他非整数输入时,应该停止使用者线程和生产者线程的等待。我在尝试解决它时遇到了infinte循环