如何模拟构造函数竞争条件? [英] How to simulate constructor race conditions?

查看:109
本文介绍了如何模拟构造函数竞争条件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



这说明如果一个线程有一个共享对象的引用,那么我将在第51页的Java Concurrency in practice其他线程可能能够在构造函数完成执行之前访问该对象。



我试图把它付诸实践,所以我写了这段代码思考,如果我跑了足够多的时间一个RuntimeException(世界是f * cked)会发生。但它不是做。



这是一个Java规范的情况,不保证什么,但是我的特定实现java保证它? (java版本:1.5.0在Ubuntu上)或者我有错误的东西在这本书吗?



代码:(我期待一个异常, >

  public class Threads {
private Widgit w;

public static void main(String [] s)throws Exception {
while(true){
Threads t = new Threads();
t.runThreads();
}
}

private void runThreads()throws Exception {
new Checker()。start();
w = new Widgit((int)(Math.random()* 100)+ 1);
}

private class Checker extends Thread {
private static final int LOOP_TIMES = 1000;

public void run(){
int count = 0;
for(int i = 0; i try {
w.checkMe();
count ++;
} catch(NullPointerException npe){
// ignore
}
}
System.out.println(checked:+ count +times of of + LOOP_TIMES);
}
}

私人静态类Widgit {
private int n;
private int n2;

Widgit(int n)throws InterruptedException {
this.n = n;
Thread.sleep(2);
this.n2 = n;
}

void checkMe(){
if(n!= n2){
throw new RuntimeException(World is f * cked);
}
}
}

}


解决方案

好吧,你需要更多地了解问题。这不是真正的任何东西的情况或不是保证。有并发问题,没有什么是真正保证,除非你真的做特定的事情,迫使问题发生。你只是依靠希望足够的运行应该产生,这不是这种情况。这些类型的问题很难预测,这就是为什么并发是一个难题。你可以尝试在你的函数中做更多的工作,但我保证这些是真正的问题,运行时不会救你。


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

This states that if a thread has references to a shared object then other threads may be able to access that object before the constructor has finished executing.

I have tried to put this into practice and so I wrote this code thinking that if I ran it enough times a RuntimeException("World is f*cked") would occur. But it isn't doing.

Is this a case of the Java spec not guaranting something but my particular implementation of java guaranteeing it for me? (java version: 1.5.0 on Ubuntu) Or have I misread something in the book?

Code: (I expect an exception but it is never thrown)

public class Threads {
 private Widgit w;

 public static void main(String[] s) throws Exception {
  while(true){
   Threads t = new Threads();
   t.runThreads();
  }
 }

 private void runThreads() throws Exception{
  new Checker().start();
  w = new Widgit((int)(Math.random() * 100)  + 1);
 }

 private class Checker extends Thread{
  private static final int LOOP_TIMES = 1000;

  public void run() {
   int count = 0;
   for(int i = 0; i < LOOP_TIMES; i++){
    try {
     w.checkMe();
     count++;
    } catch(NullPointerException npe){
     //ignore
    }
   }
   System.out.println("checked: "+count+" times out of "+LOOP_TIMES);
  }
 }

 private static class Widgit{
  private int n;
  private int n2;

  Widgit(int n) throws InterruptedException{
   this.n = n;
   Thread.sleep(2);
   this.n2 = n;
  }

  void checkMe(){
   if (n != n2) {
    throw new RuntimeException("World is f*cked");
   }
  }
 }

}

解决方案

Well, you need to understand the issues a little more. It isn't really a case of anything being or not being "guaranteed." With concurrency problems, nothing is really guaranteed unless you really do specific things to force the problem to happen. You're just relying on the hope that enough runs should produce, which is not the case. These kinds of problems are hard to predict, which is why concurrency is a hard problem. You could try doing more work in your functions, but I assure you these are real problems that the runtime is not going to save you from.

这篇关于如何模拟构造函数竞争条件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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