多线程-死锁及死锁示例

 1 package multithread3;
 2 
 3 
 4 
 5 /*
 6  * 死锁:常见情景之一:同步的嵌套。
 7  * 面试可能会让写死锁
 8  */
 9 class Ticket implements Runnable {
10     private /*static*/ int num = 200;
11     Object obj = new Object();
12     boolean flag = true;
13     public void run() {
14 
15 //        System.out.println("this:"+this);
16         if (flag) {
17             while(true) {
18                 synchronized (obj) {
19                         show();
20                     }                
21                 }
22                                     
23             
24         }else {
25             while(true) {
26                 this.show();
27             }
28         }
29     }
30         
31     
32     public  synchronized void show() {
33         
34         synchronized (obj) {
35             if (num>0) {
36                 try {
37                     Thread.sleep(10);//sleep方法有个抛出
38                 } catch (InterruptedException e) {
39                 }
40                 //
41                 System.out.println(Thread.currentThread().getName()+"....function...."+ num--);
42             }
43         }
44         
45     }
46 }
47 
48 //会出现两个相同的票数,可能会出现0票,因为用了不同的锁,同步函数的锁和代码块的锁不一样,同步函数仅仅是函数代表了同步性,本身不带锁,
49 public class DeadLockDemo {
50 
51     public static void main(String[] args) {
52         // TODO Auto-generated method stub
53         Ticket t = new Ticket();//创建一个线程任务对象。
54         
55 
56         System.out.println("t:"+t);
57         Thread t1 = new Thread(t);
58         Thread t2 = new Thread(t);
59 //        Thread t3 = new Thread(t);
60 //        Thread t4 = new Thread(t);
61 
62         t1.start();
63         try {
64             Thread.sleep(10);
65         } catch (InterruptedException e) {
66             
67         }
68         t.flag = false;
69         t2.start();
70 //        t3.start();
71 //        t4.start();
72     }
73 
74 }
DeadLockDemo
 1 package multithread3;
 2 
 3 
 4 class Test implements Runnable{
 5     private boolean flag;
 6     Test(boolean flag){
 7         this.flag = flag;
 8     }
 9     public void run() {
10         if (flag) {
11             synchronized (MyLock.locka) {
12                 System.out.println(Thread.currentThread().getName()+"if locka....");
13                 synchronized (MyLock.lockb) {
14                     System.out.println(Thread.currentThread().getName()+"if lockb....");
15                 }
16             }
17         }else {
18             synchronized (MyLock.lockb) {
19                 System.out.println(Thread.currentThread().getName()+"else lockb...");
20                 synchronized (MyLock.locka) {
21                     System.out.println(Thread.currentThread().getName()+"else locka....");
22                 }
23             }
24         }
25             
26             
27     }
28 }
29 
30 class MyLock{
31     public static final Object locka = new Object();
32     public static final Object lockb = new Object();
33     
34 }
35 public class DeadLockTest {
36 
37     public static void main(String[] args) {
38         // TODO Auto-generated method stub
39         Test a = new Test(true);
40         Test b = new Test(false);
41         
42         Thread t1 = new Thread(a);
43         Thread t2 = new Thread(b);
44         t1.start();
45         t2.start();
46     }
47 
48 }
DeadLockTest

 

posted @ 2021-11-10 16:55  doremi429  阅读(16)  评论(0)    收藏  举报