等待后解锁是如何发生的

axr492tv  于 2021-06-30  发布在  Java
关注(0)|答案(1)|浏览(311)

我写了一个小程序来交替打印奇偶数,但有一个问题:
因为线程应该在 await 那么重入锁是如何解锁的呢?

  1. public class Worker implements Runnable
  2. {
  3. private ReentrantLock rLock = null;
  4. private Condition condition = null;
  5. private String name;
  6. volatile static boolean isEvenTurn = true;
  7. public Worker(String name, ReentrantLock rLock, Condition condition)
  8. {
  9. this.name = name;
  10. this.rLock = rLock;
  11. this.condition = condition;
  12. }
  13. @Override
  14. public void run()
  15. {
  16. try
  17. {
  18. if(name.equals("ODD"))
  19. printOdd();
  20. else
  21. printEven();
  22. }
  23. catch(Exception e) { e.printStackTrace();}
  24. }
  25. private void printOdd() throws Exception
  26. {
  27. while(isEvenTurn);
  28. for(int i=1;i<10;i+=2)
  29. {
  30. try
  31. {
  32. rLock.lock();
  33. System.out.println(i);
  34. }
  35. catch(Exception e) {e.printStackTrace();}
  36. finally
  37. {
  38. condition.signal();
  39. condition.await();
  40. rLock.unlock();
  41. }
  42. }
  43. }
  44. private void printEven() throws Exception
  45. {
  46. for(int i=0;i<10;i+=2)
  47. {
  48. try
  49. {
  50. rLock.lock();
  51. System.out.println(i);
  52. isEvenTurn = false;
  53. }
  54. catch(Exception e) {e.printStackTrace();}
  55. finally
  56. {
  57. condition.signal();
  58. condition.await();
  59. rLock.unlock();
  60. }
  61. }
  62. }
  63. public static void main(String[] args)
  64. {
  65. ReentrantLock rLock = new ReentrantLock();
  66. ExecutorService service = Executors.newFixedThreadPool(2);
  67. Condition c = rLock.newCondition();
  68. Worker oddPrinter = new Worker("ODD",rLock,c);
  69. Worker evenPrinter = new Worker("EVEN",rLock,c);
  70. service.execute(evenPrinter);
  71. service.execute(oddPrinter);
  72. service.shutdown();
  73. }
  74. }
bwleehnv

bwleehnv1#

在printeven()方法中添加以下行:在finally块中:

  1. finally
  2. {
  3. condition.signal();
  4. if(i < 10)condition.await();
  5. rLock.unlock();
  6. }

通过添加这个条件,当i=10时,线程将不再等待。

相关问题