并行运行100个线程,如果之前的某些线程已完成,则运行缺少的线程

时间:2011-04-01 02:28:03

标签: java multithreading parallel-processing

例如,我需要始终运行100个线程来执行某些操作。 我有一个名为ThreadsWorker的类,它查找线程计数并运行缺少的线程,如果之前的一些完成。 所以,这是描述情况的表格:

1 second: 100 threads
2 second: 92 threads (ThreadsWorker generates new 8 threads)
3 second: 100 theads
4 second: 72 threads (ThreadsWorker generates 28 threads)

等等。 我的线程是匿名调用(只是new Thread(new Runnable(...)).start()),因为我不知道如何正确地将它们保存到Threads[]数组,因为虽然ThreadsWorker将保存threads[i] = new Threads(),但某些线程可能会完成后会与数组索引发生冲突。

由于匿名调用,我现在使用threadsCount变量并在线程主体中开始增加它,并在线程主体结束时递减(使用synchronized)。好的,它可以正常工作,我的唯一方法是使用while()循环来检查进度完成后是否threadsCount == 0

我认为这是C风格但不是Java风格的:)所以,你能帮我用Java方式吗?

5 个答案:

答案 0 :(得分:6)

如果您的目标只是让100个线程正在积极处理,我建议您更频繁地查看Java thread pools(和Executors)。

我不清楚你是想要保留所有100个线程还是等待它们全部完成。你的问题引用了两个(ThreadsWorker产生28个新线程,threadsCount == 0),它们似乎是矛盾的。

答案 1 :(得分:5)

将所有线程放入数组或集合中。

然后循环遍历每个调用Thread.join()的集合。当这个循环完成时,所有线程都完成了。

ArrayList threads = new ArrayList();
for (int i = 0; i < 5; i++) {
  Thread t = new AweseomeThread();
  t.start();
  threads.add(t);
}

for (Thread t : threads) {
  t.join();
}

您也需要一些异常处理(例如InterruptedException)。但是,我会把它作为读者的练习......:)

答案 2 :(得分:3)

http://download.oracle.com/javase/6/docs/api/java/util/concurrent/CountDownLatch.html

您可以尝试上课CountDownLatch jdk api

private CountDownLatch latch;
private static class SimpleThread extends Thread {
 public void run() {
  latch.countDown();
 }
}
public static void main(String[] args) {
 int threadcount = 10;
 latch = new CountDownLatch(threadcount);
 for (int i = 0; i < 10; i++) {
  Thread t = new SimpleThread();
  t.start();
 }
 // waiting threads all finished
 latch.await();
}

从Main类

的attibute latch获取线程计数

答案 3 :(得分:2)

我相信您正在尝试让ThreadWorker为已经完成的所有线程提交新线程。

我使用了一个BlockingQueue,线程(你的Runnable(s))在完成后添加。 ThreadWorker将等待一个线程完成,然后将启动一个新线程。

public class YourRunnable implements Runnable {
  private final BlockingQueue<YourRunnable> queue;
  public YourRunnable(BlockingQueue<YourRunnable> queue){
    this.queue = queue;
  }
  public void run{
      // Your Code...
      // Finished Processing
      queue.add(this);
  }
}
public class ThreadWorkder implements Runnable { 
  private final BlockingQueue<YourRunnable> queue;
  ThreadWorker(BlockingQueue<YourRunnable> queue){
    this.queue = queue;
  }
  public void run{
    while(queue.take()){
       (new Thread(new YourRunnable(queue))).start();
    }
  }
  // general main method
  public static void main(String [] args){
    BlockingQueue<YourRunnable> queue = new LinkedBlockingQueue<YourRunnable>();
    ThreadWorker worker = new ThreadWorker(queue);
    Thread(worker).start();
    for (int i = 0; i < 100; i++){
      (new Thread(new YourRunnable(queue))).start();
    }
  }
}

答案 4 :(得分:1)

使用集合而不是数组。线程完成后,让它们从数组中删除。像这样:

public class Foo {
  Vector<Thread> threads = new Vector<Thread>(); //Vector is threadsafe

  public ensureThreadCount(int count) {
    while (threads.size() < count) {
      Thread t = new AweseomeThread(threads);
      threads.add(t);
      t.start();
    }
  }
}

public class AwesomeThread {
  Collection threads;
  public AwesomeThread(Collection threads) {
    this.threads = threads;
  }

  public void run() {
    try {
      // do stuff
    } catch (Throwable t) {
    } finally {
      threads.remove(this);
    }
  }
}

然后,让你的工作人员调用Foo.ensureThreadCount()。