package test07;
/*
*需求:
*资源有姓名性别两个线程
*一个负责给姓名性别赋值
*一个负责获取姓名性别的值
*
*要求1 :解决程序“妖”的问题
*加入同步必须保证同一个锁 解决“妖”的问题
*
*要求2 : 实现正确数据的间隔输出
*使用等待唤醒机制 wait() notify() notifyAll()
*对于等待唤醒机制都需要判断,定义标记。
*
*要求3 :对代码进行重构
*将name sex 私有化, 资源类提供对其访问的方法
*
*要求4 : 将程序改为lock condition接口
*lock替代了同步函数或者同步代码块
*condition替代了监视器方法
*await()
*signal()
*singalAll()
*/
public class Demo16_多线程之间的通信 {
public static void main(String[] args) {
Resource16 r =new Resource16();
Input in = new Input(r);
Output out = new Output(r) ;
Thread t1 = new Thread(in);
Thread t2 = new Thread(out);
t1.start();
t2.start();
}
}
class Resource16
{
private String name ;
private String sex ;
//定义标记
boolean flag = false ;
//赋值功能
public synchronized void set(String name,String sex) {
if(flag)
try{this.wait();}
catch(InterruptedException e){}
this.name = name;
this.sex = sex;
flag = true;
this.notify();
}
//二者用同一个锁,在同一个类中,可以直接用同步函数
//获取值功能
public synchronized void out(){
if(!flag)
try{this.wait();}
catch(InterruptedException e){}
System.out.println(name+”…”+sex);
flag = false ;
this.notify();
}
}
//赋值线程任务
class Input implements Runnable
{
private Resource16 r;
Input(Resource16 r)
{
this.r = r ;
}
public void run()
{
int x = 0 ;
while(true)
{
// synchronized (r) {
// if(r.flag)//(生产消费问题)有东西,flag = true 等待(需要抛异常)
// try{r./表示所需要的锁/wait();}
// catch(InterruptedException e){}
// 同步并不能解决问题,因为同步的前提是多个(1)线程使用同一个锁(2)
// 不能使用的原因是:两个线程在不同的类中,并且同步的对象()锁不同:那么使用相同的对象(锁)就可以了 例如:r Resource16.class
if(x==0)
{
// r.name = “zhan er” ;
// r.set(“nan”) ;
r.set(“张飞”, “男”);
}
else
{
// r.name = “rose” ;
// r.set(“nv”) ;
r.set(“rose”, “女女女女女女”);
}
// r.flag = true ;
// r.notify();
// }
x = (x+1)%2 ;//实现切换
}
}
}
//获取值线程任务
class Output implements Runnable
{
private Resource16 r ;
Output(Resource16 r)
{
this.r = r ;
}
public void run()
{
while(true)
{
/* synchronized (r ) {
if(!r.flag)//(生产消费问题)有东西,flag = true 等待(需要抛异常)
try{r.表示所需要的锁wait();}
catch(InterruptedException e){}
System.out.println(r.name+"...."+r.get());
r.flag = false ;
r.notify();
}*/
r.out();
}
}
}
将程序改为lock condition接口
package test07;
import java.util.concurrent.locks.*;
import java.util.concurrent.locks.ReentrantLock;
public class Demo16_多线程之间的通信02 {
public static void main(String[] args) {
Resource1621 r =new Resource1621();
Input1621 in = new Input1621(r);
Output1621 out = new Output1621(r) ;
Thread t1 = new Thread(in);
Thread t2 = new Thread(out);
t1.start();
t2.start();
}
}
/**要求4 : 将程序改为lock condition接口
*lock替代了同步函数或者同步代码块
*condition替代了监视器方法
*await()
*signal()
*singalAll()*/
class Resource1621
{
private String name ;
private String sex ;
//定义标记
boolean flag = false ;
//创建锁对象
private final Lock lock = new ReentrantLock();
//通过所对象获取监视器对象
private Condition con = lock.newCondition();
//赋值功能
public void set(String name,String sex) {
lock.lock();
try{
if(flag)
try{con.await();}
catch(InterruptedException e){}
this.name = name;
this.sex = sex;
flag = true;
con.signal();}finally{
lock.unlock();
}
}
//二者用同一个锁,在同一个类中,可以直接用同步函数
//获取值功能
public void out(){
lock.lock();
try{
if(!flag)
try{con.await();}
catch(InterruptedException e){}
System.out.println(name+"..."+sex);
flag = false ;
con.signal();}finally{
lock.unlock();
}
}
}
//赋值线程任务
class Input1621 implements Runnable
{
private Resource1621 r;
Input1621(Resource1621 r)
{
this.r = r ;
}
public void run()
{
int x = 0 ;
while(true)
{
if(x==0)
{
r.set("张飞", "男");
}
else
{
r.set("rose", "女女女女女女");
}
x = (x+1)%2 ;//实现切换
}
}
}
//获取值线程任务
class Output1621 implements Runnable
{
private Resource1621 r ;
Output1621(Resource1621 r)
{
this.r = r ;
}
public void run()
{
while(true)
{
r.out();
}
}
}