chrisenoch

ProducerConsumer_Liang

Apr 11th, 2021 (edited)
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.51 KB | None | 0 0
  1. import java.util.concurrent.*;
  2. import java.util.concurrent.locks.*;
  3.  
  4. public class Producer_Consumer_Liang {
  5. private static Buffer buffer = new Buffer();
  6.  
  7. public static void main(String[] args) {
  8. // Create a thread pool with two threads
  9. ExecutorService executor = Executors.newFixedThreadPool(2);
  10. executor.execute(new ProducerTask());
  11. executor.execute(new ConsumerTask());
  12. executor.shutdown();
  13. }
  14.  
  15. // A task for adding an int to the buffer
  16. private static class ProducerTask implements Runnable {
  17. public void run() {
  18. try {
  19. int i = 1;
  20. while (true) {
  21. System.out.println("Producer writes " + i);
  22. buffer.write(i++); // Add a value to the buffer
  23. // Put the thread into sleep
  24. Thread.sleep((int) (Math.random() * 1000));
  25. }
  26. } catch (InterruptedException ex) {
  27. ex.printStackTrace();
  28. }
  29. }
  30. }
  31.  
  32. // A task for reading and deleting an int from the buffer
  33. private static class ConsumerTask implements Runnable {
  34. public void run() {
  35.  
  36. try {
  37. while (true) {
  38. System.out.println("\t\t\tConsumer reads " + buffer.read());
  39. // Put the thread into sleep
  40. Thread.sleep((int) (Math.random() * 1000));
  41. }
  42. } catch (InterruptedException ex) {
  43. ex.printStackTrace();
  44. }
  45. }
  46. }
  47.  
  48. // An inner class for buffer
  49. private static class Buffer {
  50. private static final int CAPACITY = 1; // buffer size
  51. private java.util.LinkedList<Integer> queue = new java.util.LinkedList<>();
  52.  
  53. // Create a new lock
  54. private static Lock lock = new ReentrantLock();
  55.  
  56. // Create two conditions
  57. private static Condition notEmpty = lock.newCondition();
  58. private static Condition notFull = lock.newCondition();
  59.  
  60. public void write(int value) {
  61. lock.lock(); // Acquire the lock
  62. try {
  63. while (queue.size() == CAPACITY) {
  64. System.out.println("Wait for notFull condition");
  65. notFull.await();
  66. }
  67.  
  68. queue.offer(value);
  69. notEmpty.signal(); // Signal notEmpty condition
  70. } catch (InterruptedException ex) {
  71. ex.printStackTrace();
  72. } finally {
  73. lock.unlock(); // Release the lock
  74. }
  75. }
  76.  
  77. public int read() {
  78. int value = 0;
  79. lock.lock(); // Acquire the lock
  80. try {
  81. while (queue.isEmpty()) {
  82. System.out.println("\t\t\tWait for notEmpty condition");
  83. notEmpty.await();
  84. }
  85.  
  86. value = queue.remove();
  87. notFull.signal(); // Signal notFull condition
  88. } catch (InterruptedException ex) {
  89. ex.printStackTrace();
  90. } finally {
  91. lock.unlock(); // Release the lock
  92. return value;
  93. }
  94. }
  95. }
  96. }
Add Comment
Please, Sign In to add comment