简体   繁体   中英

Print even and Odd number using two different threads

I Was trying to run this code but its getting stuck. I do not want to use the runnable approach. Just wanted to know what i am doing wrong here.

package com.learning.threads;

public class OddThread extends Thread {

    private Integer count;
    Object lock;

    public OddThread(Integer count,Object lock) {
        this.count = count;
        this.lock=lock;
    }

    @Override
    public void run() {

        while (count<1000) {
            synchronized (lock) {
                System.out.println("sdsd"+(count.intValue() % 2));
                if ((count.intValue() % 2) == 0) {
                    try {
                        lock.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                } else {
                    System.out.println("printing the odd number" + count);
                    count++;
                    lock.notify();
                }

            }

        }
    }
}


package com.learning.threads;

public class EvenThread extends Thread {


    private Integer count;

    Object lock;

    public EvenThread(Integer count,Object lock){
        this.count=count;
        this.lock=lock;
    }

    public void run(){

        while(count < 1000){
             synchronized (lock) {
                 System.out.println((count.intValue()%2));
                if((count.intValue()%2)!=0){
                    try {
                        lock.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }


                }else{
                    System.out.println("printing the even number"+ ++count);

                    lock.notify();
                }

            }


        }

    }

}
public class ThreadClass {

    public static void main(String[] args) {


        Integer count=new Integer(1);

        Object lock=new Object();

        EvenThread even=new EvenThread(count,lock);

        OddThread odd=new OddThread(count,lock);

        odd.start();
        even.start();


    }

Seems like an odd way to do this. School assignment?

Anyway, so I think I see what you're trying, but flipping back and forth using the notify, but I don't think it's going to work like you expect.

You're calling wait and notify on the lock, but you're still in the synchronized block. Only one thread can be in that block at a time. You'll need to have it drop out of that block, and then try to notify the other guy. That means redoing your code a bit to drop out and then try the wait/notify code outside of the synchronized block. With how you do the increment that might work for you.

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