简体   繁体   English

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

[英]concurrency in java - how to test it?

I'm on Java concurrency at the moment. 我目前正在使用Java并发。

I don't know how to write negative scenario test. 我不知道怎么写负面情景测试。

I need a way to make deadlocks and I need a way to see that without using synchronization I could end up with problems like inconsistency. 我需要一种方法来制造死锁,我需要一种方法来看到,如果不使用同步,我可能会遇到像不一致这样的问题。

What is generally best way to write some stress test code that could show me bad results if synch is omitted? 通常最好的方法是编写一些压力测试代码,如果省略同步,可能会显示不良结果?

Any code example would be really appriciated. 任何代码示例都会非常适合。

Thank you all in advance! 谢谢大家!

The following code will almost certainly create a deadlock and demonstrates the classic deadlock scenario whereby two different threads acquire locks in an inconsistent order. 以下代码几乎肯定会创建一个死锁并演示经典的死锁场景,即两个不同的线程以不一致的顺序获取锁。

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));
  }
}

The following code demonstrates a situation likely to create inconsistent results due to lack of concurrency control between two threads. 以下代码演示了由于两个线程之间缺乏并发控制而可能会产生不一致结果的情况。

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;
    }
  }
}

In above deadlock example. 在上面的死锁示例中。 Period for deadlock is 3 second. 死锁时间为3秒。 After which lockA and lockB are released and occupied by Thread 2 and Thread 1 之后lockA和lockB被线程2和线程1释放并占用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM