如何显示未正确发布的值周围的竞争条件? [英] How to demonstrate race conditions around values that aren't published properly?

查看:108
本文介绍了如何显示未正确发布的值周围的竞争条件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在阅读Java Concurrency in practice,并参考第51页的示例代码。

I am reading "Java Concurrency in practice" and looking at the example code on page 51.

根据这本书,这段代码有风险如果尚未正确发布,则失败。因为我喜欢编写代码示例,并打破他们来证明他们如何工作。我试图让它抛出一个AssertionError但失败。 (将我导向我的上一个问题

According to the book this piece of code is at risk of of failure if it has not been published properly. Because I like to code examples and break them to prove how they work. I have tried to make it throw an AssertionError but have failed. (Leading me to my previous question)

任何人都可以发布示例代码,以便抛出AssertionError?规则:不要修改Holder类。

Can anyone post sample code so that an AssertionError is thrown? Rule: Do not modify the Holder class.

public class Holder{
    private int n;

    public Holder(int n){
        this.n = n;
    }

    public void assertSanity(){
        if (n != n) {
            throw new AssertionError("This statement is false");
        }
    }
}

使它更脆弱,但我仍然不能得到一个AssertionError抛出。

I have modified the class to make it more fragile but I still can not get an AssertionError thrown.

class Holder2 {
    private int n;
    private int n2;

    public Holder2(int n) throws InterruptedException{
        this.n = n;
        Thread.sleep(200);
        this.n2 = n;
    }

    public void assertSanity(){
        if (n != n2) {
            throw new AssertionError("This statement is false");
        }
    }
}

的上述类抛出一个AssertionError?

Is it possible to make either of the above classes throw an AssertionError? Or do we have to accept that they may occasionally do so and we can't write code to prove it?

推荐答案

我可以通过这样的方式, d在多处理器机器上运行几个小时,看看会发生什么(如果使用Holder2,请删除睡眠)。这样的种族条件可能是罕见的,或者不存在于你的特定机器上 - 但至少尝试通过尝试数百万次来激发这一百万案件。

I'd run this on a multiprocessor machine for a few hours and see what happens(remove the sleep if you use your Holder2). Such race conditions might be rare, or not existant on your particular machine - but atleast try to provoke these one on a million cases , by trying millions of times.

class Checker {
  private Holder h;
  public Checker() {
   h = new Holder(42);
  }

  public void check() {
    h.assertSanity();
  }

  public void create(int n) {
   h = new Holder(n);
   }

}

public class MyThread extends thread{
  private bool check;
  private final Checker c;
  public MyThread(bool check,Checker c) {
    this.check = check;
    this.c = c;
  }
    public static void main(String[] args) {
      Checker c = new Checker();
      MyThread t1 = new MyThread(false,c);  
      MyThread t2 = new MyThread(true,c);
      t1.start();
      t2.start();
      t1.join();
      t2.join();
   }
   public void run() {
     int n = 0;
     while(true) {
       if(check) 
         c.check();
       else
         c.create(n++);
    }
   }
 }
}

这篇关于如何显示未正确发布的值周围的竞争条件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆