Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/339.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 等待完成的多线程处理_Java - Fatal编程技术网

Java 等待完成的多线程处理

Java 等待完成的多线程处理,java,Java,最近我玩了一点多线程,并尝试让多个线程在同一件事情上工作,但只在所有线程都完成后继续执行我的程序。我的代码太慢了(应该比实际速度快得多),我想知道是否有更高效、更快的方法来实现这一点: 我的跑步课。对于某个任务,需要有一个新的Runnable来扩展此Runnable并覆盖doTask() 这个类在有人调用start()和每个线程完成后“调用结束”(idk how to call This)时启动许多新线程,这些线程都在同一个任务上工作 public class ParallelCappedMul

最近我玩了一点多线程,并尝试让多个线程在同一件事情上工作,但只在所有线程都完成后继续执行我的程序。我的代码太慢了(应该比实际速度快得多),我想知道是否有更高效、更快的方法来实现这一点:

我的跑步课。对于某个任务,需要有一个新的Runnable来扩展此Runnable并覆盖doTask()

这个类在有人调用start()和每个线程完成后“调用结束”(idk how to call This)时启动许多新线程,这些线程都在同一个任务上工作

public class ParallelCappedMultithreader {

private AtomicInteger missingSignals = new AtomicInteger (0);
private final int MAX_THREADS;
private final Lock lock = new ReentrantLock();
private final Condition condition = lock.newCondition();
private final Class runnable, parentClass;
private final Object parentInstance;

public ParallelCappedMultithreader(Class<? extends ParallelCappedRunnable> runnable, int threads, Class parentClass, Object parentInstance) {
    this.runnable = runnable;
    this.parentClass = parentClass;
    this.MAX_THREADS = threads;
    this.parentInstance = parentInstance;
}
public ParallelCappedMultithreader(Class<? extends ParallelCappedRunnable> runnable, int threads) {
    this(runnable, threads, null, null);
}


public void start() throws InterruptedException{
    try {
        missingSignals.set(MAX_THREADS);
        Constructor constructor;
        if(parentClass == null) {
            constructor = runnable.getDeclaredConstructor(ParallelCappedMultithreader.class, Lock.class, Condition.class);
        }
        else {
            constructor = runnable.getDeclaredConstructor(parentClass, ParallelCappedMultithreader.class, Lock.class, Condition.class);
        }
        for(int i = 0; i < MAX_THREADS; i++) {
            if(parentClass == null)
                (new Thread((Runnable)constructor.newInstance(this, lock, condition))).start();
            else 
                (new Thread((Runnable)constructor.newInstance(parentInstance, this, lock, condition))).start();
        }
    } catch (Exception ex) {
        Logger.getLogger(ParallelCappedMultithreader.class.getName()).log(Level.SEVERE, null, ex);
        System.exit(0);
    }
    lock.lock();
    try {
        condition.await();
    } finally {
        lock.unlock();
    }
}

public int getMissingSignals() {
    return missingSignals.get();
}

public void sendSignal() {
    System.out.println(missingSignals.getAndDecrement());

}
}
公共类ParallelCappedMultithreader{
private AtomicInteger missingSignals=新的AtomicInteger(0);
私有最终int MAX_线程;
private final Lock=new ReentrantLock();
私有最终条件Condition=lock.newCondition();
私有最终类可运行,父类;
私有最终对象父实例;
public ParallelCappedMultithreader(类您尝试过thread.join()吗

基本上,您可以启动任意数量的线程,“父线程”在继续之前通过调用线程join()等待“子线程”死亡


正在进行的事情太多了(以及所有反射的原因)。可以使用
join
等待单个线程。如果要等待多个线程,可以执行以下操作:

MyRunnable(List threads) {
    lock (threads) {
        threads.add(this);
    }
}

void run() {
    doStuff()

    lock (threads) {
        threads.remove(this);

        if (threads.size() == 0)
            threads.notify();
    }
}
在manager类中:

void startAndWait(int numThreads) {
    List<MyRunnable> threads = new ArrayList<MyRunnable>();

    for (int i=0; i<numThreads; i++)
        new Thread(new MyRunnable(threads)).start();

    lock (threads) {
        while (threads.size() > 0)
            wait();
    }
}
void startAndWait(int numThreads){
List threads=new ArrayList();
对于(int i=0;i 0)
等待();
}
}

我认为您需要查看它,它看起来好像您正在尝试重新实现Thread.join()或CountDownLatch。阅读他们的javadoc。
void startAndWait(int numThreads) {
    List<MyRunnable> threads = new ArrayList<MyRunnable>();

    for (int i=0; i<numThreads; i++)
        new Thread(new MyRunnable(threads)).start();

    lock (threads) {
        while (threads.size() > 0)
            wait();
    }
}