-1

作为大学作业的一部分,我正在做一些事情,其中​​一部分被问到是线程故障的模拟。对于上下文,我正在使用 Java SE 的执行程序服务

我环顾了一下 SO 和 Google,但找不到任何具体或具体的内容。

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

4

2 回答 2

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 添加到 executor 之后,您必须保留对 runnable 的引用,然后在任何给定时间调用failOnNextOccasion()runnable 的方法。像这样:

    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();
于 2013-11-14T20:22:18.190 回答
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));
    }
  }
}

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

于 2013-11-14T21:35:40.320 回答