有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java并行运行100个线程,如果前面的一些线程完成,则运行缺少的线程

例如,我需要始终运行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) 个答案

  1. # 1 楼答案

    如果你的目标仅仅是让100个线程积极处理,我建议你看看Java thread pools(更一般地说是Executors

    我不清楚你是想让所有100个线程继续运行,还是等待它们全部完成。你的问题同时提到了这两个问题(ThreadsWorker产生了28个新线程,ThreadScont==0),它们似乎相互矛盾

  2. # 2 楼答案

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

    我会使用一个BlockingQueue,线程(你的可运行线程)在完成时添加到其中。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();
        }
      }
    }
    
  3. # 3 楼答案

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

    您可以尝试使用类CountDownLatchjdk 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();
    }
    

    从主类的attibutelatch获取线程数

  4. # 4 楼答案

    使用集合而不是数组。线程完成后,让它们从阵列中移除自己。比如:

    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()

  5. # 5 楼答案

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

    然后循环调用集合线程。在每个节点上加入()。当这个循环完成时,所有线程都完成了

    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)。但是,我将把它作为练习留给读者……:)