Guest User

Untitled

a guest
Jan 18th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.26 KB | None | 0 0
  1. public class ThreadTest2 {
  2. Lock lock = new ReentrantLock();
  3. Condition condition = lock.newCondition();
  4.  
  5. public void conditionWait() throws InterruptedException {
  6. lock.lock();
  7. try {
  8. System.out.println("1");
  9. condition.await();
  10. System.out.println("2");
  11. } catch (InterruptedException e) {
  12. // TODO Auto-generated catch block
  13. e.printStackTrace();
  14. } finally {
  15. lock.unlock();
  16. }
  17. }
  18.  
  19. public void conditionSignal() throws InterruptedException {
  20. lock.lock();
  21. try {
  22. System.out.println("3");
  23. condition.signal();
  24. System.out.println("4");
  25. } finally {
  26. lock.unlock();
  27. }
  28. }
  29.  
  30. public static void main(String[] args) throws InterruptedException {
  31. ThreadTest2 test = new ThreadTest2();
  32.  
  33. test.conditionWait();
  34.  
  35. Thread.sleep(2000);
  36.  
  37. test.conditionSignal();
  38. }
  39. }
  40.  
  41. public class ThreadTest2 {
  42. Lock lock = new ReentrantLock();
  43. Condition condition = lock.newCondition();
  44.  
  45. public void conditionWait() throws InterruptedException {
  46. new Thread() {
  47. public void run() {
  48. lock.lock();
  49. try {
  50. System.out.println("1");
  51. condition.await();
  52. System.out.println("2");
  53. } catch (InterruptedException e) {
  54. // TODO Auto-generated catch block
  55. e.printStackTrace();
  56. }finally {
  57. lock.unlock();
  58. }
  59. };
  60. }.start();
  61. }
  62.  
  63. public void conditionSignal() throws InterruptedException{
  64. new Thread() {
  65. public void run() {
  66. lock.lock();
  67. try {
  68. System.out.println("3");
  69. condition.signal();
  70. System.out.println("4");
  71. } finally {
  72. lock.unlock();
  73. }
  74. };
  75. }.start();
  76. }
  77.  
  78. public static void main(String[] args) throws InterruptedException {
  79. ThreadTest2 test = new ThreadTest2();
  80.  
  81. test.conditionWait();
  82.  
  83. Thread.sleep(2000);
  84.  
  85. test.conditionSignal();
  86. }
  87. }
Add Comment
Please, Sign In to add comment