简体   繁体   English

线程在使用匿名线程并尝试通过两个线程打印奇数、偶数时进入死锁状态

[英]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我可以通过实现 Runnable 接口来实现这一点

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然后我通过扩展线程 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 O/P 符合预期

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)并且有死锁有人可以解释为什么最后一个没有显示适当的o / p,我只是想用匿名线程而不是通过扩展单独的线程类来实现这一点(我在上面的第二种方法中做了)

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当您在旧线程和事件线程中不知不觉中引用此内容时,您将同步块应用于不同的并调用notifyAllwait这些相应的对象

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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