java - wait() 和 notification() 相关问题

标签 java multithreading java-threads

我只是想使用线程打印从1到10。但是我的代码将在数字1处停止。input()将提供从1到10的变量,而output()将打印出它们。首先执行input(),然后执行output()。之后 for() 将确保它们将开始另一个迭代。

class InputOutput{
    private static int i=0;
    private static boolean ToF=false;

    synchronized void output(){
        try{
            while(!ToF){
                notify();
                wait();
            }
        }
            catch(InterruptedException e){
                e.printStackTrace();
            }
        System.out.println("Output: "+i);
        ToF=false;
        notify();
    }
    synchronized void input(){
        try{
            while(ToF){
                notify();
                wait();
            }
        }
            catch(InterruptedException e){
                e.printStackTrace();
            }
        i++;
        ToF=true;
        notify();
    }
    class input implements Runnable{
    private int i=1;
    InputOutput io=new InputOutput();
    public void run(){
        for(i=1;i<=10;i++)
            io.input();
    }
    }
    class output implements Runnable{
    private int i=1;
    InputOutput io=new InputOutput();
    public void run(){
        for(i=1;i<=10;i++)
            io.output();
    }
    }
    public class Homework07Part3 {

    public static void main(String[] args) {
        Thread t1=new Thread(new input());
        t1.start();
        Thread t2=new Thread(new output());
        t2.start();
    }
}

最佳答案

while 循环将等待两个线程通信的单个对象

while(ToF){
           //dont put notify here.
            notify();
            wait();
        }

使其成为实例变量

private static boolean ToF=false;

公共(public)课Homework07Part3{

    public static void main(String[] args) {
        InputOutput io = new InputOutput();
        Thread t1 = new Thread(new input(io));
        t1.start();
        Thread t2 = new Thread(new output(io));
        t2.start();
    }

    private static class input implements Runnable {
        private int i = 1;
        private InputOutput io;

        public input(InputOutput io) {
            this.io = io;
        }

        public void run() {
            for (i = 1; i <= 10; i++)
                io.input();
        }
    }

    private static class output implements Runnable {
        private int i = 1;
        private InputOutput io;

        public output(InputOutput io) {
            this.io = io;
        }

        public void run() {
            for (i = 1; i <= 10; i++)
                io.output();
        }
    }
}

class InputOutput {
    private int i = 0;
    private boolean ToF = false;

    synchronized void output() {
        try {
            while (!ToF) {
                wait();
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("Output: " + i);
        ToF = false;
        notify();
    }

    synchronized void input() {
        try {
            while (ToF) {
                wait();
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        i++;
        ToF = true;
        notify();
    }

}

关于java - wait() 和 notification() 相关问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44114205/

相关文章:

java - 停止线程列表

java - 如何访问线程中 Runnable 对象的字段

java - 改变多维列表的特定行

java - 我无法在 pdf 中嵌入颜色空间以创建 Pdf/A

Java ant 脚本不显示警告

java - 在 C++ 中是否有 Java 'volatile' 的等价物?

java - 我的服务线程安全吗?

java - 我有一个 JSP 页面,单击该页面的提交后,第二个 jsp 未显示

java - 固定线程池中的线程阻塞

python - Python Tornado中的已连接客户端列表