class Resource
{
private String name="烤鸭";
private int count = 1;
public void set()
{
name = name + count;
count++;
System.out.println(Thread.currentThread().getName()+"...生产..."+this.name);
}
}
class Producer implements Runnable
{
Resource r =new Resource();
public void run()
{
while (true)
{
r.set();
}
}
}
class Test2
{
public static void main(String[] args)
{
Resource r = new Resource();
Producer p = new Producer();
Thread t0 = new Thread(p);
t0.start();
}
}
第二组代码
class Resource
{
private String name;
private int count = 1;
public void set(String name)
{
this.name = name + count;
count++;
System.out.println(Thread.currentThread().getName()+"...生产..."+this.name);
}
}
class Producer implements Runnable
{
Resource r =new Resource();
public void run()
{
while (true)
{
r.set("烤鸭");
}
}
}
class Test3
{
public static void main(String[] args)
{
Resource r = new Resource();
Producer p = new Producer();
Thread t0 = new Thread(p);
t0.start();
}
}