多线程通信

/**
 * 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();


}


}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值