简体   繁体   English

我使用2个线程最多打印10个奇数的实现有什么问题

[英]What is wrong with my implementation of printing even odd numbers upto 10 using 2 threads

I am trying to print even and odd numbers using two different threads. 我正在尝试使用两个不同的线程打印偶数和奇数。 Can some one point out the mistake I am making. 有人可以指出我犯的错误吗? Currently this is what the output looks like: 当前,输出如下所示:

Odd: 1 赔率:1

going to wait 1 要等待1

notify 2 通知2

going to wait 2 要等待2

Please find the code below: 请在下面找到代码:

 public class EvenOdd {
        public static void main(String[] args) {        
            PrintEvenOdd p1=new PrintEvenOdd(false);
            PrintEvenOdd p2=new PrintEvenOdd(true);
            p1.start();
            p2.start();         
        }   
   }    
class PrintEvenOdd extends Thread{    
    boolean isEven;
    boolean isOdd=true;

    public PrintEvenOdd(boolean e) {
        isEven=e;
    }

    public synchronized void run() {
            if(isEven) {
            for(int i=2;i<=10;i+=2)
            {
                while(isOdd) {
                    try {
                        System.out.println("going to wait 1");
                        wait();
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
                System.out.println("Even: "+i);
                isOdd=true;
                System.out.println("notify 1");
                notifyAll();

            }
            }
            if(!isEven) {
            for(int i=1;i<=10;i+=2) {
                while(!isOdd) {
                    try {
                        System.out.println("going to wait 2");
                        wait();
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
                System.out.println("Odd: "+ i);
                isOdd=false;
                System.out.println("notify 2");
                notifyAll();
            }
        }
    }
}

As the main idea of your algorithm is not definitely clear for me, I'll just point out where is the bug & you can try to fix it by yourself. 由于您对算法的主要想法对我来说还不是很清楚,所以我仅指出错误在哪里,您可以尝试自己修复它。

When you start your threads one after another: p1.start(); p2.start(); 当您一个接一个地启动线程时: p1.start(); p2.start(); p1.start(); p2.start(); they are executing in the same order as they we started. 它们以与开始时相同的顺序执行。

When the first thread calls synchronized void run() method, than it locks the monitor on PrintEvenOdd.class . 当第一个线程调用同步的void run()方法时,它将监视器锁定在PrintEvenOdd.class That means the second thread will wait until the first releases the monitor (say will not execute any single line of code). 这意味着第二个线程将等到第一个线程释放监视器(例如将不执行任何一行代码)。

Going further in debugger by the first thread execution flow you'll see operations below: 在第一个线程执行流程中进一步在调试器中,您将看到以下操作:

  • drop down into if(!isEven) condition 下降到if(!isEven)条件
  • set isOdd=false during i=1 loop iteration i=1循环迭代期间设置isOdd=false
  • goes to endless wait() during i=2 loop iteration i=2循环迭代期间进入无尽的wait()

As wait operation does not release the class monitor you're getting situation when both threads are suspended. 由于wait操作不会释放类监视器,因此当两个线程都挂起时会遇到这种情况。

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

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