|
1 | | -package Deadlock_11; |
2 | | - |
3 | | -/** |
4 | | - * <a href="https://wikipedia.org/wiki/Deadlock">Deadlock</a> |
5 | | - * can occur in a situation when a thread is waiting for an object's lock, |
6 | | - * that is acquired by another thread and the second thread is waiting for an |
7 | | - * object lock that is acquired by first thread. Since, both threads are waiting |
8 | | - * for each other to release the lock, the condition is called deadlock. If you |
9 | | - * make sure that all locks are always taken in the same order by any thread, |
10 | | - * deadlocks cannot occur. |
11 | | - * <br><br> |
12 | | - * Codes with minor comments are from |
13 | | - * <a href="http://www.caveofprogramming.com/youtube/"> |
14 | | - * <em>http://www.caveofprogramming.com/youtube/</em> |
15 | | - * </a> |
16 | | - * <br> |
17 | | - * also freely available at |
18 | | - * <a href="https://www.udemy.com/java-multithreading/?couponCode=FREE"> |
19 | | - * <em>https://www.udemy.com/java-multithreading/?couponCode=FREE</em> |
20 | | - * </a> |
21 | | - * |
22 | | - * @author Z.B. Celik <celik.berkay@gmail.com> |
23 | | - */ |
24 | | -public class App { |
25 | | - |
26 | | - public static void main(String[] args) throws Exception { |
27 | | - final Runner runner = new Runner(); |
28 | | - Thread t1 = new Thread(new Runnable() { |
29 | | - public void run() { |
30 | | - try { |
31 | | - runner.firstThread(); |
32 | | - } catch (InterruptedException ignored) {} |
33 | | - } |
34 | | - }); |
35 | | - |
36 | | - Thread t2 = new Thread(new Runnable() { |
37 | | - public void run() { |
38 | | - try { |
39 | | - runner.secondThread(); |
40 | | - } catch (InterruptedException ignored) {} |
41 | | - } |
42 | | - }); |
43 | | - |
44 | | - t1.start(); |
45 | | - t2.start(); |
46 | | - t1.join(); |
47 | | - t2.join(); |
48 | | - runner.finished(); |
49 | | - } |
50 | | -} |
| 1 | +package Deadlock_11; |
| 2 | + |
| 3 | +/** |
| 4 | + * <a href="https://wikipedia.org/wiki/Deadlock">Deadlock</a> |
| 5 | + * can occur in a situation when a thread is waiting for an object's lock, |
| 6 | + * that is acquired by another thread and the second thread is waiting for an |
| 7 | + * object lock that is acquired by first thread. Since, both threads are waiting |
| 8 | + * for each other to release the lock, the condition is called deadlock. If you |
| 9 | + * make sure that all locks are always taken in the same order by any thread, |
| 10 | + * deadlocks cannot occur. |
| 11 | + * <br><br> |
| 12 | + * Codes with minor comments are from |
| 13 | + * <a href="http://www.caveofprogramming.com/youtube/"> |
| 14 | + * <em>http://www.caveofprogramming.com/youtube/</em> |
| 15 | + * </a> |
| 16 | + * <br> |
| 17 | + * also freely available at |
| 18 | + * <a href="https://www.udemy.com/java-multithreading/?couponCode=FREE"> |
| 19 | + * <em>https://www.udemy.com/java-multithreading/?couponCode=FREE</em> |
| 20 | + * </a> |
| 21 | + * |
| 22 | + * @author Z.B. Celik <celik.berkay@gmail.com> |
| 23 | + */ |
| 24 | +public class App { |
| 25 | + |
| 26 | + public static void main(String[] args) throws Exception { |
| 27 | + final Runner runner = new Runner(); |
| 28 | + Thread t1 = new Thread(new Runnable() { |
| 29 | + public void run() { |
| 30 | + try { |
| 31 | + runner.firstThread(); |
| 32 | + } catch (InterruptedException ignored) {} |
| 33 | + } |
| 34 | + }); |
| 35 | + |
| 36 | + Thread t2 = new Thread(new Runnable() { |
| 37 | + public void run() { |
| 38 | + try { |
| 39 | + runner.secondThread(); |
| 40 | + } catch (InterruptedException ignored) {} |
| 41 | + } |
| 42 | + }); |
| 43 | + |
| 44 | + t1.start(); |
| 45 | + t2.start(); |
| 46 | + t1.join(); |
| 47 | + t2.join(); |
| 48 | + runner.finished(); |
| 49 | + } |
| 50 | +} |
0 commit comments