歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Java多線程應用——生產者消費者

Java多線程應用——生產者消費者

日期:2017/3/1 10:38:46   编辑:Linux編程

Java多線程應用——生產者消費者:

  1. import java.util.Random;
  2. /*
  3. * @topic:用互斥實現生產者消費者問題
  4. */
  5. public class Custom{
  6. public static void main(String[] args) {
  7. FruitBasket fb = new FruitBasket();
  8. new Thread(new Farmer("農夫1",fb)).start();
  9. new Thread(new Farmer("農夫2",fb)).start();
  10. new Thread(new Farmer("農夫3",fb)).start();
  11. new Thread(new Child("小孩1",fb)).start();
  12. new Thread(new Child("小孩2",fb)).start();
  13. new Thread(new Child("小孩3",fb)).start();
  14. }
  15. }
  16. class Fruit{
  17. private int id;
  18. private static int number = 0;
  19. private String variety;
  20. private String[] varietys = "蘋果,桃子,梨子,香蕉,西瓜,荔枝,葡萄".split(",");
  21. public Fruit(){
  22. this.variety = varietys[new Random().nextInt(7)];
  23. this.id = ++number;
  24. }
  25. public int getId(){
  26. return this.id;
  27. }
  28. public String getVariety(){
  29. return this.variety;
  30. }
  31. }
  32. class FruitBasket{
  33. private Fruit[] fruits = new Fruit[10];//容量為10的水果數組
  34. private int index = 0;//下一個將要放入水果的位置
  35. public boolean isEmpty(){//判斷水果籃是否為空
  36. return index == 0 ?true:false;
  37. }
  38. public boolean isFull(){//是否為滿
  39. return index == fruits.length?true:false;
  40. }
  41. public synchronized void push(String name , Fruit fruit){//對push方法實行同步
  42. while(isFull()){
  43. try{
  44. this.wait();//��為滿,則不能push,只能等待。
  45. }catch(InterruptedException e){
  46. e.printStackTrace();
  47. }
  48. }
  49. fruits[index++] = fruit;
  50. System.out.println(name + "向水果框中放入編號為" + fruit.getId() + "的"+fruit.getVariety());
  51. display();
  52. this.notify(); //通知另一線程
  53. }
  54. public synchronized Fruit pop(String name){
  55. while(isEmpty()){
  56. try{
  57. this.wait();
  58. }catch(Exception e){
  59. e.printStackTrace();
  60. }
  61. }
  62. Fruit fruit = fruits[--index];
  63. System.out.println(name + "從水果框中拿出編號為"+fruit.getId() +"的" + fruit.getVariety());
  64. display();
  65. this.notify();
  66. return fruit;
  67. }
  68. public void display(){
  69. for(int i = 0 ; i < index ; i++){
  70. System.out.printf(fruits[i].getId()+fruits[i].getVariety() + " |");
  71. }
  72. for (int i = index; i < fruits.length; i++) {
  73. System.out.printf( "[" + (i + 1) + "]|");
  74. }
  75. System.out.println("\n");
  76. }
  77. }
  78. class Farmer implements Runnable{
  79. private String name ;
  80. private FruitBasket fruitBasket;
  81. public void run(){//不斷往籃子中放
  82. while(true){
  83. fruitBasket.push(name,new Fruit());
  84. try{
  85. Thread.sleep(new Random().nextInt(2000));
  86. }catch(Exception e){
  87. e.printStackTrace();
  88. }
  89. }
  90. }
  91. Farmer(String name, FruitBasket fruitBasket) {
  92. this.name = name;
  93. this.fruitBasket = fruitBasket;
  94. }
  95. }
  96. class Child implements Runnable{
  97. private String name;
  98. private FruitBasket fruitBasket;
  99. public void run(){//不斷往籃子中取
  100. while(true){
  101. fruitBasket.pop(this.name);
  102. try{
  103. Thread.sleep(new Random().nextInt(5000));
  104. }catch(Exception e){
  105. e.printStackTrace();
  106. }
  107. }
  108. }
  109. Child(String name, FruitBasket fruitBasket) {
  110. this.name = name;
  111. this.fruitBasket = fruitBasket;
  112. }
  113. }
Copyright © Linux教程網 All Rights Reserved