Home  >  Q&A  >  body text

java - 多线程死锁测试

package test;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.TimeUnit;

/**
 * Created by rhwayfun on 16-4-3.
 */
public class ThreadTest {

    private static DateFormat format = new SimpleDateFormat("HH:mm:ss");

    public synchronized void tryOther(ThreadTest other) throws InterruptedException {
        System.out.println(Thread.currentThread().getName() + " enter tryOther method at " + format.format(new Date()));      
        System.out.println(Thread.currentThread().getName() + " tryOther method is about to invoke other method at " + format.format(new Date()));
        other.other();
    }

    public synchronized void other() throws InterruptedException {
        System.out.println(Thread.currentThread().getName() + " enter other method atatatatat " + format.format(new Date()));
    }

    public static void main(String[] args) throws InterruptedException {
        final ThreadTest d1 = new ThreadTest();
        final ThreadTest d2 = new ThreadTest();

        Thread t1 = new Thread(new Runnable() {
            public void run() {
                try {
                    d1.tryOther(d2);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }, "threadA");

        Thread t2 = new Thread(new Runnable() {
            public void run() {
                try {
                    d2.tryOther(d1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }, "threadB");

        t1.start();
        //让threadA先运行一秒
        TimeUnit.SECONDS.sleep(1);
        t2.start();

    }
}

如上,随便找的产生死锁的代码,问题:
TimeUnit.SECONDS.sleep(1);加上这行后,不存在死锁问题。sleep并不释放锁,为何这边死锁情况会消失。
输出结果为:
threadA enter tryOther method at 15:37:39
threadA tryOther method is about to invoke other method at 15:37:39
threadA enter other method atatatatat 15:37:39
threadB enter tryOther method at 15:37:40
threadB tryOther method is about to invoke other method at 15:37:40
threadB enter other method atatatatat 15:37:40

注掉这行,正常死锁。
输出结果为:
threadB enter tryOther method at 15:37:10
threadA enter tryOther method at 15:37:10
threadB tryOther method is about to invoke other method at 15:37:10
threadA tryOther method is about to invoke other method at 15:37:10

阿神阿神2743 days ago693

reply all(3)I'll reply

  • 大家讲道理

    大家讲道理2017-04-18 10:56:48

    Thread A gets the tryOther lock, but it still needs to get the other lock.
    Thread B gets the tryOther lock, but it still needs to get the other lock.
    It is possible that A has just released the lock and B has just released the tryOther lock.
    At this time But they both want to acquire the other's lock at the same time. At this time, no one will let anyone deadlock. The solution is to prevent the two threads from grabbing the second lock at the same time. Let A stop for a while. But if you adjust the time to Deadlock will occur if you try multiple times per second. It is not recommended to prevent deadlock in this way. If the amount of concurrency is high.

    reply
    0
  • 阿神

    阿神2017-04-18 10:56:48

    Both parties are fighting for the same lock, there will be no deadlock

    reply
    0
  • PHPz

    PHPz2017-04-18 10:56:48

    I suddenly knew why and was too lazy to delete the post. Write down my opinion. If there is any mistake, please correct me and lightly spray.
    When there is no sleep, thread a starts, completes the tryOther method, releases the lock and executes the other method. At this time, b obtains the lock and executes the tryOther method.
    At this time, a In the other method, the required resources are locked by thread b. After executing tryOther, b needs to obtain a resource, resulting in a deadlock.

    After adding sleep. When a executes the tryOther method to release the lock, thread b is not executed at this time, and the other lock is successfully obtained at this time. Thread b is executed after 2s,
    No deadlock environment.

    reply
    0
  • Cancelreply