简体   繁体   中英

Thread entered in to deadlock condition when using anonymous thread and trying to print odd, even numbers by two thread

I was trying to print odd, even numbers by two threads repetitively using wait and notify.

I can achieve this by implementing Runnable interface

public class OddEven implements Runnable {
    public void run() {
        for (int i = 1; i <= 100; i++) {
            if (i % 2 == Integer.parseInt(Thread.currentThread().getName())) {
                synchronized (this) {
                    notifyAll();
                    System.out.println((Thread.currentThread().getName().equals("1") ? "odd : " : "even: ") + i);
                    try {
                        if (i != 100)
                            wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }

    public static void main(String[] args) {
        OddEven obj = new OddEven();
        Thread t1 = new Thread(obj, "1");
        Thread t2 = new Thread(obj, "0");
        t1.start();
        t2.start();
    }
}

    O/P is:

    Odd : 1
    Even: 2
    Odd : 3
    Even: 4
    Odd : 5
    .
    .
    .
    .
    Odd : 97
    Even: 98
    Odd : 99
    Even: 100

Then I tried the same thing by extending Thread class

public class Test {
    public static void main(String[] args) {
        Test t = new Test();
        new OddThraed(t).start();
        new EvenThraed(t).start();
    }
}

class OddThraed extends Thread {
    Test t;

    public OddThraed(Test t) {
        this.t = t;
    }

    public void run() {
        for (int i = 1; i <= 100; i++) {
            if (i % 2 == 1) {
                synchronized (t) {
                    t.notifyAll();
                    System.out.println("Odd : " + i);
                    try {                       
                        if (i != 100)
                            t.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }
}

class EvenThraed extends Thread {
    Test t;

    public EvenThraed(Test t) {
        this.t = t;
    }

    public void run() {
        for (int i = 1; i <= 100; i++) {
            if (i % 2 == 0) {
                synchronized (t) {
                    t.notifyAll();
                    System.out.println("Even: " + i);
                    try {                       
                        if (i != 100)
                            t.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }
}

The O/P was as expected

Then I think there are too many classes and tried to achieve this with using anonymous thread

    public class TestOddEvenAnonymousThread {
        public static void main(String[] args) {
            (new Thread("Odd") {
                public void run() {
                    for (int i = 1; i <= 100; i++) {
                        if (i % 2 == 1) {
                            synchronized (this) {
                                notifyAll();
                                System.out.println("Odd : " + i);
                                try {
                                    if (i != 100)
                                        wait();
                                } catch (InterruptedException e) {
                                    e.printStackTrace();
                                }
                            }
                        }
                    }
                }
            }).start();

            (new Thread("Even") {
                public void run() {
                    for (int i = 1; i <= 100; i++) {
                        if (i % 2 == 0) {
                            synchronized (this) {
                                notifyAll();
                                System.out.println("Even: " + i);
                                try {
                                    if (i != 100)
                                        wait();
                                } catch (InterruptedException e) {
                                    e.printStackTrace();
                                }
                            }
                        }
                    }
                }
            }).start();

        }
    }

But this time O/P is

Odd : 1
Even: 2

and there is deadlock can someone explain why the last one not showing the appropriate o/p where I am just trying to achieve this with anonymous thread not by extending separate thread class(which I did in second approach above)

class TestOddEvenAnonymousThread {

    public static void main(String[] args) {
        TestOddEvenAnonymousThread t = new TestOddEvenAnonymousThread();
        Thread oddT = (new Thread("Odd") {
            public void run() {
                for (int i = 1; i <= 100; i++) {
                    if (i % 2 == 1) {
                        synchronized (t) {
                            t.notifyAll();
                            System.out.println("Odd : " + i);
                            try {
                                if (i != 100)
                                    t.wait();
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                }
            }
        });
        Thread eventT = (new Thread("Even") {
            public void run() {
                for (int i = 1; i <= 100; i++) {
                    if (i % 2 == 0) {
                        synchronized (t) {
                            t.notifyAll();
                            System.out.println("Even: " + i);
                            try {
                                if (i != 100)
                                    t.wait();
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                }
            }
        });
                oddT.start();
                eventT.start();
    }


}

When you referred this in old and event thread unknowingly you applied synchronization block on different and invoked notifyAll and wait on those respective objects

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM