This is the 15th day of my participation in Gwen Challenge

The interviewer asks again: What is a deadlock? This article is on the concept of deadlock began to speak, do not understand how to deadlock, please go to the beginning of the link, first to learn about deadlock

In the last article, we discussed three conditions for deadlock, one of which is holding on to resources! But do we have a mechanism to release the lock after we get the resource?

In Java, there’s a Lock interface and a Lock interface method

There’s a way to break a lock, there’s a tryLock tryLock mechanism, unlock method releases a lock, and we’re going to use a little code, tryLock tryLock mechanism to break a deadlock

public class TryLock {
    private static Lock No13 = new ReentrantLock();// The first lock
    private static Lock No14 = new ReentrantLock();// The second lock

    // Try to get No13 lock, then try to get No14 lock, No14 lock did not get, with No13 lock released
    private static void fisrtToSecond(a) throws InterruptedException {
        String threadName = Thread.currentThread().getName();
        Random r = new Random();
        while(true) {if(No13.tryLock()){
                System.out.println(threadName
                        +" get 13");
                try{
                    if(No14.tryLock()){
                        try{
                            System.out.println(threadName
                                    +" get 14");
                            System.out.println("fisrtToSecond do work------------");
                            break;
                        }finally{ No14.unlock(); }}}finally{ No13.unlock(); }}//Thread.sleep(r.nextInt(3));}}// Try to get No14 lock, then try to get No13 lock, No13 lock failed, with No14 lock released
    private static void SecondToFisrt(a) throws InterruptedException {
        String threadName = Thread.currentThread().getName();
        Random r = new Random();
        while(true) {if(No14.tryLock()){
                System.out.println(threadName
                        +" get 14");
                try{
                    if(No13.tryLock()){
                        try{
                            System.out.println(threadName
                                    +" get 13");
                            System.out.println("SecondToFisrt do work------------");
                            break;
                        }finally{ No13.unlock(); }}}finally{ No14.unlock(); }}//Thread.sleep(r.nextInt(3));}}private static class TestThread extends Thread{

        private String name;

        public TestThread(String name) {
            this.name = name;
        }

        public void run(a){
            Thread.currentThread().setName(name);
            try {
                SecondToFisrt();
            } catch(InterruptedException e) { e.printStackTrace(); }}}public static void main(String[] args) {
        Thread.currentThread().setName("TestDeadLock");
        TestThread testThread = new TestThread("SubTestThread");
        testThread.start();
        try {
            fisrtToSecond();
        } catch(InterruptedException e) { e.printStackTrace(); }}}Copy the code

In the above code, if both threads get the corresponding lock on the first attempt, then if they can’t get the lock on the second attempt, they release the lock along with the first attempt, so that other threads can get the lock.

The above code comments two lines of code with random time for thread sleep. Why add these two methods, the purpose of which is to stagger the time at which the lock is taken, increasing the likelihood that I will release the lock and it will be taken by another thread

If we don’t add thread dormancy methods, one is likely to happen, two threads at the same time, a lock to lock a 13 to 14, then release them at the same time, and at the same time, a given lock, 13 a get 14 lock, and release the lock at the same time, form a loop, two threads have been helpless pain with lock lock is released, Instead of doing anything! This is the legendary live lock! Deadlocks, on the other hand, are completely blocked there, like dead!

That’s the description of live locks! Hope the content is helpful to XDM, XDM give a three even!