简体   繁体   English

没有获得用于打印具有不同线程的偶数和奇数的desried输出

[英]Not getting desried output for printing even and odd numbers with different threads

Hi I am trying to print the even and odd numbers with the help of two different threads but i am not able to get the output .Below is the program . 嗨,我试图在两个不同的线程的帮助下打印偶数和奇数,但我无法获得输出。下面是程序。

public class oddeven 
{
    static volatile Integer t= 0;
public static void main(String as[])
{  
    Object ob = new Object ();
    /*oddrunnable or= ;
    evenrunnable er=;*/

    Thread t1 = new Thread (new oddrunnable(t,ob),"odd");
    Thread t2 = new Thread ( new evenrunnable(t,ob),"even");

    t1.start();
    t2.start();




}

}
class oddrunnable implements Runnable
{
    Integer t ;
    Object ob = new Object ();
    public oddrunnable(Integer t,Object ob)
    {
        this.t =  t;
        this.ob= ob;
    }
    @Override
    public void run()
    {
        // TODO Auto-generated method stub

        synchronized (ob) { 
        while (true)
        {if (t%2==0)
            {
                try {
                    ob.wait();
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            try {
                Thread.sleep(300);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            System.out.println("Current thread id "+Thread.currentThread().getName()+"value of integer is "+t);;
            t++;
            ob.notify();
        }
    }
    }

}
class evenrunnable implements Runnable
{
    Integer t ;
    Object ob = new Object ();
    public evenrunnable(Integer t, Object ob)
    {
        this.t =  t;
        this.ob= ob;
    }


    @Override
    public void run()
    {
        synchronized (ob) {
            while (true)
            {
            if (t%2!=0)
                {
                try {
                        ob.wait();
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }try {
                    Thread.sleep(300);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                t++;
                System.out.println("Current thread id "+Thread.currentThread().getName()+"value of integer is "+t);

                ob.notify();
            }
        }
    }

}

i want the output as below : 我想要的输出如下:

Current thread id evenvalue of integer is 0
Current thread id oddvalue of integer is 1
Current thread id evenvalue of integer is 2
Current thread id oddvalue of integer is 3
Current thread id evenvalue of integer is 4
Current thread id oddvalue of integer is 5
Current thread id evenvalue of integer is 6
Current thread id oddvalue of integer is 7

but i am getting it wrong as below . 但是我错了,如下所示。

Current thread id evenvalue of integer is 1
Current thread id oddvalue of integer is 0
Current thread id oddvalue of integer is 1
Current thread id evenvalue of integer is 2
Current thread id evenvalue of integer is 3
Current thread id oddvalue of integer is 2
Current thread id oddvalue of integer is 3
Current thread id evenvalue of integer is 4
Current thread id evenvalue of integer is 5
Current thread id oddvalue of integer is 4
Current thread id oddvalue of integer is 5
Current thread id evenvalue of integer is 6
Current thread id evenvalue of integer is 7
Current thread id oddvalue of integer is 6

Also while trying to debug the program gets stuck up . 另外,在尝试调试程序时会卡住。 where i am going wrong ? 我要去哪里错了?

One thing wrong with your code is that both evenrunnable and oddrunnable increment a different variable. 代码的一件事是evenrunnableoddrunnable增加了一个不同的变量。 Each has its own t member. 每个都有自己的t成员。

If you want them to increment static volatile Integer t= 0; 如果要让它们递增static volatile Integer t= 0; , remove the Integer t instance variable from those classes. ,从这些类中删除Integer t实例变量。

Passing the static t to the constructor of those classes only passes a copy of its reference. 将静态t传递给这些类的构造函数只会传递其引用的副本。 Since Integer is immutable, t++ creates a new Integer and assigns it to the instance variable t , which doesn't affect the static variable t . 由于Integer是不可变的,因此t++创建一个新的Integer并将其分配给实例变量t ,这不会影响静态变量t

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

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