package hh;import java.util.concurrent.locks.*;
public class hh 
{
public static void main(String[] args)
{
info i = new info();

producer pro = new producer(i);

customer con = new customer(i);

Thread t1 = new Thread (pro);

Thread t2 = new Thread (con);

t1.start();

t2.start();
}
}
class info
{
private String name="张三";

private String content="傻逼";

private boolean flag = false;

Lock lk = new ReentrantLock();

Condition con = lk.newCondition();

public void setName(String name)
{
this.name = name;
}
public void setContent(String content)
{
this.content = content;
}

public String getName()
{
return name;
}

public String getContent()
{
return content;
}

public void set (String name ,String content)throws InterruptedException
{
lk.lock();
try
{
if(!flag)
con.await();
else

this.setName(name);
this.setContent(content);
flag = false;
con.signal();

finally
{
lk.unlock();
}
}
public  void get () throws InterruptedException
{
lk.lock();
try
{
if(flag)
con.await();
else
System.out.println("姓名是:"+this.getName()+"  类型是"+this.getContent());
flag = true;
con.signal();
}
finally{lk.unlock();}
}
}
class producer implements Runnable
{
private info i;

boolean flag = false;

public producer(info i)
{
this.i = i;
}

public void run()
{
for(int x = 0; x<50;x++)
{
if(flag)
{
try 
{
i.set("张三", "傻逼");
}
catch (InterruptedException e) 
{
e.printStackTrace();
}

flag = false;
}
else
{
try
{
i.set("李四", "帅哥");
}
catch (InterruptedException e)
{
e.printStackTrace();
}

flag = true;
}
}
}
}
class customer implements Runnable
{
private info i;

public customer(info i)
{
this.i = i;
}

public void run()
{
while(true)
{
try
{
i.get();

catch (InterruptedException e) 
{
e.printStackTrace();
}
}
}
}