public synchronized void out() {
if (!flag)
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(name+"........"+sex);
flag = false;
this.notify();
}
}
class Input1 implements Runnable {
private Res1 r;
Input1(Res1 r) {
this.r = r;
}
public void run() {
int x = 0;
while (true) {
if (x == 0)
r.set("mike", "man");
else
r.set("丽丽", "女女女女女");
x = (x + 1) % 2;
}
}
}
class Output1 implements Runnable {
private Res1 r;
Output1(Res1 r) {
this.r = r;
}
public void run() {
while (true) {
r.out();
}
}
}
public class InputOutputDemo1 {
public static void main(String[] args) {
Res1 r = new Res1();
new Thread(new Input1(r)).start();
new Thread(new Output1(r)).start();
/*Input1 in = new Input1(r);
Output1 out = new Output1(r);
Thread t1 = new Thread(in);
Thread t2 = new Thread(out);
t1.start();
t2.start();*/
}
}