/**
* 1.
* wait()
* notify()
* notifyAll()
* 都使用在同步中,因为要对持有监视器(锁)的线程操作,所以都要使用在同步中,有同步才会有锁的存在
*
* 2.操作线程的方法为什么都定义在Object中呢?
* 因为这些方法在操作这些同步中的线程时,都必须要标识他们所操作的线程持有的锁
* 持有同一锁被等待的线程,可以被同一个锁上的notify唤醒;
* 不可以被不同锁的线程唤醒。
*
* 3.等待和唤醒必须是同一个锁(监视器)。
*
*4. 锁可以是任意对象。
*
*5. 可以被任意对象调用的方法定义在Object类中。
* */
//资源类
public class Resource {private String name;;
private String sex;
public boolean flag=false;
public Resource(){
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
}
//输入线程
public class Input implements Runnable {
private Resource res;
Input(Resource res) {
this.res = res;
}
@Override
public void run() {
int num = 0;
while (true) {
synchronized (res){
if(res.flag){
try {
res.wait();//冻结
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (num == 0) {
res.setName("MM");
res.setSex("女");
} else {
res.setName("桥");
res.setSex("男");
}
num = (num + 1) % 2;
res.flag=true;
res.notify(); // 唤醒线程
}
}
}
}
//输出线程
public class Output implements Runnable {
private Resource res;
Output(Resource res) {
this.res = res;
}
@Override
public void run() {
while (true) {
synchronized (res){
if(!res.flag){
try {
res.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out.println(res.getName() + "___" + res.getSex());
res.flag=false;
res.notify();
}
}
}
}
//测试线程
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
Resource res= new Resource();
Input in= new Input(res);
Output out = new Output(res);
Thread t1 = new Thread(in);
Thread t2 = new Thread(out);
t1.start();
t2.start();
}
}