我有工作接口IJob这样的:在Java中运行多个线程,错误管理
public interface IJob {
public void execute();
}
在我的应用程序有多个类实现了这个接口,像IJob1和IJob2:
public class IJob1 implements IJob{
@Override
public void execute(){
System.out.println("IJob1\n");
}
}
public class IJob2 implements IJob{
@Override
public void execute(){
System.out.println("IJob2\n");
}
}
因为要运行的作业量稳步增加,所以我想创建一个新的作业,它将IJob实例列表并行运行。新实现用于并行运行作业的线程数量应该是可配置的。如果其中一个作业引发异常,则其他所有当前正在运行的作业也应该停止,并且execute()方法应将异常传递给调用者。
我写了这个,但我不能够运行作业和检查,如果有一个错误:
import java.util.LinkedList;
public class WorkQueue
{
private final int nThreads;
private final IJob[] threads;
private final LinkedList queue;
public WorkQueue(int nThreads)
{
this.nThreads = nThreads;
queue = new LinkedList();
threads = new IJob[nThreads];
for (int i=0; i<nThreads; i++) {
threads[i] = new IJob();
threads[i].execute();
}
}
public void execute(Runnable r) {
synchronized(queue) {
queue.addLast(r);
queue.notify();
}
}
private class PoolWorker extends Thread {
public void run() {
Runnable r;
while (true) {
synchronized(queue) {
while (queue.isEmpty()) {
try
{
queue.wait();
}
catch (InterruptedException ignored)
{
}
}
r = (Runnable) queue.removeFirst();
}
// If we don't catch RuntimeException,
// the pool could leak threads
try {
r.run();
}
catch (RuntimeException e) {
// You might want to log something here
}
}
}
}
}
能否请您给我一些帮助很少去? 非常感谢。
看看'ThreadPoolExecutor'。你的工作将是这方面的任务。 – Thomas
它们不是线程,所以它们按顺序执行......创建一个需要扩展'Thread'类的线程 – Marcx
感谢您的帮助。我会去看看这个背景 – sharkbait