Advertisement
Guest User

Untitled

a guest
Mar 26th, 2019
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.35 KB | None | 0 0
  1. package me.lihongjie;
  2.  
  3. import org.junit.Before;
  4. import org.junit.Test;
  5. import org.slf4j.Logger;
  6. import org.slf4j.LoggerFactory;
  7.  
  8. import java.util.concurrent.ExecutorService;
  9. import java.util.concurrent.Executors;
  10. import java.util.concurrent.TimeUnit;
  11. import java.util.concurrent.locks.Condition;
  12. import java.util.concurrent.locks.ReentrantLock;
  13.  
  14. public class LockTest {
  15.  
  16. // private static Logger logger = LoggerFactory.getLogger(LockTest.class);
  17.  
  18. private boolean aNotDone = true;
  19. private Condition aDoneCnd;
  20. private ReentrantLock lock;
  21. private boolean firstRun = true;
  22. private boolean cNotDone = true;
  23. private Condition cDoneCnd;
  24. private Condition bDoneCnd;
  25. private boolean bNotDone = true;
  26.  
  27. @Before
  28. public void setUp() throws Exception {
  29. lock = new ReentrantLock();
  30.  
  31. aDoneCnd = lock.newCondition();
  32. bDoneCnd = lock.newCondition();
  33. cDoneCnd = lock.newCondition();
  34. }
  35.  
  36. @Test
  37. public void test() throws Exception {
  38.  
  39.  
  40. ExecutorService threadPool = Executors.newFixedThreadPool(3);
  41.  
  42. threadPool.submit(() -> {
  43.  
  44.  
  45. for (int i = 0; i < 10; i++) {
  46.  
  47.  
  48. lock.lock();
  49. // logger.info("a get lock");
  50. if (firstRun) {
  51. System.out.println("A");
  52.  
  53. firstRun = false;
  54.  
  55. }else{
  56.  
  57.  
  58. while (cNotDone) {
  59.  
  60. try {
  61. cDoneCnd.await();
  62. } catch (InterruptedException e) {
  63. Thread.currentThread()
  64. .interrupt();
  65. }
  66.  
  67.  
  68. }
  69.  
  70. System.out.println("A");
  71.  
  72.  
  73. }
  74. aNotDone = false;
  75. cNotDone = true;
  76. aDoneCnd.signal();
  77. lock.unlock();
  78. // logger.info("a release lock");
  79. }
  80. });
  81.  
  82.  
  83. threadPool.submit(() -> {
  84.  
  85.  
  86. for (int i = 0; i < 10; i++) {
  87.  
  88. lock.lock();
  89. // logger.info("b get lock");
  90. while (aNotDone) {
  91.  
  92.  
  93. try {
  94. aDoneCnd.await();
  95. } catch (InterruptedException e) {
  96. Thread.currentThread()
  97. .interrupt();
  98. }
  99. }
  100.  
  101. System.out.println("B");
  102. aNotDone = true;
  103. bNotDone = false;
  104. bDoneCnd.signal();
  105. lock.unlock();
  106. // logger.info("b release lock");
  107. }
  108. });
  109.  
  110.  
  111. threadPool.submit(() -> {
  112.  
  113.  
  114. for (int i = 0; i < 10; i++) {
  115.  
  116. lock.lock();
  117. // logger.info("c get lock");
  118.  
  119. while (bNotDone) {
  120.  
  121.  
  122. try {
  123. bDoneCnd.await();
  124. } catch (InterruptedException e) {
  125. Thread.currentThread()
  126. .interrupt();
  127. }
  128. }
  129.  
  130. System.out.println("C");
  131. bNotDone = true;
  132. cNotDone = false;
  133. cDoneCnd.signal();
  134. lock.unlock();
  135. // logger.info("c release lock");
  136. }
  137. });
  138.  
  139.  
  140. threadPool.awaitTermination(1, TimeUnit.HOURS);
  141.  
  142. }
  143.  
  144. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement