资讯专栏INFORMATION COLUMN

Thread and AbstractQueuedSynchronizer

notebin / 1646人阅读

摘要:详解并发之详解中实现如下其中利用了的方法,调用的前提是已经获得线程的锁,如果对象被锁住则会等待其被释放。

Thread详解
Java并发之AQS详解

Thread中join实现如下:

</>复制代码

  1. public final synchronized void join(long millis) throws InterruptedException {
  2. long base = System.currentTimeMillis();
  3. long now = 0;
  4. if (millis < 0) {
  5. throw new IllegalArgumentException("timeout value is negative");
  6. }
  7. if (millis == 0) {
  8. while (isAlive()) {
  9. wait(0);
  10. }
  11. } else {
  12. while (isAlive()) {
  13. long delay = millis - now;
  14. if (delay <= 0) {
  15. break;
  16. }
  17. wait(delay);
  18. now = System.currentTimeMillis() - base;
  19. }
  20. }
  21. }

其中利用了Object的wait方法,调用的前提是已经获得join线程的锁,如果thread对象被锁住则会等待其被释放。

</>复制代码

  1. import core.AbstractCommonConsumer;
  2. import core.CommonUtils;
  3. import core.MagnetMonitor;
  4. import core.NamedThreadPoolExecutor;
  5. import org.apache.ibatis.io.Resources;
  6. import org.slf4j.Logger;
  7. import org.slf4j.LoggerFactory;
  8. import java.io.BufferedReader;
  9. import java.io.IOException;
  10. import java.io.InputStreamReader;
  11. import java.lang.reflect.InvocationTargetException;
  12. import java.util.Date;
  13. import java.util.LinkedList;
  14. import java.util.List;
  15. import java.util.concurrent.*;
  16. import java.util.concurrent.locks.LockSupport;
  17. class JoinTester01 implements Runnable {
  18. private String name;
  19. JoinTester01(String name) {
  20. this.name = name;
  21. }
  22. public void run() {
  23. System.out.printf("%s begins: %s
  24. ", name, new Date());
  25. try {
  26. TimeUnit.SECONDS.sleep(10);
  27. } catch (InterruptedException e) {
  28. e.printStackTrace();
  29. }
  30. System.out.printf("%s has finished: %s
  31. ", name, new Date());
  32. }
  33. }
  34. class JoinTester02 implements Runnable {
  35. Thread thread;
  36. JoinTester02(Thread thread) {
  37. this.thread = thread;
  38. }
  39. Thread getThread(){
  40. return this.thread;
  41. }
  42. public void run() {
  43. synchronized (thread) {
  44. System.out.printf("getObjectLock at %s
  45. ", new Date());
  46. try {
  47. Thread.sleep(2000);
  48. } catch (InterruptedException ex) {
  49. ex.printStackTrace();
  50. }
  51. System.out.printf("ReleaseObjectLock at %s
  52. ", new Date());
  53. }
  54. try {
  55. Thread.sleep(1000);
  56. } catch (InterruptedException ex) {
  57. ex.printStackTrace();
  58. }
  59. synchronized (thread) {
  60. System.out.printf("getObjectLock again at %s
  61. ", new Date());
  62. try {
  63. Thread.sleep(2000);
  64. } catch (InterruptedException ex) {
  65. ex.printStackTrace();
  66. }
  67. System.out.printf("ReleaseObjectLock again at %s
  68. ", new Date());
  69. }
  70. }
  71. }
  72. class TestMain {
  73. public static void main(String[] args) {
  74. Thread thread = new Thread(new JoinTester01("Leon"));
  75. JoinTester02 tester02 = new JoinTester02(thread);
  76. Thread getLockThread = new Thread(tester02);
  77. getLockThread.start();
  78. thread.start();
  79. try {
  80. System.out.printf("start join at %s
  81. ", new Date());
  82. thread.join(1000);
  83. System.out.printf("stop join at %s
  84. ", new Date());
  85. } catch (InterruptedException e) {
  86. // TODO Auto-generated catch block
  87. e.printStackTrace();
  88. }
  89. }
  90. }

</>复制代码

  1. start join at Sun Jun 30 18:40:08 CST 2019
  2. Leon begins: Sun Jun 30 18:40:08 CST 2019
  3. getObjectLock at Sun Jun 30 18:40:08 CST 2019
  4. ReleaseObjectLock at Sun Jun 30 18:40:10 CST 2019
  5. stop join at Sun Jun 30 18:40:11 CST 2019
  6. getObjectLock again at Sun Jun 30 18:40:11 CST 2019
  7. ReleaseObjectLock again at Sun Jun 30 18:40:13 CST 2019
  8. Leon has finished: Sun Jun 30 18:40:18 CST 2019

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/77902.html

相关文章

  • Thread and AbstractQueuedSynchronizer

    摘要:详解并发之详解中实现如下其中利用了的方法,调用的前提是已经获得线程的锁,如果对象被锁住则会等待其被释放。 Thread详解Java并发之AQS详解 Thread中join实现如下: public final synchronized void join(long millis) throws InterruptedException { long base = System.c...

    LeanCloud 评论0 收藏0
  • AbstractQueuedSynchronizer源代码分析(未完成)

    摘要:公平锁线程占用锁,等待,然后依次获取锁,其中会被挂起或者是自旋,然后当线程释放锁后,线程再被唤醒,以此类推,按照申请锁的先后顺序来。 Node exclusive lock(独占锁) ReentrantLock ReentrantLock实现了公平锁与非公平锁,公平锁提供顺序获取锁的方式,而非公平锁提供抢占式获取锁的方式。公平锁: 线程A占用锁,B等待,然后依次获取锁,其中B会被挂起或...

    omgdog 评论0 收藏0
  • AbstractQueuedSynchronizer源代码分析(未完成)

    摘要:公平锁线程占用锁,等待,然后依次获取锁,其中会被挂起或者是自旋,然后当线程释放锁后,线程再被唤醒,以此类推,按照申请锁的先后顺序来。 Node exclusive lock(独占锁) ReentrantLock ReentrantLock实现了公平锁与非公平锁,公平锁提供顺序获取锁的方式,而非公平锁提供抢占式获取锁的方式。公平锁: 线程A占用锁,B等待,然后依次获取锁,其中B会被挂起或...

    chanjarster 评论0 收藏0
  • AbstractQueuedSynchronizer源代码分析(未完成)

    摘要:公平锁线程占用锁,等待,然后依次获取锁,其中会被挂起或者是自旋,然后当线程释放锁后,线程再被唤醒,以此类推,按照申请锁的先后顺序来。 Node exclusive lock(独占锁) ReentrantLock ReentrantLock实现了公平锁与非公平锁,公平锁提供顺序获取锁的方式,而非公平锁提供抢占式获取锁的方式。公平锁: 线程A占用锁,B等待,然后依次获取锁,其中B会被挂起或...

    zhunjiee 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<