java多线程

Posted on 2017-10-31 23:00  yacbo  阅读(163)  评论(0编辑  收藏  举报
public static void main()

{

Resource r  = new Resource();

Producer pro = new Producer(r);

Consumer con = new Consumer(r);

Thread t1 = new Thread(pro);

Thread t2= new Thread(con);

t1.start();

t2.start();

}

 

class Resource

{

private  String name;

private int count = 1;

private bolean flag = flag;

public synchroized void set(String name)

{

}

public synchroized void out()

{

} 

}

class Producer implements Runnable

{

private Resource res;

Producer(Resource res)

{

this.res = res;

}

public void run()

{

while(true)

{

res.set("shangpin");

}

}

}

 

class Consumer implements Runnable

{

private Resource res;

Producer(Resource res)

{

this.res = res;

}

public void run()

{

while(true)

{

res.out();

}

}

}

  

Copyright © 2024 yacbo
Powered by .NET 8.0 on Kubernetes