有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java wait()和notify()相关问题

我只是想使用线程打印出从1到10。但我的代码会停在第一位。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();
    }
}

共 (2) 个答案

  1. # 1 楼答案

    while循环在一个对象上放置wait,两个线程为该对象进行通信

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

    使其成为实例变量

    private static boolean ToF=false;
    

    公共课家庭作业第三部分{

        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();
        }
    
    }
    
  2. # 2 楼答案

    I simply want to use thread to print out from 1 to 10. But my code will stop at number 1.

    [[另一个答案似乎解决了你的问题,但它没有解释发生了什么以及为什么解决方法有效。]]

    问题是,两个线程都在调用不同的对象上的synchronizenotify()wait()。当线程使用这些信号进行通信时,它们都需要共享同一个对象实例。您正在创建2InputOutput个对象,因此由于notify()调用丢失,两个线程都卡在了wait()

    class Input implements Runnable{
        ...
        // this is local to the Input class
        InputOutput io=new InputOutput();
    ...
    class Output implements Runnable{
        ...
        // this is a different instance 
        InputOutput io=new InputOutput();
    

    您应该执行以下操作:

    final InputOutput io = new InputOutput();
    Thread t1=new Thread(new Input(io));
    t1.start();
    Thread t2=new Thread(new Output());
    t2.start();
    ...
    private static class Input {
        private final InputOutput io;
        public Input(InputOutput io) { this.io = io; }
    ...
    private static class Output {
        private final InputOutput io;
        public Output(InputOutput io) { this.io = io; }
    ...
    

    那么InputOutput类都使用了InputOutput类的同一个实例。当它们在方法上调用synchronized时,它们锁定在同一个实例上,当它们调用wait()notify()时,信号被另一个线程看到