public class ThreadMain {
public static void main(String args[]) {
Q q = new Q();
new Thread(new Producer(q)).start();
new Thread(new Cousumer(q)).start();
}
}
class Producer implements Runnable {
Q q;
public Producer(Q q) {
super();
this.q = q;
}
@Override
public void run() {
int i = 0;
while (true) {
if (i == 0) {
q.put("zheng", "male");
} else {
q.put("zhao", "female");
}
i = (i + 1) % 2;
}
}
}
class Q {
private String name = "unkown";
private String sex = "unkown";
private boolean bFull = false;
public class ThreadMain {
public static void main(String [] args){
Q q = new Q();
new Thread(new Producer(q)).start();
new Thread(new Cousumer(q)).start();
}
class Q {
private String name = "unknown";
private String sex = "unknown";
private boolean bFull = false;
//使用同步方法
public synchronized void put(String name,String sex){
notify();// 线程重新运行