• java学习多线程之死锁


    形成死锁的前提是同步代码块嵌套。

    什么是死锁?当一个线程拿到锁以后在这个锁内部的代码需要访问另一段的代码的时候另外一个程序的锁被另外一个线程拿到,这样的话,就造成了两个锁互不想让程序没法往下执行的这种状况就是死锁。

     1 class DeadDemoA implements Runnable
     2 {
     3     Object  obj = new Object();
     4     int x = 0;
     5 
     6     public void run()
     7     {
     8     
     9 
    10             if(x == 0){
    11 
    12                 while(true)
    13                 {
    14 
    15                     synchronized(obj)
    16                     {
    17                         System.out.println("-----");
    18 
    19                             showA();
    20                     
    21                         System.out.println("-----");
    22 
    23                     }
    24                     showA();
    25 
    26 
    27                 }
    28 
    29             }else{
    30 
    31                 while(true)
    32                 {
    33                     showA();
    34                     x = x+1;
    35                     x = x%2;
    36                 }
    37                     
    38             }
    39         
    40 
    41     }
    42 
    43     public synchronized void showA()
    44     {
    45 
    46         synchronized (obj)
    47         {
    48 
    49             System.out.println("SI suo");
    50 
    51         }
    52 
    53 
    54     }
    55 
    56 }
    57 
    58 
    59 class DeadLock
    60 {
    61 
    62     public static void main(String[] args) {
    63         
    64         DeadDemoA a = new DeadDemoA();
    65         Thread t1 = new Thread(a);
    66         Thread t2 = new Thread(a);
    67         Thread t3 = new Thread(a);
    68         Thread t4 = new Thread(a);
    69 
    70         t1.start();
    71         t2.start();
    72         t3.start();
    73         t4.start();
    74 
    75 
    76     }
    77 
    78 }

    以上就是一个死锁。

    简单来说就是一段程序代码中有两个同步的代码块嵌套,也就是有两个锁,但是一个锁的执行依赖另外一个锁。

    代码中A方法要调用B方法,同时这两个都是同步的,有各自的锁A比如说有A锁,B比如说有B锁,那么A方法要执行完成的话,必须同时具备A锁和B锁,如果此时B锁被其他线程拿到的话,这个时候A就等待B锁,同时其他线程也没法执行A方法,这个时候就是相互僵持,成为死锁。同时要注意,锁这个概念是针对线程的,跟本身的程序没有关系。我们再来写个死锁的示例:

     1 class DDlock1 implements Runnable
     2 {
     3 
     4     private Object obj = new Object();
     5 
     6     public void run()
     7     {
     8                 
     9             
    10             while(true)
    11             {
    12                 synchronized (obj)
    13                 {                                                
    14                     show();
    15 
    16                 }
    17                     show();//let other thread get the lock    
    18             }
    19 
    20     }
    21 
    22     public synchronized void show()
    23     {
    24 
    25         synchronized(obj){
    26 
    27             System.out.println("Dead lock example~");
    28         }
    29             
    30 
    31     }
    32 
    33 }
    34 
    35 class DeadLock1
    36 {
    37 
    38     public static void main(String[] args) {
    39         
    40         DDlock1 d = new DDlock1();
    41 
    42         Thread t1 = new Thread(d);
    43         Thread t2 = new Thread(d);
    44         Thread t3 = new Thread(d);
    45         Thread t4 = new Thread(d);
    46 
    47         t1.start();
    48         t2.start();
    49         t3.start();
    50         t4.start();
    51 
    52 
    53     }
    54 
    55 }

  • 相关阅读:
    十五周作业
    第十四周总结
    十三周总结与感想
    排球比赛积分规则
    Centos + nginx + JBOSS AS 7 搭建Java web application
    eclipse + maven + jboss 遇到ClassNotFoundException
    “/”应用程序中的服务器错误。 纠错方法
    Sharepoint 问题集锦
    Sharepoint 问题集锦
    Sharepoint 问题集锦
  • 原文地址:https://www.cnblogs.com/sunchuanzhen/p/3379145.html
Copyright © 2020-2023  润新知