繁体   English   中英

打印奇数且偶数有2个线程出错

[英]Print odd and even with 2 threads get wrong

我想打印奇数,甚至从0到1000,简单代码如下,使用wait(),notifyAll()并同步以锁定此实例。 但是结果停止打印0和1,对此我感到困惑,是否错过了某些内容,或者同步关键字使用不正确? 有人可以解释一下吗,我试图弄清楚几个小时,却一无所获...

public class Main {
    public static void main(String[] args) {
        final int count = 1000;
        new Thread() {
            public void run() {
                for (int j = 0; j <= count; j = j + 2) {
                    synchronized (this) {
                            System.out.println("Even thread:\t" + j);
                        notifyAll();
                        try {
                            wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }.start();

        new Thread() {
            public void run() {
                for (int j = 1; j <= count; j = j + 2)
                    synchronized (this) {
                        System.out.println("Odd thread:\t" + j);
                        notifyAll();
                        try {
                            wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
            }
        }.start();
    }
}

结果是:

Even thread:    0
Odd thread: 1

并且JVM仍在运行,但是我要打印的是“ 0 1 2 3 4 5 ....”。 我不知道为什么错了。

解决了,谢谢!!!

解释:关键字“ this”应该指向一个特定的对象,我认为它指的是外部类实例。 我要做的只是创建一个名为obj的Object实例,并使用此“ obj”同步,等待,notifyAll。 只是一个参考问题。 我觉得我有点傻...

我发现我无法在2天内接受答案:“接受×您可以在2天内接受自己的回答”

public class Main {
    public static void main(String[] args) {
        //String obj="";
        Object obj=new Object();

        final int count = 1000;
        new Thread() {
            public void run() {
                for (int j = 0; j <= count; j = j + 2) {
                    synchronized (obj) {
                        System.out.println("Even thread:\t" + j);
                        if(j==1000) System.exit(0);//exit the JVM when prints 1000
                        obj.notifyAll();
                        try {
                            obj.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }.start();

        new Thread() {
            public void run() {
                for (int j = 1; j <= count; j = j + 2)
                    synchronized (obj) {
                        System.out.println("Odd thread:\t" + j);
                        obj.notifyAll();
                        try {
                            obj.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
            }
        }.start();
    }
}

暂无
暂无

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

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