package com.itheima;
//定义一个商品类
class Goods
{
String name;
String use;
}
//定义生产者类,用来生产商品
class Producer implements Runnable
{
Goods good;
Producer(Goods good)
{
this.good = good;
}
@Override
public void run()
{
boolean flag = true;
while(true)
{
if(flag)
{
//生产者生产一个篮球
good.name = "basketball";
good.use = "play";
flag = false;
}
else
{
//生产者生产一个面包
good.name = "面包";
good.use = "吃的";
flag = true;
}
}
}
}
//定义消费者类,用来消费商品
class Consumer implements Runnable
{
Goods good;
Consumer(Goods good)
{
this.good = good;
}
@Override
public void run()
{
while(true)
{
//消费者消费商品
System.out.println(good.name + "---" + good.use);
}
}
}
public class Test027
{
/**
* 有一个商品,生产者生产出来什么东西,消费者就消费什么东西。
* @param args
*/
public static void main(String[] args)
{
Goods good = new Goods();
Producer producer = new Producer(good);
Consumer consumer = new Consumer(good);
new Thread(producer).start();
new Thread(consumer).start();
}
}
|
|