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

Java 当使用匿名线程并试图通过两个线程打印奇数、偶数时,线程进入死锁状态,java,multithreading,Java,Multithreading,我试图使用wait和notify通过两个线程重复打印奇数和偶数 我可以通过实现Runnable接口来实现这一点 public class OddEven implements Runnable { public void run() { for (int i = 1; i <= 100; i++) { if (i % 2 == Integer.parseInt(Thread.currentThread().getName())) {

我试图使用wait和notify通过两个线程重复打印奇数和偶数

我可以通过实现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
public类Odd偶数实现可运行{
公开募捐{
对于(int i=1;i
类TestOddEvenonymousThread){
公共静态void main(字符串[]args){
TestOddEvenAnonymousThread t=新的TestOddEvenAnonymousThread();
螺纹oddT=(新螺纹(“奇数”){
公开募捐{

对于(int i=1;i),这里没有死锁,只有一个invite块。是!!由于匿名类,这两个块都引用不同的对象。
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();
                    }
                }
            }
        }
    }
}
    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
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();
    }


}