小编Raj*_*aju的帖子

为什么没有发生死锁?

我对Deadlock的行为感到困惑.如果我先写a.foo(b),然后t.start()在Deadlock类的构造函数中,那么死锁不会发生,但为什么呢?

class A {

    synchronized void foo(B b) {
        String name = Thread.currentThread().getName();
        System.out.println(name + " entered A.foo");
        try {
            Thread.sleep(2000);
        } catch (Exception e) {
            System.out.println("A Interrupted");
        }
        System.out.println(name + " trying to call B.last()");
        b.last();

    }

    synchronized void last() {
        System.out.println("Inside A.last");
    }
}

class B {


    synchronized void bar(A a) {

        String name = Thread.currentThread().getName();
        System.out.println(name + " entered B.bar");
        try {
            Thread.sleep(1000);
        } catch (Exception e) {
            System.out.println("B Interrupted");
        }
        System.out.println(name + …
Run Code Online (Sandbox Code Playgroud)

java multithreading deadlock

1
推荐指数
1
解决办法
111
查看次数

标签 统计

deadlock ×1

java ×1

multithreading ×1