* 產品
*/
public class Product {
String name;
String color;
boolean flag = false;//沒有產品
/**
* 進行消費
*/
public synchronized void get() {
//沒產品,等待生產
if(flag==false) {
try {
wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//有產品進行消費
System.out.println("消費了:"+color+"的"+name);
//消費完,沒有產品
flag = false;
//通知生產
notify();
}
/**
* 進行生產
* @param name 產品名稱
* @param color 產品顏色
*/
public synchronized void put(String name,String color) {
//有產品,等待消費
if(flag==true) {
try {
wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//把傳進來的屬性進行賦值操作
this.name = name;
this.color = color;
//沒產品進行生產
System.out.println("生產了:"+color+"的"+name);
//有產品了
flag = true;
//通知消費
notify();
}
}
/**
*消費者負責消費產品
*/
public class Consumer implements Runnable {
Product p;
public Consumer() {
}
public Consumer(Product p) {
this.p = p;
}
@Override
public void run() {
while (true) {
p.get();
}
}
}
/**
* 生產者負責生產產品
*/
public class Producer implements Runnable {
Product p;
public Producer() {
}
public Producer(Product p) {
this.p = p;
}
@Override
public void run() {
int i = 0;
while (true) {
if (i % 2 == 0) {
p.put("饅頭", "白色");
} else {
p.put("玉米餅", "黃色");
}
i++;
}
}
}
/**
* 測試類
*/
public class Test {
public static void main(String[] args) {
Product p = new Product();
new Thread(new Producer(p)).start();
new Thread(new Consumer(p)).start();
}
}
//第二個例子
import java.util.List;
import java.util.Vector;
/**
* 資源類(包子)
*/
public class Bun {
List<Integer> bz = new Vector<Integer>();
/**
* 買包子的方法
* @throws InterruptedException
*/
public synchronized void buy() throws InterruptedException {
Thread.sleep(100);
//包子賣空了,等待做包子再買
if(bz.size()==0) {
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("消費者買到第"+bz.size()+"個包子");
//買到1個就少1個
bz.remove(bz.size()-1);
//通知生產者做包子
notify();
}
/**
* 製作包子的方法
* @throws InterruptedException
*/
public synchronized void make() throws InterruptedException {
Thread.sleep(100);
//包子做到10個了,先不做,開始賣
if(bz.size()>=10) {
try {
wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out.println("生產者在做第"+(bz.size()+1)+"個包子");
//製作一個就多一個
bz.add(666);
//通知消費者買包子
notify();
}
}
**
* 消費者負責消費產品
*/
public class Consumer2 implements Runnable {
Bun b;
public Consumer2() {
}
public Consumer2(Bun b) {
this.b = b;
}
@Override
public void run() {
while (true) {
try {
b.buy();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
/**
* 生產者負責生產產品
*/
public class Producer2 implements Runnable {
Bun b;
public Producer2() {
}
public Producer2(Bun b) {
this.b = b;
}
@Override
public void run() {
while (true) {
try {
b.make();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
/**
*測試類
*/
public class Test2 {
public static void main(String[] args) {
Bun b = new Bun();
new Thread(new Producer2(b)).start();
new Thread(new Consumer2(b)).start();
}
}