如何惰性地创建Java线程池使用的任务

如何惰性地创建Java线程池使用的任务,java,multithreading,lazy-loading,threadpool,java.util.concurrent,Java,Multithreading,Lazy Loading,Threadpool,Java.util.concurrent,我正在用Java编写一个负载测试应用程序,它有一个线程池,可以对测试中的服务器执行任务。因此,为了制作1000个作业并在5个线程中运行它们,我做了如下操作: ExecutorService pool = Executors.newFixedThreadPool(5); List<Runnable> jobs = makeJobs(1000); for(Runnable job : jobs){ pool.execute(job); }

我正在用Java编写一个负载测试应用程序,它有一个线程池,可以对测试中的服务器执行任务。因此,为了制作1000个作业并在5个线程中运行它们,我做了如下操作:

    ExecutorService pool = Executors.newFixedThreadPool(5);
    List<Runnable> jobs = makeJobs(1000);
    for(Runnable job : jobs){
        pool.execute(job);
    }
ExecutorService池=Executors.newFixedThreadPool(5);
列出作业=生成作业(1000);
for(可运行作业:作业){
pool.execute(作业);
}
然而,我认为这种方法不能很好地扩展,因为我必须提前制作所有“作业”对象,并将它们保存在内存中,直到需要它们为止

我正在寻找一种方法,让池中的线程在每次需要新作业时都转到某种“JobFactory”类,并让工厂根据请求构建可运行程序,直到运行了所需数量的作业。工厂可能会开始返回“null”以向线程发出信号,表示没有更多的工作要做

我可以手工编写类似的代码,但它似乎是一个足够常见的用例,我想知道在奇妙但复杂的“java.util.concurrent”包中是否有什么可以替代的东西?

Hrm。您可以创建具有固定容量的,并让每个工作线程将a
Runnable
出列并运行它。然后您可以有一个生产者线程,它将作业放入队列

主线程将执行以下操作:

// 100 is the capacity of the queue before blocking
BlockingQueue<Runnable> queue = new LinkedBlockingQueue<Runnable>(100);
// start the submitter thread
new Thread(new JobSubmitterThread(queue)).start();
// make in a loop or something?
new Thread(new WorkerThread(queue)).start();
new Thread(new WorkerThread(queue)).start();
...
public class WorkerThread implements Runnable {
     private final BlockingQueue<Runnable> queue;
     public WorkerThread(BlockingQueue<Runnable> queue) {
         this.queue = queue;
     }
     public void run() {
         // run until the main thread shuts it down using volatile boolean or ...
         while (!shutdown) {
             Runnable job = queue.take();
             job.run();
         }
     }
}
 public class JobSubmitterThread implements Runnable {
     private final BlockingQueue<Runnable> queue;
     public WorkerThread(BlockingQueue<Runnable> queue) {
         this.queue = queue;
     }
     public void run() {
         for (int jobC = 0; jobC < 1000; jobC++) {
             Runnable job = makeJob();
             // this would block when the queue reaches capacity
             queue.put(job);
         }
     }
 }
//100是阻塞前队列的容量
BlockingQueue=新的LinkedBlockingQueue(100);
//启动提交者线程
新线程(新作业提交器线程(队列)).start();
//做一个循环什么的?
新线程(新工作线程(队列)).start();
新线程(新工作线程(队列)).start();
...
工人看起来像:

// 100 is the capacity of the queue before blocking
BlockingQueue<Runnable> queue = new LinkedBlockingQueue<Runnable>(100);
// start the submitter thread
new Thread(new JobSubmitterThread(queue)).start();
// make in a loop or something?
new Thread(new WorkerThread(queue)).start();
new Thread(new WorkerThread(queue)).start();
...
public class WorkerThread implements Runnable {
     private final BlockingQueue<Runnable> queue;
     public WorkerThread(BlockingQueue<Runnable> queue) {
         this.queue = queue;
     }
     public void run() {
         // run until the main thread shuts it down using volatile boolean or ...
         while (!shutdown) {
             Runnable job = queue.take();
             job.run();
         }
     }
}
 public class JobSubmitterThread implements Runnable {
     private final BlockingQueue<Runnable> queue;
     public WorkerThread(BlockingQueue<Runnable> queue) {
         this.queue = queue;
     }
     public void run() {
         for (int jobC = 0; jobC < 1000; jobC++) {
             Runnable job = makeJob();
             // this would block when the queue reaches capacity
             queue.put(job);
         }
     }
 }
公共类WorkerThread实现可运行{
私有最终阻塞队列;
公共WorkerThread(阻塞队列){
this.queue=队列;
}
公开募捐{
//运行直到主线程使用volatile boolean或。。。
而(!关机){
Runnable job=queue.take();
job.run();
}
}
}
提交工作的人看起来像:

// 100 is the capacity of the queue before blocking
BlockingQueue<Runnable> queue = new LinkedBlockingQueue<Runnable>(100);
// start the submitter thread
new Thread(new JobSubmitterThread(queue)).start();
// make in a loop or something?
new Thread(new WorkerThread(queue)).start();
new Thread(new WorkerThread(queue)).start();
...
public class WorkerThread implements Runnable {
     private final BlockingQueue<Runnable> queue;
     public WorkerThread(BlockingQueue<Runnable> queue) {
         this.queue = queue;
     }
     public void run() {
         // run until the main thread shuts it down using volatile boolean or ...
         while (!shutdown) {
             Runnable job = queue.take();
             job.run();
         }
     }
}
 public class JobSubmitterThread implements Runnable {
     private final BlockingQueue<Runnable> queue;
     public WorkerThread(BlockingQueue<Runnable> queue) {
         this.queue = queue;
     }
     public void run() {
         for (int jobC = 0; jobC < 1000; jobC++) {
             Runnable job = makeJob();
             // this would block when the queue reaches capacity
             queue.put(job);
         }
     }
 }
公共类JobSubmitterThread实现可运行{
私有最终阻塞队列;
公共WorkerThread(阻塞队列){
this.queue=队列;
}
公开募捐{
对于(int-jobC=0;jobC<1000;jobC++){
Runnable job=makeJob();
//这将在队列达到容量时阻塞
队列.放置(作业);
}
}
}

您可以使用AtomicInteger在线程池的执行线程中执行所有工作,以监视执行的可运行程序的数量

 int numberOfParties = 5;
 AtomicInteger numberOfJobsToExecute = new AtomicInteger(1000);
 ExecutorService pool = Executors.newFixedThreadPool(numberOfParties );
 for(int i =0; i < numberOfParties; i++){
     pool.submit(new Runnable(){
        public void run(){
            while(numberOfJobsToExecute.decrementAndGet() >= 0){
                makeJobs(1).get(0).run();
            }
        }
     });
 }
int numberOfParties=5;
AtomicInteger numberOfJobsToExecute=新的AtomicInteger(1000);
ExecutorService pool=Executors.newFixedThreadPool(多方数);
对于(int i=0;i=0){
makeJobs(1.get(0.run));
}
}
});
}

您还可以将返回的未来存储在列表中,并
get()
在其上等待完成(在其他机制中)

Good answer@John+1.当然比我的答案简单。我认为您希望>=0,否则它将执行999个作业,因为减量首先发生<代码>makeJobs显然必须是可重入的。