Guest User

Untitled

a guest
Nov 18th, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.75 KB | None | 0 0
  1. public class TestThread {
  2. public static Object Lock1 = new Object();
  3. public static Object Lock2 = new Object();
  4.  
  5. public static void main(String args[]) {
  6. ThreadDemo1 T1 = new ThreadDemo1();
  7. ThreadDemo2 T2 = new ThreadDemo2();
  8. T1.start();
  9. T2.start();
  10. }
  11.  
  12. private static class ThreadDemo1 extends Thread {
  13. public void run() {
  14. synchronized (Lock1) {
  15. System.out.println("Thread 1: Holding lock 1...");
  16.  
  17. try { Thread.sleep(10); }
  18. catch (InterruptedException e) {}
  19. System.out.println("Thread 1: Waiting for lock 2...");
  20.  
  21. synchronized (Lock2) {
  22. System.out.println("Thread 1: Holding lock 1 & 2...");
  23. }
  24. }
  25. }
  26. }
  27. private static class ThreadDemo2 extends Thread {
  28. public void run() {
  29. synchronized (Lock2) {
  30. System.out.println("Thread 2: Holding lock 2...");
  31.  
  32. try { Thread.sleep(10); }
  33. catch (InterruptedException e) {}
  34. System.out.println("Thread 2: Waiting for lock 1...");
  35.  
  36. synchronized (Lock1) {
  37. System.out.println("Thread 2: Holding lock 1 & 2...");
  38. }
  39. }
  40. }
  41. }
  42. }
  43.  
  44. Thread 1: Holding lock 1...
  45. Thread 2: Holding lock 2...
  46. Thread 1: Waiting for lock 2...
  47. Thread 2: Waiting for lock 1...
  48.  
  49. public class TestThread {
  50. public static Object Lock1 = new Object();
  51. public static Object Lock2 = new Object();
  52.  
  53. public static void main(String args[]) {
  54. ThreadDemo1 T1 = new ThreadDemo1();
  55. ThreadDemo2 T2 = new ThreadDemo2();
  56. T1.start();
  57. T2.start();
  58. }
  59.  
  60. private static class ThreadDemo1 extends Thread {
  61. public void run() {
  62. synchronized (Lock1) {
  63. System.out.println("Thread 1: Holding lock 1...");
  64.  
  65. try {
  66. Thread.sleep(10);
  67. } catch (InterruptedException e) {}
  68. System.out.println("Thread 1: Waiting for lock 2...");
  69.  
  70. synchronized (Lock2) {
  71. System.out.println("Thread 1: Holding lock 1 & 2...");
  72. }
  73. }
  74. }
  75. }
  76. private static class ThreadDemo2 extends Thread {
  77. public void run() {
  78. synchronized (Lock1) {
  79. System.out.println("Thread 2: Holding lock 1...");
  80.  
  81. try {
  82. Thread.sleep(10);
  83. } catch (InterruptedException e) {}
  84. System.out.println("Thread 2: Waiting for lock 2...");
  85.  
  86. synchronized (Lock2) {
  87. System.out.println("Thread 2: Holding lock 1 & 2...");
  88. }
  89. }
  90. }
  91. }
  92. }
  93.  
  94. Thread 1: Holding lock 1...
  95. Thread 1: Waiting for lock 2...
  96. Thread 1: Holding lock 1 & 2...
  97. Thread 2: Holding lock 1...
  98. Thread 2: Waiting for lock 2...
  99. Thread 2: Holding lock 1 & 2...
Add Comment
Please, Sign In to add comment