Waiting for threads to complete in a executor service

后端 未结 2 1782
轮回少年
轮回少年 2021-01-28 04:47

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

2条回答
  •  没有蜡笔的小新
    2021-01-28 04:59

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

    jobStack = MrMestri.buildJobs();
    

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

    jobStack.isEmpty()
    

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


    Sample code:

    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 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 buildJobs() {
            Stack stack = new Stack();
            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;
        }
    
    }
    

提交回复
热议问题