等待线程在执行程序服务中完成

等待线程在执行程序服务中完成

本文介绍了等待线程在执行程序服务中完成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用N个线程初始化了一个exectuor服务。在N个线程完成之后,我想等待一段时间,然后使用N个线程的新实例重用执行程序。我该怎么做?

I have initialized a exectuor service with N threads. After the N threads finishes i want to wait for a while and then reuse the executor with new instance of N threads. How do i do this?

以下是我正在使用的示例代码失败:

Here is the sample code i am using that is failing:

       int NumberOfThreads=Integer.parseInt(PropertyHandler.getProperty("numberOfThreads"));

       ExecutorService executor = Executors.newFixedThreadPool(NumberOfThreads);
       log.info("Executor class has been initialized");
       while (true) {

        jobStack = MrMestri.buildJobs();
        log.info("Creating a job stack of the search urls");
        if (jobStack.isEmpty())
        {
           Thread.sleep(10000);
        }
        else {
           int jobToken = 0;
           while (jobStack.size() > 0) {
           jobToken++;
           MrRunnable worker = new MrRunnable(jobStack.pop());
           executor.execute(worker);
           if (jobToken% Integer.parseInt(PropertyHandler.getProperty("totalTrends")) == 0)       {
           log.info("All jobs for the clock cycle complete , waiting for next clock cycle to start. Number of jobs completed " + jobToken);
           executor.shutdown();
           Thread.sleep(milliseconds);

}

现在我正在使用执行程序关闭,没有执行程序执行我的线程。我的线程实现了runnable。

Now that i am using executor shutdown , there is no executor to execute my threads. And my threads implement runnable.

任何快速响应都会有很大的帮助。谢谢。

Any quick response would be of great help. Thank you.

推荐答案

问题在于下面一行放在内,而 loop。

The problem is with below line that is placed inside while loop.

jobStack = MrMestri.buildJobs();

在这种情况下,下面的条件将始终返回false,因为 jobStack 永远不会是空的

In this case below condition will always return false because jobStack is never empty if you want to process next N tasks

jobStack.isEmpty()

在内部中移动此条件,而循环和中断内部循环如果条件满足处理下N个任务。

Move this condition in inner while loop and break inner loop if condition meets to process next N tasks.

示例代码:

import java.util.Stack;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class Executor {

    /**
     * @param args
     * @throws InterruptedException
     */
    public static void main(String[] args) throws InterruptedException {
        int NumberOfThreads = Integer.parseInt("10");

        ExecutorService executor = Executors.newFixedThreadPool(NumberOfThreads);
        while (true) {

            Stack<Job> jobStack = MrMestri.buildJobs();
            int jobToken = 0;
            while (true) {
                if (jobStack.size() > 0) {
                    jobToken++;
                    MrRunnable worker = new MrRunnable(jobStack.pop());
                    executor.execute(worker);
                    if (jobToken % Integer.parseInt("4") == 0) {
                        // executor.shutdown();
                        System.out.println("short waiting...");
                        Thread.sleep(2000);

                    }
                } else {
                    System.out.println("long waiting...");
                    Thread.sleep(10000);
                    break;
                }
            }
        }
    }
}

class MrMestri {

    public static Stack<Job> buildJobs() {
        Stack<Job> stack = new Stack<Job>();
        stack.push(new Job("A"));
        stack.push(new Job("B"));
        stack.push(new Job("C"));
        stack.push(new Job("D"));
        stack.push(new Job("E"));
        stack.push(new Job("F"));
        return stack;
    }

}

class MrRunnable implements Runnable {
    private Job job;

    public MrRunnable(Job j) {
        job = j;
    }

    @Override
    public void run() {
        System.out.println(job.getName());
    }
}

class Job {
    private String name;

    public Job(String n) {
        name = n;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

}

这篇关于等待线程在执行程序服务中完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 06:52