java中的并发性-如何测试它?

java中的并发性-如何测试它?,java,Java,我现在正在研究Java并发性 我不知道如何编写负面情景测试 我需要一种方法来制造死锁,我需要一种不用同步就能看到死锁的方法 我最终可能会遇到矛盾等问题 编写压力测试代码的最佳方法通常是什么 如果省略同步,会显示不好的结果吗 任何代码示例都将非常有用 提前谢谢大家 下面的代码几乎肯定会创建死锁,并演示了经典的死锁场景,其中两个不同的线程以不一致的顺序获取锁 public class Main { private final Object lockA = new Object(); priv

我现在正在研究Java并发性

我不知道如何编写负面情景测试

我需要一种方法来制造死锁,我需要一种不用同步就能看到死锁的方法 我最终可能会遇到矛盾等问题

编写压力测试代码的最佳方法通常是什么 如果省略同步,会显示不好的结果吗

任何代码示例都将非常有用


提前谢谢大家

下面的代码几乎肯定会创建死锁,并演示了经典的死锁场景,其中两个不同的线程以不一致的顺序获取锁

public class Main {
  private final Object lockA = new Object();
  private final Object lockB = new Object();

  public static void main(String[] args) {
    new Main();
  }

  public Main() {
    new Thread(new Runnable() {
      public void run() {
        a();
        sleep(3000L); // Add a delay here to increase chance of deadlock.
        b();
      }
    }, "Thread-A").start();

    new Thread(new Runnable() {
      public void run() {
        // Note: Second thread acquires locks in the reverse order of the first!
        b();
        sleep(3000L); // Add a delay here to increase chance of deadlock.
        a();
      }
    }, "Thread-A").start();
  }

  private void a() {
    log("Trying to acquire lock A.");

    synchronized(lockA) {
      log("Acquired lock A.");
    }
  }

  private void b() {
    log("Trying to acquire lock B.");

    synchronized(lockB) {
      log("Acquired lock B.");
    }
  }

  private void sleep(long millis) {
    try {
      Thread.sleep(millis);
    } catch(InterruptedException ex) {
    }
  }

  private void log(String msg) {
    System.err.println(String.format("Thread: %s, Message: %s",
      Thread.currentThread().getName(), msg));
  }
}
下面的代码演示了由于两个线程之间缺乏并发控制而可能产生不一致结果的情况

public class Main {
  // Non-volatile integer "result".
  private int i;

  public static void main(String[] args) {
    new Main();
  } 

  public Main() {
    Thread t1 = new Thread(new Runnable() {
      public void run() {
        countUp();
      }
    }, "Thread-1");

    Thread t2 = new Thread(new Runnable() {
      public void run() {
        countDown();
      }
    }, "Thread-2");

    t1.start();
    t2.start();

    // Wait for two threads to complete.
    t1.join();
    t2.join();

    // Print out result.  With correct concurrency control we expect the result to
    // be 0.  A non-zero result indicates incorrect use of concurrency.  Also note
    // that the result may vary between runs because of this.
    System.err.println("i: " + i);
  }

  private void countUp() {
    // Increment instance variable i 1000,000 times.  The variable is not marked
    // as volatile, nor is it accessed within a synchronized block and hence
    // there is no guarantee that the value of i will be reconciled back to main
    // memory following the increment.
    for (int j=0; j<1000000; ++j) {
      ++i;
    }
  }

  private void countDown() {
    // Decrement instance variable i 1000,000 times.  Same consistency problems
    // as mentioned above.
    for (int j=0; j<1000000; ++j) {
      --i;
    }
  }
}
公共类主{
//非易失性整数“结果”。
私人互联网i;
公共静态void main(字符串[]args){
新的Main();
} 
公用干管(){
线程t1=新线程(新的可运行线程(){
公开募捐{
倒计时();
}
}“螺纹-1”);
线程t2=新线程(新可运行(){
公开募捐{
倒计时();
}
}“螺纹-2”);
t1.start();
t2.start();
//等待两个线程完成。
t1.join();
t2.连接();
//打印结果。通过正确的并发控制,我们希望结果
//为0。非零结果表示不正确地使用了并发。另请注意
//因此,运行之间的结果可能会有所不同。
System.err.println(“i:+i”);
}
私有无效计数(){
//增量实例变量i 1000000次。未标记该变量
//作为易失性,它也不能在同步块内访问,因此
//无法保证i的值将与main进行对账
//内存在增量之后。

对于(intj=0;j,在上面的死锁示例中。死锁的时间是3秒。在这之后,锁A和锁B被线程2和线程1释放和占用

这里类似的问题:事先考虑(写在纸上)所有的可能性,而不是测试它们。您可能还会发现Nactest()框架很方便。Add.start()在线程创建和抛出主方法delcarations:)和--j;在倒计时中必须是--i;(对不起,我只是为了好玩才这么做)@Helios:谢谢-做了这些更正。你必须快速键入才能得到分数!伙计们,非常感谢你们在我完成测试后发布的提示和代码,我将在这里回复!