1

有人可以解释这段代码中的竞争条件在哪里。我的讲师设置了它,我还不完全理解如何发现它们,或者说为什么会出现给出的结果。

public class SlowRace {

      public static void main(String args []) throws Exception {

          MyThread.count = 0 ;

          MyThread thread1 = new MyThread() ;
          thread1.name = "A" ;

          MyThread thread2 = new MyThread() ;
          thread2.name = "B" ;

          thread1.start() ;
          thread2.start() ;

          thread2.join() ;
          thread1.join() ;

          System.out.println("MyThread.count = " + MyThread.count) ;
      }
  }

  class MyThread extends Thread {

      volatile static int count ;

      String name ;

      public void run() {

          for(int i = 0 ; i < 10 ; i++) {
              delay() ;
              int x = count ;
              System.out.println("Thread " + name + " read " + x) ;
              delay() ;
              count = x + 1;
              System.out.println("Thread " + name + " wrote " + (x + 1)) ;
          }
      }

      static void delay() {

          int delay = (int) (1000000000 * Math.random()) ;
          for(int i = 0 ; i < delay ; i++) {}
      }
  } 

返回的结果:

Thread A read 0
Thread A wrote 1
Thread B read 0
Thread A read 1
Thread B wrote 1
Thread A wrote 2
Thread B read 2
Thread A read 2
Thread B wrote 3
Thread A wrote 3
Thread B read 3
Thread A read 3
Thread B wrote 4
Thread A wrote 4
Thread B read 4
Thread A read 4
Thread B wrote 5
Thread A wrote 5
Thread B read 5
Thread A read 5
Thread B wrote 6
Thread A wrote 6
Thread B read 6
Thread A read 6
Thread B wrote 7
Thread A wrote 7
Thread B read 7
Thread A read 7
Thread B wrote 8
Thread A wrote 8
Thread B read 8
Thread A read 8
Thread B wrote 9
Thread A wrote 9
Thread B read 9
Thread A read 9
Thread B wrote 10
Thread A wrote 10
Thread B read 10
Thread B wrote 11
MyThread.count = 11
4

3 回答 3

2

嘿伙计们,有人可以解释一下这段代码中的竞争条件,

比赛是在这两条线之间:

          int x = count ;
          ...
          count = x + 1;

一个线程获取该值,但另一个线程可以在第一个线程使用增量值更新它之前获取相同的值。那就是比赛。

  1. thread-1 获取 的值count并将其存储在x. x是(假设是 10)。
  2. 同时,thread-2 也获取 的值count并将其存储在x. x是(假设是 10)。
  3. thread-1 递增x为 be11并将其存储回count.
  4. 线程 2 增加其xto be的副本并将其11存储回count——这将覆盖线程 1 的增量。

因此count,其中一个增量将丢失,而不是 12,而是 11。

练习是指出增量不是原子的。真的delay()没有必要。 count++也会证明问题,因为它不是原子的(get/increment/set),并且线程可以在 3 个操作的中间被中断。

使此代码复杂的一件事System.out.println(...)是同步的,因此控制台输出将改变程序的时间。

于 2013-10-23T15:41:03.713 回答
0

您告诉编译器将信息存储在内存而不是缓存中。

volatile static int count ;

执行此操作的 2 个线程同时运行。

  public void run() {

      for(int i = 0 ; i < 10 ; i++) {
          delay() ;
          int x = count ;
          System.out.println("Thread " + name + " read " + x) ;
          delay() ;
          count = x + 1;
          System.out.println("Thread " + name + " wrote " + (x + 1)) ;
      }
  }

想象。

count = 0;
Thread1(int x = count); //x = 0;
Thread1(delay)
Thread2(int x = count); //x = 0;
Thread2(delay)
Thread1(count = x + 1); //count = 1;
Thread2(count = x + 1); //count = 1; //While it has to be 2.
于 2013-10-23T15:42:57.787 回答
0

此逻辑是线程不安全的,因为MyThread 类中静态变量计数的更新不受保护。
主线程将在启动后加入 Thread1 和 Thread2 并等待它们完成。但是,同时运行线程(线程 1,线程 2)时不走运的时间可能最终会更新变量“计数”和相同的值。

于 2013-10-23T15:44:20.463 回答