简易的对象池,需要深入了解的话,得阅读<<Thinking in Pattern>>

  1. import java.util.*;
  2. import java.util.concurrent.*;
  3.  
  4. /**
  5. * 对象池
  6. * @author Administrator
  7. *
  8. * @param <T>
  9. */
  10. class Pool<T>{
  11. private int size;
  12. private List<T> items = new ArrayList<T>();
  13. private volatile boolean[] isCheckedOut;
  14. Semaphore signalControl;
  15. public Pool(int size,Class<T> classObject) {
  16. signalControl = new Semaphore(size,true);
  17. this.size = size;
  18. isCheckedOut = new boolean[size];
  19. for(int i=0;i<size;i++) {
  20. try {
  21. items.add(classObject.getDeclaredConstructor().newInstance());
  22. }catch (Exception e) {
  23. throw new RuntimeException(e);
  24. }
  25. }
  26. }
  27. public void checkIn(T x) {
  28. if(releaseItem(x)) {
  29. signalControl.release();
  30. }
  31. }
  32. public T checkOut() throws InterruptedException {
  33. signalControl.acquire();
  34. return getItem();
  35. }
  36. public synchronized T getItem() {
  37. for(int i=0;i<size;i++) {
  38. if(!isCheckedOut[i]) {
  39. isCheckedOut[i] = true;
  40. return items.get(i);
  41. }
  42. }
  43. return null;
  44. }
  45. public synchronized boolean releaseItem(T item) {
  46. int index = items.indexOf(item);
  47. if(index == -1)
  48. return false;
  49. if(isCheckedOut[index]) {
  50. isCheckedOut[index] = false;
  51. return true;
  52. }
  53. return false;
  54. }
  55. }
  56.  
  57. class Fat{
  58. private volatile double d;
  59. private static int counter = 0;
  60. private final int id = counter++;
  61. public Fat() {
  62. //构建花费昂贵
  63. for(int i=1;i<10000;i++) {
  64. d += (Math.PI + Math.E);
  65. }
  66. }
  67. public void operation() {
  68. System.out.println(this);
  69. }
  70. public String toString() {
  71. return "Fat id: " + id;
  72. }
  73. }
  74.  
  75. class CheckoutTask<T> implements Runnable{
  76. private static int counter = 0;
  77. private final int id = counter++;
  78. private Pool<T> pool;
  79. public CheckoutTask(Pool<T> pool) {
  80. this.pool = pool;
  81. }
  82. public void run() {
  83. try {
  84. T item = pool.checkOut();
  85. System.out.println("Check out " + item);
  86. TimeUnit.SECONDS.sleep(1);
  87. System.out.println("Check in " + item);
  88. pool.checkIn(item);
  89. }catch (InterruptedException e) {
  90. }
  91. }
  92. public String toString() {
  93. return "CheckTase " + id + " ";
  94. }
  95. }
  96.  
  97. public class Restaurant{
  98. final static int SIZE = 25;
  99. public static void main(String[] args) throws Exception {
  100. //创建一个对象池
  101. final Pool<Fat> fatPool = new Pool<Fat>(SIZE, Fat.class);
  102. ExecutorService executorService = Executors.newCachedThreadPool();
  103. for(int i=0;i<SIZE;i++) {
  104. executorService.execute(new CheckoutTask<Fat>(fatPool));
  105. }
  106. System.out.println("All CheckoutTask created");
  107. List<Fat> list = new ArrayList<>();
  108. for(int i=0;i<SIZE;i++) {
  109. Fat fat = fatPool.checkOut();
  110. System.out.println(i + ": main() thread check out");
  111. fat.operation();
  112. list.add(fat);
  113. }
  114. Future<?> blocked = executorService.submit(new Runnable() {
  115. @Override
  116. public void run() {
  117. // TODO Auto-generated method stub
  118. try {
  119. System.out.println("Is leave out?");
  120. fatPool.checkOut();
  121. }catch (Exception e) {
  122. System.out.println("NO");
  123. }
  124. }
  125. });
  126. TimeUnit.SECONDS.sleep(2);
  127. blocked.cancel(true);
  128. for(Fat fat:list) {
  129. fatPool.checkIn(fat);
  130. }
  131. for(Fat fat:list) {
  132. fatPool.checkIn(fat);
  133. }
  134. executorService.shutdown();
  135. }
  136. }

  

版权声明:本文为--zz原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/--zz/p/9671119.html