繁体   English   中英

java并发问题

[英]java concurrency issue

为什么n有时等于1或2

private static int n = 0;

private static Thread t1, t2;

private synchronized static void increment() {
    n++;
}

public static void main(String[] args) {
    t1 = new Thread(new Runnable() {
        public void run() {
            increment();
        }
    });

    t2 = new Thread(new Runnable() {
        public void run() {
            t1.start();
            increment();
        }
    });

    t2.start();

    try {
        t2.join();
    } catch(InterruptedException e) {
        e.printStackTrace();
    }

    System.out.println(n);
}

增量方法不应该只允许一个线程在任何给定时刻执行它吗?

也许它是调试器,似乎当我正常运行时我总是得到2但是当我调试代码时它有时会返回1。

确实如此,它可能以任何顺序发生。 你只需等待t2完成,但不能等待t1完成。

等待t1

private static int n = 0; 

private static Thread t1, t2;

private synchronized static void increment() {  // Lock will be on the "class" Object
    n++;
}

public static void main(String[] args) {
    t1 = new Thread(new Runnable() {
        public void run() {
            increment();
        }
    });

    t2 = new Thread(new Runnable() {
        public void run() {
            t1.start();           
// t1 starts after t2. Now, t1's increment might also be called or t2's increment() might also be called. If t2 calls increment(), then the join() method below (you are joining the in the main thread) will be completed and "n" will be printed (if t1 is not getting appropriate time of execution..)
            increment();
        }
    });

    t2.start();           // t2 starts first

    try {
        t2.join();
    } catch(InterruptedException e) {
        e.printStackTrace();
    }

    System.out.println(n); // increment() might not have been called by t1
}

无法保证一个线程会在另一个线程之前执行(即使具有同步条件..)。 所以,你可以join t1和t2。 这将确保您始终输出为2。

我想,如果increment中调用run的方法t2发生的前increment的方法t1在被调用t1run方法,然后n被锁定,由时间t2结束, t1可能仍在运行,但你在t1的增量结束之前,打印输出n增加t2

澄清:

  • t2开始了
  • t2产生t1
  • t2t1有机会之前调用increment
  • nt2increment持续时间内被锁定
  • Threadt1有机会increment之前加入t2
  • Threadt1有机会increment之前打印n

暂无
暂无

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

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