crazyStart
crazyStart

Reputation: 127

Trying to create a deadlock between two threads

For creating a deadlock in between two threads by accessing print method into Threads. I have used cyclic Barrier so that both of the thread starts at same time. If I am correct my print method is not taking time, for that reason it is getting shared by the two threads and not causing a Deadlock.

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();
    }
}

Upvotes: 1

Views: 278

Answers (2)

windc
windc

Reputation: 151

like Mureinik's, this is the 'synchronized' demo:

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...");
}

}

the deadlock output(just one contion, maybe t2 starts first):

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

you can replace

methodA(d2, d1);

to

methodA(d1, d2);

and this will output:

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

and this is not deadlock, hope to help you.

Upvotes: 3

Mureinik
Mureinik

Reputation: 311063

You can't create a deadlock with a single barrier. The idea behind a deadlock is to have (at least) two threads, each one holding a different lock and attempting to take a lock on the other one. E.g., consider this simple example:

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();
    }
}

The first thread locks lock1 in its constructor and the second locks lock2 in its constructor. The first thread then tries to lock lock2 when its run - but it can't since lock is held by the other thread. Similarly, the second thread attempts to lock lock1 when its run, and fails for the same reason. Thus, you get a deadlock, and the message "After the lock in run()" is never printed.

Upvotes: 6

Related Questions