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

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

问题描述

我正在阅读Java 并发实践"并查看第 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?

推荐答案

我会在多处理器机器上运行它几个小时,看看会发生什么(如果您使用 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天全站免登陆