简体   繁体   中英

Print odd and even with 2 threads get wrong

I want to print odd and even from 0 to 1000, the simple code are as follow using wait(),notifyAll() and synchronized to lock this instance. But the result stops printing 0 and 1, I am confused by this, did I miss something or the synchronized key-word isn't used proper? Can someone explain this, I've trying to figure it out for several hours,yet get nothing...

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

The result is:

Even thread:    0
Odd thread: 1

and the JVM is still running,But I want to print is "0 1 2 3 4 5....". I don't know why it's wrong.

Solved ,thank you!!!

Explain: And key-word "this" should refer to a specific object, I thought it refers to the outer class instance for granted. And what I do is just create a Object instance called obj , and synchronized,wait, notifyAll with this "obj". Just a problem of reference. I feel I am a little dumb...

I found I can't accept my answer within 2 days:"accept × You can accept your own answer in 2 days"

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

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