生产者与消费者:
- /**
- * 生产者每次生产10件商品,消费者每次消费1件商品
- * 没有商品时生产者生产商品,有商品时消费者消费商品
- */
- class Producer extends Thread
- {
- private Cangku ck;
- public Producer(Cangku ck)
- {
- this.ck = ck;
- }
- public void run()
- {
- ck.createProduct();
- }
- }
- class Consumer extends Thread
- {
- private Cangku ck;
- public Consumer(Cangku ck)
- {
- this.ck = ck;
- }
- public void run()
- {
- ck.useProduct();// 消费商品
- }
- }
- class Cangku//商品对象会变化,创建一个容器对象,锁住不变
- {
- public synchronized void useProduct()//通过方法进行封装
- {
- for (int i = 0; i < 50; i++)
- {
- if (productSize() > 0)
- {
- for (int j = 0; j < 10; j++)
- {
- Product p = removeProduct(0);
- System.out.println("消费了" + j + "个产品");
- }
- }
- notify();
- try
- {
- if (i != 49)
- {
- wait();
- }
- }
- catch (InterruptedException e)
- {
- e.printStackTrace();
- }
- }
- }
- public synchronized void createProduct()
- {
- for (int i = 0; i < 50; i++)
- {
- if (productSize() == 0)
- {
- for (int j = 0; j < 10; j++)
- {
- addProduct(new Product());
- }
- System.out.println("生产了十个产品");
- notify();
- try
- {
- if (i != 49)
- {
- wait();
- }
- }
- catch (InterruptedException e)
- {
- e.printStackTrace();
- }
- }
- }
- }
- private LinkedList<Product> ppp = new LinkedList<Product>();//创建一个商品类型的容器
- public int productSize()
- {
- return ppp.size();
- }
- public void addProduct(Product p)
- {
- if (null != p)
- {
- ppp.add(p);
- }
- }
- public Product removeProduct(int index)
- {
- return ppp.remove(index);
- }
- }
- class Product
- {
- String name = "玩具";
- }
- public class Test
- {
- public static void main(String[] args)
- {
- Cangku ck = new Cangku();
- Producer p = new Producer(ck);
- Consumer c = new Consumer(ck);
- p.start();
- c.start();
- }
- }
重构经过:
1.
- /**
- * 加油一次20升,每次加完开车10公里
- * 开车耗完油,没油时加油,有油时开车
- * 双线程循环五十次
- */
- public class TwoThread
- {
- public static void main(String[] args)
- {
- CarOilBox cob = new CarOilBox();
- Car c = new Car(cob);
- Oil o = new Oil(cob);
- c.start();
- o.start();
- }
- }
- class Car extends Thread
- {
- private CarOilBox cob;
- public Car(CarOilBox cob)
- {
- this.cob = cob;
- }
- @Override
- public void run()
- {
- synchronized (cob)
- {
- for (int i = 0; i < 50; i++)
- {
- if (cob.co != null)
- {
- for (int j = 0; j < 10; j++)
- {
- cob.co = null;
- System.out.println("第" + i + "次开车" + j + "公里");
- }
- }
- cob.notify();
- if (i < 49)
- {
- try
- {
- cob.wait();
- }
- catch (InterruptedException e)
- {
- e.printStackTrace();
- }
- }
- }
- }
- }
- }
- class Oil extends Thread
- {
- private CarOilBox cob;
- public Oil(CarOilBox cob)
- {
- this.cob = cob;
- }
- @Override
- public void run()
- {
- synchronized (cob)
- {
- for (int i = 0; i < 50; i++)
- {
- if (cob.co == null)
- {
- for (int j = 0; j < 20; j++)
- {
- cob.co = new CarOil();
- System.out.println("第" + i + "次加油" + j + "升");
- }
- }
- cob.notify();
- if (i < 49)
- {
- try
- {
- cob.wait();
- }
- catch (InterruptedException e)
- {
- e.printStackTrace();
- }
- }
- }
- }
- }
- }
- class CarOil
- {
- }
- class CarOilBox
- {
- CarOil co;
- }
2.
- public class CopyTwoThread
- {
- public static void main(String[] args)
- {
- CarOilBox cob = new CarOilBox();
- Car c = new Car(cob);
- Oil o = new Oil(cob);
- c.start();
- o.start();
- }
- }
- class Car extends Thread
- {
- private CarOilBox cob;
- public Car(CarOilBox cob)
- {
- this.cob = cob;
- }
- @Override
- public void run()
- {
- synchronized (cob)
- {
- for (int i = 0; i < 50; i++)
- {
- if (cob.aco.size() != 0)
- {
- for (int j = 0; j < 10; j++)
- {
- CarOil co1 = cob.aco.remove(0);
- CarOil co2 = cob.aco.remove(0);
- System.out.println("第" + i + "次开车" + j + "公里");
- }
- }
- cob.notify();
- if (i < 49)
- {
- try
- {
- cob.wait();
- }
- catch (InterruptedException e)
- {
- e.printStackTrace();
- }
- }
- }
- }
- }
- }
- class Oil extends Thread
- {
- private CarOilBox cob;
- public Oil(CarOilBox cob)
- {
- this.cob = cob;
- }
- @Override
- public void run()
- {
- synchronized (cob)
- {
- for (int i = 0; i < 50; i++)
- {
- if (cob.aco.size() == 0)
- {
- for (int j = 0; j < 20; j++)
- {
- cob.aco.add(new CarOil());
- System.out.println("第" + i + "次加油" + j + "升");
- }
- }
- cob.notify();
- if (i < 49)
- {
- try
- {
- cob.wait();
- }
- catch (InterruptedException e)
- {
- e.printStackTrace();
- }
- }
- }
- }
- }
- }
- class CarOil
- {
- }
- class CarOilBox
- {
- CarOil co;
- ArrayList<CarOil> aco =new ArrayList<CarOil>();
- }
3.
- public class CopyTwoThread
- {
- public static void main(String[] args)
- {
- CarOilBox cob = new CarOilBox();
- Car c = new Car(cob);
- Oil o = new Oil(cob);
- c.start();
- o.start();
- }
- }
- class Car extends Thread
- {
- private CarOilBox cob;
- public Car(CarOilBox cob)
- {
- this.cob = cob;
- }
- @Override
- public void run()
- {
- synchronized (cob)
- {
- for (int i = 0; i < 50; i++)
- {
- if (cob.arraySize() != 0)
- {
- for (int j = 0; j < 10; j++)
- {
- CarOil co1 = cob.arrayRemove();
- CarOil co2 = cob.arrayRemove();
- System.out.println("第" + i + "次开车" + j + "公里");
- }
- }
- cob.notify();
- if (i < 49)
- {
- try
- {
- cob.wait();
- }
- catch (InterruptedException e)
- {
- e.printStackTrace();
- }
- }
- }
- }
- }
- }
- class Oil extends Thread
- {
- private CarOilBox cob;
- public Oil(CarOilBox cob)
- {
- this.cob = cob;
- }
- @Override
- public void run()
- {
- synchronized (cob)
- {
- for (int i = 0; i < 50; i++)
- {
- if (cob.arraySize() == 0)
- {
- for (int j = 0; j < 20; j++)
- {
- cob.arrayAdd();
- System.out.println("第" + i + "次加油" + j + "升");
- }
- }
- cob.notify();
- if (i < 49)
- {
- try
- {
- cob.wait();
- }
- catch (InterruptedException e)
- {
- e.printStackTrace();
- }
- }
- }
- }
- }
- }
- class CarOil
- {
- }
- class CarOilBox
- {
- //CarOil co;
- private ArrayList<CarOil> aco =new ArrayList<CarOil>();
- public int arraySize()
- {
- return aco.size();
- }
- public CarOil arrayRemove()
- {
- return aco.remove(0);
- }
- public void arrayAdd()
- {
- aco.add(new CarOil());
- }
- }
4.
- public class CopyTwoThread
- {
- public static void main(String[] args)
- {
- CarOilBox cob = new CarOilBox();
- Car c = new Car(cob);
- Oil o = new Oil(cob);
- c.start();
- o.start();
- }
- }
- class Car extends Thread
- {
- private CarOilBox cob;
- public Car(CarOilBox cob)
- {
- this.cob = cob;
- }
- @Override
- public void run()
- {
- cob.driveCar(cob);
- }
- }
- class Oil extends Thread
- {
- private CarOilBox cob;
- public Oil(CarOilBox cob)
- {
- this.cob = cob;
- }
- @Override
- public void run()
- {
- cob.addOil(cob);
- }
- }
- class CarOil
- {
- }
- class CarOilBox
- {
- //CarOil co;
- private ArrayList<CarOil> aco =new ArrayList<CarOil>();
- public int arraySize()
- {
- return aco.size();
- }
- public CarOil arrayRemove()
- {
- return aco.remove(0);
- }
- public void arrayAdd()
- {
- aco.add(new CarOil());
- }
- public synchronized void driveCar(CarOilBox cob)
- {
- for (int i = 0; i < 50; i++)
- {
- if (arraySize() != 0)
- {
- for (int j = 0; j < 10; j++)
- {
- CarOil co1 = arrayRemove();
- CarOil co2 = arrayRemove();
- System.out.println("第" + i + "次开车" + j + "公里");
- }
- }
- notify();
- if (i < 49)
- {
- try
- {
- wait();
- }
- catch (InterruptedException e)
- {
- e.printStackTrace();
- }
- }
- }
- }
- public synchronized void addOil(CarOilBox cob)
- {
- for (int i = 0; i < 50; i++)
- {
- if (arraySize() == 0)
- {
- for (int j = 0; j < 20; j++)
- {
- arrayAdd();
- System.out.println("第" + i + "次加油" + j + "升");
- }
- }
- notify();
- if (i < 49)
- {
- try
- {
- wait();
- }
- catch (InterruptedException e)
- {
- e.printStackTrace();
- }
- }
- }
- }
- }
本文转自 glblong 51CTO博客,原文链接:http://blog.51cto.com/glblong/1195004,如需转载请自行联系原作者