演示死锁并排查问题

死锁:

由于资源竞争或通信问题造成的一个阻塞现象,若无外力作用无法推进下去.
当线程互相持有对方所需要的资源时,会互相等待对方释放资源,如果线程都不主动释放所占有的资源,将产生死锁。

条件

1 < 竞争资源 < 抢占资源的线程数
如果竞争资源为1时,不存在死锁问题,因为不是不是A线程占有就是B线程占有,不会相互等待对方释放

代码演示

import com.enjoy.demo.p1.ch1.class1.SleepTools;

/**
 * @Author: BillYu
 * @Description:演示普通的死锁和解决
 * @Date: Created in 13:29 2019-03-15.
 *
 *
 * 死锁:由于资源竞争或通信问题造成的一个阻塞现象,若无外力作用无法推进下去
 * 1 < 竞争资源 < 抢占资源的线程数
 *
 */
public class NormalDeadLock {
    /**
     * 第一个锁
     */
    private static Object valueFirst = new Object();

    /**
     * 第二个锁
     */
    private static Object valueSecond = new Object();

    /**
     * 先拿第一个锁,再拿第二个锁
     */
    private static  void firstToSecond() throws InterruptedException{
        String threadName = Thread.currentThread().getName();
        synchronized (valueFirst){
            System.out.println(threadName+" get first");
            SleepTools.ms(100);
            synchronized (valueSecond){
                System.out.println(threadName+" get second");
            }
        }
    }

    /**
     * 先拿第二个锁,再拿第一个锁
     */
    private static  void secondToFirst() throws InterruptedException{
        String threadName = Thread.currentThread().getName();
        synchronized (valueSecond){
            System.out.println(threadName+" get second");
            SleepTools.ms(100);
            synchronized (valueFirst){
                System.out.println(threadName+" get first");
            }
        }
    }

    /**
     * 执行先拿第二个锁,再拿第一个锁
     */
    private static class TestThread extends Thread{
        private String name;

        public TestThread(String name){
            this.name = name;
        }
        @Override
        public void run(){
            Thread.currentThread().setName(name);
            try{
                secondToFirst();
            }catch (InterruptedException e){
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) {
        Thread.currentThread().setName("TestDeadLock");
        TestThread testThread = new TestThread("SubTestThread");
        testThread.start();
        try{
            firstToSecond();
        }catch (InterruptedException e){
            e.printStackTrace();
        }

    }
    
}

控制台输出:
SubTestThread get second
TestDeadLock get first

怀疑发生死锁如何排查

通过jps查询应用的id,再通过jstack id查看应用的锁的持有情况(持有的锁 等待的锁)。
C29D169D-3552-42F1-B2C2-E96A659D291B.png
880D17CB-E527-45DD-812A-D908950812D8.png
69C09A17-3FFE-4273-A2A8-AB2227E464A1.png

解决死锁

1.保证加锁的顺序
比如对象的hashcode,可以使用System.identityHashCode() 系统提供的native方法,两个对象identityHashCode相同的概率只有千万分之一。如果对象的identityHashCode相同,可以再在外面加一个总的锁,如果不同就以identityHashCode从小到大获取锁的顺序进行。

2.ReentrantLock
尝试获取锁,直到获取到所有的锁为止,有锁竞争时释放锁。
可能会导致活锁的情况。可休眠随机时长,避免活锁的发生。

活锁

尝试拿锁的机制中,发生多个线程之间相互谦让,不断发生拿锁,释放锁的过程。
解决办法:每个线程休眠随机时长,错开拿锁的时间

已有 14 条评论
  1. cialis buy online

    Good day very cool web site!! Guy .. Beautiful
    .. Superb .. I will bookmark your blog and take the feeds also?
    I am happy to seek out so many useful information right here within the put up, we
    need develop extra strategies on this regard, thanks for sharing.
    . . . . .

    cialis buy online November 19th, 2019 at 02:43 am回复
  2. StevWews

    Propecia Generic Fda Buy Cialis Online Pharmacy Without Prescription Propecia Cortes De Pelo Kamagra Per Nachname

    StevWews January 14th, 2020 at 11:36 pm回复
  3. MatInfoge

    Keflex Metformin Interaction Cialis Cialis On Line Melbourne Vic Fast Del Buy Lasix Water Pills

    MatInfoge January 18th, 2020 at 10:34 am回复
  4. KelSuefab

    Cialis Bayer 20 http://cialibuy.com - Buy Cialis Foro Cialis Levitra Viagra Buy Cialis Propecia Side Effects 5mg

    KelSuefab January 23rd, 2020 at 04:29 am回复
  5. LesAstona

    Buy Online Female Viagra http://cialisir.com - Buy Cialis British Online Pharmacy cialis 20mg price at walmart Priligy Usa Blog

    LesAstona January 28th, 2020 at 03:20 am回复
  6. LesAstona

    Cialis Soft Tabs For Sale http://buyciallisonline.com/# - Cialis Online Pharmacy No Prescription Cialis Propecia Precio Biologia

    LesAstona February 7th, 2020 at 01:50 pm回复
  7. StevWews

    Acheter Cialis Generic http://abcialisnews.com - viagra vs cialis Vente Cialis En Belgique online pharmacy Trouver Du Viagra A Paris

    StevWews February 10th, 2020 at 11:42 pm回复
  8. MatInfoge

    Cialis En Mujeres http://abuycialisb.com - Cialis Viagra Ou Cialis Ordonnance cialis Propecia Effects Impotence

    MatInfoge February 26th, 2020 at 08:19 pm回复
  9. LesAstona

    Secure Free Shipping Hydrochlorothiazide Direct Delivered On Saturday http://buycialisuss.com - cialis online Acquista Propecia Finasteride 1mg Buy Cialis Bentyl Store

    LesAstona March 1st, 2020 at 06:13 am回复
  10. Stepsok

    Buy Lasix Water Pills Buy Cialis Forum Cialis Achat generic cialis canada Acheter Du Amoxicillin Moins Cher Medicament

    Stepsok March 8th, 2020 at 03:33 pm回复
  11. Stepsok

    Propecia Espana Se cialis Achat Cialis Pour Femme cialis Propecia Precios

    Stepsok March 13th, 2020 at 05:08 pm回复
  12. Stepsok

    Propecia Complications Women cialis 5mg best price New Canadian Meds cialis canada Cat Uri Amoxicillin Dosage

    Stepsok March 16th, 2020 at 03:05 pm回复
  13. Stepsok

    Levaquin No Prescription With Free Shipping Store Wisconsin cialis generic tadalafil Viagra Kaufen Schnelle Lieferung Cialis isotretinoin tablet in germany

    Stepsok March 22nd, 2020 at 08:10 pm回复
  14. Janmit

    Viagra Online Gunstig Cialis Ajanta Kamagra 100 Cialis Resistance To Amoxicillin

    Janmit March 23rd, 2020 at 07:28 pm回复
发表新评论