当前位置:   article > 正文

Java多线程——多线程之间的顺序执行和交替执行的多种方法(线程间通信)wait¬ify ReentrantLock LockSupport_java挨个执行多个while true

java挨个执行多个while true
  1. package com.leolee.multithreadProgramming.concurrent.threadCommunication;
  2. import lombok.extern.slf4j.Slf4j;
  3. import java.util.concurrent.locks.Condition;
  4. import java.util.concurrent.locks.LockSupport;
  5. import java.util.concurrent.locks.ReentrantLock;
  6. /**
  7. * @ClassName Test
  8. * @Description: 线程间通讯——线程的执行顺序
  9. * @Author LeoLee
  10. * @Date 2020/12/14
  11. * @Version V1.0
  12. **/
  13. @Slf4j
  14. public class Test {
  15. //=============================固定顺序执行 wait notify 方法====================================
  16. /*
  17. * 功能描述: <br>
  18. * 〈线程的顺序执行——wait notify方法〉
  19. * 该实例使t2一定先执行
  20. * @Param: []
  21. * @Return: void
  22. * @Author: LeoLee
  23. * @Date: 2020/12/14 21:18
  24. */
  25. static final Object object = new Object();
  26. static boolean t2Runned = false;//表示t2是否运行过
  27. public void sequenceByWaitNotify() {
  28. Thread t1 = new Thread(() -> {
  29. synchronized (object) {
  30. while (!t2Runned) {
  31. try {
  32. object.wait();
  33. } catch (InterruptedException e) {
  34. e.printStackTrace();
  35. }
  36. }
  37. log.info("{} 执行", Thread.currentThread().getName());
  38. }
  39. }, "t1");
  40. Thread t2 = new Thread(() -> {
  41. synchronized (object) {
  42. log.info("{} 执行", Thread.currentThread().getName());
  43. t2Runned = true;
  44. object.notifyAll();
  45. }
  46. }, "t2");
  47. t1.start();
  48. t2.start();
  49. }
  50. //=============================固定顺序执行 ReentrantLock法====================================
  51. /*
  52. * 功能描述: <br>
  53. * 〈固定顺序执行 ReentrantLock法〉
  54. * 该实例使t2一定先执行
  55. * @Param: []
  56. * @Return: void
  57. * @Author: LeoLee
  58. * @Date: 2020/12/14 22:28
  59. */
  60. private static final ReentrantLock reentrantLock = new ReentrantLock();
  61. public void sequenceByReentrantLock() {
  62. Condition condition1 = reentrantLock.newCondition();
  63. Thread t1 = new Thread(() -> {
  64. reentrantLock.lock();
  65. try {
  66. while (!t2Runned) {
  67. try {
  68. condition1.await();
  69. } catch (InterruptedException e) {
  70. e.printStackTrace();
  71. }
  72. }
  73. } finally {
  74. reentrantLock.unlock();
  75. }
  76. log.info("{} 执行", Thread.currentThread().getName());
  77. }, "t1");
  78. Thread t2 = new Thread(() -> {
  79. reentrantLock.lock();
  80. try {
  81. log.info("{} 执行", Thread.currentThread().getName());
  82. t2Runned = true;
  83. condition1.signalAll();
  84. } finally {
  85. reentrantLock.unlock();
  86. }
  87. }, "t2");
  88. t1.start();
  89. t2.start();
  90. }
  91. //=============================固定顺序执行 park unpark 法====================================
  92. /*
  93. * 功能描述: <br>
  94. * 〈固定顺序执行 park unpark 法〉
  95. * 该实例使t2一定先执行
  96. * @Param: []
  97. * @Return: void
  98. * @Author: LeoLee
  99. * @Date: 2020/12/15 0:17
  100. */
  101. public void sequenceByPark() {
  102. Thread t1 = new Thread(() -> {
  103. LockSupport.park();
  104. log.info("{} 执行", Thread.currentThread().getName());
  105. }, "t1");
  106. Thread t2 = new Thread(() -> {
  107. log.info("{} 执行", Thread.currentThread().getName());
  108. LockSupport.unpark(t1);
  109. }, "t2");
  110. t1.start();
  111. t2.start();
  112. }
  113. //=============================交替执行wait & notify法====================================
  114. /* 等待标识 下一个标识
  115. a 1 2
  116. b 2 3
  117. c 3 1
  118. */
  119. private int flag;//等待标记
  120. //循环次数
  121. private int loopNum;
  122. private void print(String str, int waitFlag, int nextFlag) {
  123. for (int i = 0; i < loopNum; i++) {
  124. synchronized (this) {
  125. while (flag != waitFlag) {
  126. try {
  127. this.wait();
  128. } catch (InterruptedException e) {
  129. e.printStackTrace();
  130. }
  131. }
  132. log.info("{}", str);
  133. flag = nextFlag;
  134. this.notifyAll();
  135. }
  136. }
  137. }
  138. /*
  139. * 功能描述: <br>
  140. * 〈〉
  141. * @Param: [flag 开始的标记, loopNum循环次数]
  142. * @Return: void
  143. * @Author: LeoLee
  144. * @Date: 2020/12/15 20:47
  145. */
  146. public void alternateByWaitNotify(int flag, int loopNum) {
  147. this.flag = flag;
  148. this.loopNum = loopNum;
  149. new Thread(() -> {
  150. this.print("a", 1, 2);
  151. }).start();
  152. new Thread(() -> {
  153. this.print("b", 2, 3);
  154. }).start();
  155. new Thread(() -> {
  156. this.print("c", 3, 1);
  157. }).start();
  158. }
  159. //=============================交替执行ReentrantLock - await & signal法====================================
  160. /*
  161. * 功能描述: <br>
  162. * 〈交替执行ReentrantLock - await & signal法〉
  163. * @Param: []
  164. * @Return: void
  165. * @Author: LeoLee
  166. * @Date: 2020/12/15 17:44
  167. */
  168. public void alternateByAwaitSignal() {
  169. this.loopNum = 4;
  170. Condition condition1 = reentrantLock.newCondition();
  171. Condition condition2 = reentrantLock.newCondition();
  172. Condition condition3 = reentrantLock.newCondition();
  173. new Thread(() -> {
  174. this.print("a", condition1, condition2);
  175. }).start();
  176. new Thread(() -> {
  177. this.print("b", condition2, condition3);
  178. }).start();
  179. new Thread(() -> {
  180. this.print("c", condition3, condition1);
  181. }).start();
  182. try {
  183. Thread.sleep(1000);
  184. } catch (InterruptedException e) {
  185. e.printStackTrace();
  186. }
  187. //开始唤起第一个线程
  188. reentrantLock.lock();
  189. try {
  190. condition1.signal();
  191. } finally {
  192. reentrantLock.unlock();
  193. }
  194. }
  195. public void print(String str, Condition currentCondition, Condition nextCondition) {
  196. for (int i = 0; i < loopNum; i++) {
  197. reentrantLock.lock();
  198. try {
  199. currentCondition.await();
  200. log.info(str);
  201. nextCondition.signal();
  202. } catch (InterruptedException e) {
  203. e.printStackTrace();
  204. } finally {
  205. reentrantLock.unlock();
  206. }
  207. }
  208. }
  209. //=============================交替执行LockSupport - park & unpark法====================================
  210. private Thread t1;
  211. private Thread t2;
  212. private Thread t3;
  213. public void alternateByLockSupport() {
  214. this.loopNum = 5;
  215. t1 = new Thread(() -> {
  216. this.print("a", t2);
  217. });
  218. t2 = new Thread(() -> {
  219. this.print("b", t3);
  220. });
  221. t3 = new Thread(() -> {
  222. this.print("c", t1);
  223. });
  224. t1.start();
  225. t2.start();
  226. t3.start();
  227. LockSupport.unpark(t1);
  228. }
  229. public void print(String str, Thread nextThread) {
  230. for (int i = 0; i < loopNum; i++) {
  231. LockSupport.park();
  232. log.info(str);
  233. LockSupport.unpark(nextThread);
  234. }
  235. }
  236. public static void main(String[] args) {
  237. Test test = new Test();
  238. // test.sequenceByWaitNotify();
  239. // test.sequenceByReentrantLock();
  240. // test.sequenceByPark();
  241. // test.alternateByWaitNotify(1, 10);
  242. // test.alternateByAwaitSignal();
  243. test.alternateByLockSupport();
  244. }
  245. }

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Guff_9hys/article/detail/920492
推荐阅读
相关标签
  

闽ICP备14008679号