简体   繁体   中英

What is happening while printing even odd numbers using interrupt method?

I am trying to print even odd numbers using two threads with interrupt method. I refereed code from internet and wrote a code showing below.It prints properly but after prints 20,program is continuing it's execution.

  1. What change do i have to make in the code to stop the execution of the program?

  2. Without oldNum check code is working fine. Is there any logic to provide oldNum check ?

  3. If I remove Thread.sleep(1000L) from Line-a then it only prints "Even Thread prints 20" and continue execution.What is happening here?

  4. Provided break points inside run() method and inside for loop of main method , run() methods break points are not hitting.Why this is happening?

In short I want to know what is the code flow here.
Thanks
Vikash

public class PrintOddEvenUsingInterrupt {    

public static volatile int count;
  public static void main(String[] args) throws InterruptedException {
    Thread oddThread = new Thread(new OddInterruptThread(), "Odd Thread ");
    Thread evenThread = new Thread(new EvenInterruptThread(),"Even Thread ");

    oddThread.start();
    evenThread.start();
    for (int i = 0; i < 20; i++) {
        count++;
        oddThread.interrupt();//Break points works here 
        evenThread.interrupt();
        Thread.sleep(1000L);// Line-a 
    }
}

static class OddInterruptThread implements Runnable {
    public void run() {
        int oldNum = 0;//Break points doesn't works here
        while (true) {
            try {
                Thread.sleep(Integer.MAX_VALUE);
            } catch (InterruptedException e) {
            }

            if (oldNum != count && count % 2 == 1) {
                System.out.println(Thread.currentThread().getName()
                        + " prints " + count);
                oldNum = count;
            }
        }
    }
}

static class EvenInterruptThread implements Runnable {
    public void run() {
        int oldNum = 0;//Break points doesn't works here
        while (true) {
            try {
                Thread.sleep(Integer.MAX_VALUE);
            } catch (InterruptedException e) {
            }

            if (oldNum != count && count % 2 == 0) {
                System.out.println(Thread.currentThread().getName()
                        + " prints " + count);
                oldNum = count;
            }
        }
    }
}
}    

The reason your program is not stopping is: while your main thread exits, your odd and even threads sleeps in infinite loop.

You will need to define a stopping condition for your threads to come out.

One way to achieve this is via using conditions. Eg:

public volatile static boolean oddFinished = false;
public volatile static boolean evenFinished = false;

Then in your threads, instead of looping infinitely, loop against condition while (! oddFinished){ // also change your thread sleep to sleep for fewer time interval (say 1000L or whatever your program wants to wait for) }

Do the same for even thread...

while (! evenFinished){
    // also change your thread sleep to sleep for fewer time interval (say 1000L or whatever your program wants to wait for)
}

And in the main thread, you can add the following code after your for loop ends...

oddFinished = true;
evenFinished = true;

oddThread.join();
evenThread.join();

This will allow your code to stop gracefully.

I think the simplest solution will be to make your threads demons. Just add the following lines before starting your thteads.

oddThread.setDaemon(true);
evenThread.setDaemon(true);

And your program will exit immediately after exiting from main.

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