在Java中模拟线程故障

时间:2013-11-14 20:09:38

标签: java multithreading threadpool

我正在做一些事情,作为大学任务的一部分,所要求的部分是模拟线程故障。对于上下文,我正在使用Java SE中的执行程序服务

我已经环顾了一下SO和Google了,但是却没有找到任何具体或具体的东西来做这样的事情。

有没有人知道或有任何有关如何处理此事的信息或指导的良好来源?

2 个答案:

答案 0 :(得分:1)

如果要在遇到异常时测试线程“失败”的方式,可以实现Runnable,命令失败:

public class FailingRunnable implements Runnable {

    private volatile boolean doFail = false;

    @Override
    public void run() {
        while(!doFail && ! Thread.interrupted())
        {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
            }
        }
        throw new RuntimeException("failed");
    }

    public void failOnNextOccasion() {
        doFail = true;
    }

}

在将runnable添加到执行程序之后,必须保留对runnable的引用,然后在任何给定时间调用runnable的方法failOnNextOccasion()。像这样:

    ExecutorService execSrv = Executors.newFixedThreadPool(2);

    FailingRunnable one = new FailingRunnable();
    FailingRunnable two = new FailingRunnable();

    execSrv.submit(one);
    execSrv.submit(two);

    try {
        Thread.sleep(5000);
    } catch (InterruptedException e) {
        e.printStackTrace();
        Thread.currentThread().interrupt();
    }

    one.failOnNextOccasion();
    two.failOnNextOccasion();

答案 1 :(得分:0)

一个更复杂的线程失败了一个不那么明显的错误:

public class Test {

  static class FailerThread implements Runnable {

    final Object[] objects;
    final Random random;
    final int number;

    public FailerThread(final Object[] objects, final int number) {
      this.objects = objects;
      this.random = new Random();
      this.number = number;
    }

    @Override
    public void run() {
      final boolean isWriter = number % 2 == 0;
      int index = random.nextInt(objects.length);
      try {
        while (Thread.interrupted() == false) {
          synchronized (objects) {
            if (isWriter) {
              while (objects[index] == null) {
                System.out.println(number + ": Index " + index + " is null, waiting...");
                objects.wait();
              }
              for (int copyIndex = 0; copyIndex < objects.length; ++copyIndex) {
                if (objects[copyIndex] == null) {
                  objects[copyIndex] = this.objects[index];
                }
              }
              objects.notifyAll();
            } else {
              objects[index] = null;
            }
          }

          ++index;
          if (index >= objects.length) {
            index = 0;
          }
        }
      } catch (InterruptedException e) {
      }
    }
  }

  public static void main(String[] args) throws InterruptedException {
    final Object[] objects = new Object[10];
    for (int i = 0; i < objects.length; ++i) {
      objects[i] = new Object();
    }

    final int NUM_THREADS = 32;
    final ExecutorService executor = Executors.newFixedThreadPool(NUM_THREADS);
    for (int i = 0; i < NUM_THREADS; ++i) {
      executor.execute(new FailerThread(objects, i));
    }
  }
}

它应该立即失败,但其原因只是微不足道。