简体   繁体   English

与线程相关的程序不起作用

[英]Threads related program not working

I am trying to run the following program for threads and expecting that the variable object should be updated by two different threads. 我正在尝试为线程运行以下程序,并期望变量对象应由两个不同的线程更新。 Very strangely, it is just showing values 'addition -1' and 'multiply -2'. 非常奇怪的是,它仅显示值“加法-1”和“乘-2”。 But when I am using debugger and debugging it step by step, then 'addition' and multiplication is happening as expected. 但是,当我使用调试器并逐步调试它时,“加法”和乘法正按预期发生。 If I put 'Thread.sleep' in 'run' method of 'P' for 1000 ms. 如果我将“ Thread.sleep”放在“ P”的“运行”方法中1000毫秒。 It works fine. 工作正常。 I have commented out the code of 'Thread.sleep', but if you uncomment it and run, you will find expected results. 我已经注释掉了“ Thread.sleep”的代码,但是如果您取消注释并运行它,则会发现预期的结果。 Can anyone clarify this to me, please? 有人可以向我澄清一下吗?

public class TestingThreads {   

    public static void main(String[] args) {

        Variable b = new Variable();
        Thread tp = new Thread(new P(b));
        Thread tq = new Thread(new Q(b));
        tp.start();
        tq.start();
        try {
            tp.join();
            tq.join();
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

}

class Variable {
    int i = 0;
    boolean on = true;

    public synchronized void addition() {
        if (!on) {
            try {
                wait();
            } catch (InterruptedException e) {

                e.printStackTrace();
            }

        }
        i = i + 1;
        on=false;
        notify();
        System.out.println("addition " + i);



    }

    public synchronized void multiply() {

        if (on) {
            try {
                wait();
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }
        i = i * 2;
        on=true;
        notify();
        System.out.println("multiply " + i);

    }

}

class P implements Runnable {

    Variable b;

    P(Variable b) {
        this.b = b;
    }


    @Override
    public void run() {


        while (true) {
            /*uncomment it and it will work fine.*/

            /*try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }*/

            b.addition();

        }
    }

}

class Q implements Runnable {

    Variable b;

    Q(Variable b) {
        this.b = b;
    }

    @Override
    public void run() {

        while (true) {

            b.multiply();
        }
    }
}

You are hitting the top of the integers and wrapping around. 您正在达到整数的顶部并环绕。 Look before and after values of each operation: 查看每个操作的前后值:

addition pre: 0
addition post: 1
multiply pre: 1
multiply post: 2
addition pre: 2
addition post: 3
multiply pre: 3
multiply post: 6
addition pre: 6
addition post: 7
multiply pre: 7
multiply post: 14
addition pre: 14
addition post: 15
multiply pre: 15
multiply post: 30
addition pre: 30
addition post: 31
multiply pre: 31
multiply post: 62
addition pre: 62
addition post: 63
multiply pre: 63
multiply post: 126
addition pre: 126
addition post: 127
multiply pre: 127
multiply post: 254
addition pre: 254
addition post: 255
multiply pre: 255
multiply post: 510
addition pre: 510
addition post: 511
multiply pre: 511
multiply post: 1022
addition pre: 1022
addition post: 1023
multiply pre: 1023
multiply post: 2046
addition pre: 2046
addition post: 2047
multiply pre: 2047
multiply post: 4094
addition pre: 4094
addition post: 4095
multiply pre: 4095
multiply post: 8190
addition pre: 8190
addition post: 8191
multiply pre: 8191
multiply post: 16382
addition pre: 16382
addition post: 16383
multiply pre: 16383
multiply post: 32766
addition pre: 32766
addition post: 32767
multiply pre: 32767
multiply post: 65534
addition pre: 65534
addition post: 65535
multiply pre: 65535
multiply post: 131070
addition pre: 131070
addition post: 131071
multiply pre: 131071
multiply post: 262142
addition pre: 262142

... ...

multiply pre: 1073741823
multiply post: 2147483646
addition pre: 2147483646
addition post: 2147483647
multiply pre: 2147483647
multiply post: -2
addition pre: -2
addition post: -1
multiply pre: -1
multiply post: -2
addition pre: -2

The same thing will happen with a sleep, just much more slowly. 睡眠会发生同样的事情,只是速度要慢得多。 :) :)

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

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