mikebartowski13 mikebartowski13 -4 years ago 44
Java Question

wait() and notify() related issue

I simply want to use thread to print out from 1 to 10. But my code will stop at number 1. input() will provide variable from 1 to 10, while output() will print out them. input() will be executed first and then output(). After that for() will make sure they will start another iteration.

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();
}

}

Answer Source

while loop you put wait on a single object for which two thread communication

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

Make it instance variable

private static boolean ToF=false;

public class 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();
    }

}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download