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();
}
}
文章整理:站长天空 网址:http://www.z6688.com/
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!




