package testJava.thread;
public class SellBuy {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Q q = new Q();
new Thread(new Producer(q)).start();
new Thread(new Comsumer(q)).start();
}
}
class Producer implements Runnable
{
Q q;
public Producer(Q q)
{
this.q=q;
}
public void run()
{
int i=0;
while(true)
{
if(i==0)
q.put("zhangsan","male");
else
q.put("lisi","femail");
i=(i 1)%2;
}
}
}
class Comsumer implements Runnable
{
Q q;
public Comsumer(Q q)
{
this.q=q;
}
public void run()
{
while(true)
{
q.get();
}
}
}
class Q
{
String name="unknown";
String sex="unkonwn";
boolean bFull=false;
public synchronized void put(String name,String sex)
{
if(bFull)
try {wait();} catch (Exception e) {}
this.name=name;
try {Thread.sleep(1);} catch (Exception e) {}
this.sex=sex;
bFull=true;
notify();
}
public synchronized void get()
{
if(!bFull)
try {wait();} catch (Exception e) {}
System.out.println(name ":" sex);
bFull=false;
notify();
}
}
上一篇: 快速Vsftpd配置手记
下一篇: 用Swing处理图片缩放和拖动!!!
文章整理:西部数码--专业提供域名注册、虚拟主机服务
http://www.west263.com
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!




