标签:
前几天老师领着学习了一下单线程和多线程的题目。
这里是操作系统中非常经典的题目,生产者和消费者的题,这里涉及的是仓库,
只有一个人(生产者或消费者)进入,另一个人只有等待。
这里的重点是关于传值的问题。一定要保持一致,不然,对于存和取 的对象,就可能出现多个。
//========================================================================//
仓库类
//========================================================================//
package House;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
public class WareHouse{
ArrayList<Integer> products = null;
private final int MAXN = 20;
public WareHouse(){
products = new ArrayList<>();
}
public void add(int number){
try {
synchronized (products) {
if(products.size() < MAXN){
System.out.printf("生产者向仓库中存入了一个编号为%d商品\n", number);
products.add(number);
System.out.printf("仓库有中共有: "+ products.size() + "个\n");
products.notify();
}else if(products.size() == MAXN){
System.out.println("仓库已满,请耐心等待消费者来取走商品!!!");
products.wait();
}
}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void get(int number){
try {
synchronized (products) {
if(products.size() == 0){
System.out.println("仓库已空,请耐心等待商品的添加!!");
products.wait();
}else if(products.size() > 0){
System.out.printf("消费者取出了编号为%d的商品\n", products.get(0));
products.remove(0);
System.out.println("仓库有中共有: "+ products.size() + "个\n");
products.notify();
}
}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
//===============================================================//
消费者类:
//==========================================================================//
package House;
import java.util.Random;
public class Customer implements Runnable {
Random ran = new Random();
public WareHouse wa;
public int name;
public Customer(WareHouse wa, int name) {
// TODO Auto-generated constructor stub
this.wa = wa;
this.name = name;
}
@Override
public void run() {
// TODO Auto-generated method stub
for(int i = 0; i < 20; i++){
int getnumber = ran.nextInt(600);
wa.get(getnumber);
}
}
}
//=====================================================================================//
生产者类
//=====================================================================================//
package House;
import java.util.Random;
public class Producer implements Runnable {
Random ran = new Random();
public WareHouse wa;
public int name;
public Producer(WareHouse wa, int name) {
// TODO Auto-generated constructor stub
this.wa = wa;
this.name = name;
}
@Override
public void run() {
// TODO Auto-generated method stub
for(int i = 0; i < 20; i++){
int addnumber = ran.nextInt(500);
wa.add(addnumber);
}
}
}
//======================================================================================//
测试类
//========================================================================================//
package House;
public class Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("消费者和生产者的大战开始了----->>>>>>");
WareHouse wa = new WareHouse();
Producer p = new Producer(wa, 1);
Customer C = new Customer(wa, 1);
Thread P1 = new Thread(p);
P1.start();
Thread C1 = new Thread(C);
C1.start();
System.out.println("消费者和生产者的资源抢占结束----->>>>>>");
}
}
标签:
原文地址:http://blog.csdn.net/u012965373/article/details/45059679