package hl.test;
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Food f=new Food();
Producter p=new Producter(f);
Consumer c=new Consumer(f);
new Thread(p).start();
new Thread(c).start();
}
}
class Producter implements Runnable
{
private Food food;
public Producter(Food food)
{
this.food=food;
}
@Override
public void run() {
// TODO Auto-generated method stub
for (int i = 0; i < 100; i++)
{
if(i%2==0)
{
food.set("A", "有益");
}
else
{
food.set("B", "有害");
}
}
}
}
class Consumer implements Runnable
{
private Food food;
public Consumer(Food food)
{
this.food=food;
}
@Override
public void run() {
// TODO Auto-generated method stub
for (int i = 0; i < 100; i++)
{
food.get();
}
}
}
class Food
{
private String name;
private String content;
private boolean flag=true;//true表示可以生产,不能拿走;false表示可以消费,不能生产
//生产产品
public synchronized void set(String name,String content)
{
if(!flag)
{
try {
this.wait(); //让当前线程进入线程池等待,没有指定时间,需要其他线程唤醒,释放对象锁,让出CPU
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
this.setName(name);
try {
Thread.sleep(300);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
this.setContent(content);
flag=false; //false表示可以消费
this.notify();//唤醒该监视器上的一个线程
}
public synchronized void get()
{
if(flag)
{
try {
this.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
try {
Thread.sleep(300);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(this.getName()+":"+this.getContent());
flag=true;
this.notify();
}
public Food() {
super();
// TODO Auto-generated constructor stub
}
public Food(String name, String content) {
super();
this.name = name;
this.content = content;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
@Override
public String toString() {
return "Food [content=" + content + ", name=" + name + "]";
}
}
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Food f=new Food();
Producter p=new Producter(f);
Consumer c=new Consumer(f);
new Thread(p).start();
new Thread(c).start();
}
}
class Producter implements Runnable
{
private Food food;
public Producter(Food food)
{
this.food=food;
}
@Override
public void run() {
// TODO Auto-generated method stub
for (int i = 0; i < 100; i++)
{
if(i%2==0)
{
food.set("A", "有益");
}
else
{
food.set("B", "有害");
}
}
}
}
class Consumer implements Runnable
{
private Food food;
public Consumer(Food food)
{
this.food=food;
}
@Override
public void run() {
// TODO Auto-generated method stub
for (int i = 0; i < 100; i++)
{
food.get();
}
}
}
class Food
{
private String name;
private String content;
private boolean flag=true;//true表示可以生产,不能拿走;false表示可以消费,不能生产
//生产产品
public synchronized void set(String name,String content)
{
if(!flag)
{
try {
this.wait(); //让当前线程进入线程池等待,没有指定时间,需要其他线程唤醒,释放对象锁,让出CPU
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
this.setName(name);
try {
Thread.sleep(300);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
this.setContent(content);
flag=false; //false表示可以消费
this.notify();//唤醒该监视器上的一个线程
}
public synchronized void get()
{
if(flag)
{
try {
this.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
try {
Thread.sleep(300);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(this.getName()+":"+this.getContent());
flag=true;
this.notify();
}
public Food() {
super();
// TODO Auto-generated constructor stub
}
public Food(String name, String content) {
super();
this.name = name;
this.content = content;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
@Override
public String toString() {
return "Food [content=" + content + ", name=" + name + "]";
}
}