试图在两个线程之间创建死锁

时间:2016-10-08 11:54:32

标签: java multithreading deadlock

通过访问Threads中的print方法在两个线程之间创建死锁。我使用了循环屏障,以便两个线程同时启动。如果我是正确的,我的打印方法不需要时间,因此它会被两个线程共享并且不会导致死锁。

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;

public class TWOTHREADDEADLOCLK {

    static int b =0;
    synchronized static void print()
    {

        System.out.println(Thread.currentThread().getName() + "     " + b);
    }
    synchronized static int  getb()
    {
        print();
        return b;
    }

    synchronized static void updateb()
    {
        print();
        b=b+10;
    }
    public static void main(String[] args) {

        final CyclicBarrier bar = new CyclicBarrier(2);
        Thread thread1  = new Thread(new Runnable(){

            @Override
            public void run() 
            {
                try {
                    bar.await();

                    Thread.sleep(10000);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                catch ( BrokenBarrierException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName());
                while(true)
                    print();

            }

        });
        Thread thread2  = new Thread(new Runnable(){

            @Override
            public void run() 
            {try {
                bar.await();
            } catch (InterruptedException | BrokenBarrierException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName());
            while(true)
                getb();

            }

        });
        thread1.start();
        thread2.start();
    }
}

2 个答案:

答案 0 :(得分:6)

您无法通过单个屏障创建死锁。死锁背后的想法是(至少)有两个线程,每个线程持有一个不同的锁并尝试锁定另一个。例如,考虑这个简单的例子:

public class TwoLockRunnable implements Runnable {

    private Lock lockInConstructor;
    private Lock lockInRuntime;

    public TwoLockThread(Lock lockInConstructor, Lock lockInRuntime) {
        this.lockInConstructor = lockInConstructor;
        this.lockInRuntime = lockInRuntime;

        this.lockInConstructor.lock();
    }

    @Override
    public void run() {
        lockInRuntime.lock();

        System.out.println("After the lock in run()");
    }

    public static void main(String[] args) {
        Lock lock1 = new ReentrantLock();
        Lock lock2 = new ReentrantLock();

        TwoLockRunnable runnable1 = new TwoLockThread(lock1, lock2);
        TwoLockRunnable runnable2 = new TwoLockThread(lock2, lock1);

        new Thread(runnable1).start();
        new Thread(runnable2).start();
    }
}

第一个线程在其构造函数中锁定lock1,第二个线程在其构造函数中锁定lock2。然后第一个线程在其运行时尝试锁定lock2 - 但它不能由另一个线程保持锁定。类似地,第二个线程在其运行时尝试锁定lock1,并且由于相同的原因而失败。因此,您会遇到死锁,并且永远不会打印消息"After the lock in run()"

答案 1 :(得分:2)

像Mureinik一样,这是'同步'演示:

public class DeadLockAATest {

static void methodA(DeadLockAATest d1, DeadLockAATest d2) {
    synchronized (d1) {
        try {
            Thread.sleep(10);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        synchronized (d2) {
            System.out.println("\t\t\tmethodA:" + Thread.currentThread().getName());
        }
    }
}

public static void main(String[] args) {
    DeadLockAATest d1 = new DeadLockAATest(), d2 = new DeadLockAATest();
    Thread t1 = new Thread(new Runnable() {
        @Override
        public void run() {
            System.out.println("t1-start:" + Thread.currentThread().getName());
            methodA(d1, d2);
            System.out.println("t1-end:" + Thread.currentThread().getName());
        }
    });
    Thread t2 = new Thread(new Runnable() {
        @Override
        public void run() {
            System.out.println("t2-start:" + Thread.currentThread().getName());
            methodA(d2, d1);
            System.out.println("t2-end:" + Thread.currentThread().getName());
        }
    });
    t1.start();
    t2.start();
    System.out.println("deadlock...");
}

}

死锁输出(只有一个contion,可能t2首先开始):

t1-start:Thread-0
deadlock...
t2-start:Thread-1

你可以替换

  

方法A(d2,d1);

  

方法A(d1,d2);

这将输出:

t1-start:Thread-0
t2-start:Thread-1
deadlock...
            methodA:Thread-0
t1-end:Thread-0
            methodA:Thread-1
t2-end:Thread-1

这不是僵局,希望能帮到你。