0

我有固定数量的线程。我希望每个线程Runnable一个接一个地运行三个。这里有一些伪代码来解释:

Thread[] threads = new Thread[4];

for (int i = 0; i < threads.length; i++) {
    // Set the first tasks.
    threads[i] = new Thread(new FirstRunnable());
    threads[i].start();
}

for (int i = 0; i < threads.length; i++)
    threads[i].join(); // wait until the first tasks are done

for (int i = 0; i < threads.length; i++) {
    // Set the second task.
    threads[i].setRunnable(new SecondRunnable());
    threads[i].start();
}

for (int i = 0; i < threads.length; i++)
    threads[i].join(); // wait until the second tasks are done

...

使用ThreadPool声音的方式过大了,特别是因为我正在追求性能,性能,性能。在 Java 中实现这一点的最佳方法是什么?

4

5 回答 5

4

每当你看到new Thread(...).start(),利用Executors框架。特别是,利用Executors.newFixedThreadPool(...).

于 2011-06-30T11:12:33.917 回答
1

似乎对Executors类中的 newFixedThreadPool 很有用。

所以你的代码看起来像:

ExecutorService es = Executors.newFixedThreadPool(4);
List<Future> futures = new ArrayList<Future>();
for (int x = 0; x < 4; x ++) {
    futures.add(es.submit(new FirstRunnable()));
}
while (futures.size() > 0) {
   futures.remove(0).get();
}
for (int x = 0; x < 4; x ++) {
    futures.add(es.submit(new SecondRunnable()));
}

while (futures.size() > 0) {
   futures.remove(0).get();
}

当然,您可以轻松地重构上面的代码以消除代码重复。

于 2011-06-30T11:17:29.627 回答
1

您可以使用CyclicBarrier和“CombinedRunnable”,如下所示。屏障允许线程在继续下一个可运行之前相互等待完成。

CyclicBarrier barrier = new CyclicBarrier(4);
Runnable r = new CombinedRunnable(barrier, new FirstRunnable(), new SecondRunnable());
Thread[] threads = new Thread[4];
for (int i = 0; i < threads.length; i++) {
    threads[i] = new Thread(r);
    threads[i].start();
}

CombinedRunnable 类:

public class CombinedRunnable implements Runnable{

    private final CyclicBarrier barrier;
    private final Runnable[] runnables;

    public CombinedRunnable(CyclicBarrier barrier, Runnable... runnables){
        this.barrier = barrier;
        this.runnables = runnables;
    }

    /* (non-Javadoc)
     * @see java.lang.Runnable#run()
     */
    @Override
    public void run() {
        for(Runnable r: runnables){
            r.run();
            try {
                barrier.await();
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (BrokenBarrierException e) {
                e.printStackTrace();
            }
        }
    }
}
于 2011-06-30T11:14:27.307 回答
0

实现此目的的惯用方法是将Executora 与 a 结合使用CompletionService。这允许您将许多工作单元映射到固定大小的线程池,并且还提供了一种优雅的阻塞机制,直到所有工作完成。

请注意,您对使用线程池可能会如何影响效率的担忧并不是真正的问题:主要开销是创建单个线程,无论如何您都在这样做;创建池的额外对象创建开销可以忽略不计。

// Create fixed thread pool and wrap in a CompletionService to allow for easy access to completed tasks.
// We don't have an explicit result for each Runnable so parameterise the service on Void.
CompletionService<Void> cs = new ExecutorCompletionService<Void>(Executors.newFixedThreadPool(3));

// Create units of work for submission to completion service.
Runnable[] runnables = ...

// Submit runnables.  Note that we don't care about the result so pass in null.
for (Runnable r : runnables) {
  cs.submit(r, null);
}

// Take each *completed* result in turn, blocking until a completed result becomes available.
for (int i=0; i<runnables.length; ++i) {
  Future<Void> completed = cs.take();
}
于 2011-06-30T11:47:28.640 回答
-2

Executor Framework 只适合你。
伪代码如下:
1. 创建执行器服务

Executors type1Runnables = Executors.newFixedThreadPool(4);
Executors type2Runnables = Executors.newFixedThreadPool(4);

等等。
2. 向它提交任务

for(){
type1Runnables.submit(new Runnable1());
type2Runnables.submit(new Runnable2);
}

3. 调用执行者

type1Runnables.invokeAll();
type2Runnables.invokeAll();

为了使其更通用,您也许可以编写自己的 executorservicefactory 接受不同的可运行类型。

于 2011-06-30T11:17:25.130 回答