1

I have a java program deployed in Weblogic server which is executed using Quartz scheduler. The program executes in every 10 seconds. In the java code I have created two threads using ExcutorService and I have called service.shutdown() at the end. But every time the quartz scheduler runs the program it creates a new pool of threads by incrementing the pool id like "pool-109-thread-1" and "pool-109-thread-2" then pool-110-thread-1" and "pool-110-thread-2". So this pool count is increasing. Is it ok or do I need to change something in my code ? Sample Code below:`

public void post(){
ExecutorService service = Executors.newFixedThreadPool(2);
    for (String filePath : strArray) {
service.submit(new PostImages(postURL,filePath)); 
}
service.shutDown();
}

`

Xerxis
  • 191
  • 1
  • 9
  • 3
    you shouldn't be initializing `ExecutorService` everytime. – sidgate Jan 15 '16 at 12:04
  • Ok but what happens to the old thread pool in my code ? And why is the pool count increasing for every execution of the post() method ? Is service.shutdown() not working ? – Xerxis Jan 15 '16 at 12:18
  • Hi Xersis : When do you call post() method? Move this statement out of post() method. ExecutorService service = Executors.newFixedThreadPool(2); Call shutDown() method in some other place. – Ravindra babu Mar 15 '17 at 15:45

3 Answers3

2

Every time you call the Executors.newFixedThreadPool(2) a new thread pool of 2 threads is created. It will be a problem if running too much times because the number of process of your OS will crash.

You must to convert your local variable to a static variable and to have only 1 instance of this thread pool, keeping only 2 threads to execute the jobs.

1

I had the same problem, this is what you want to use

If you are interested in knowing when a certain task completes, or a certain batch of tasks, you may use ExecutorService.submit(Runnable). Invoking this method returns a Future object which may be placed into a Collection which your main thread will then iterate over calling Future.get() for each one. This will cause your main thread to halt execution until the ExecutorService has processed all of the Runnable tasks.

Collection<Future<?>> futures = new LinkedList<Future<?>>();
futures.add(executorService.submit(myRunnable));
for (Future<?> future:futures) {
    future.get();
}

How to wait for all tasks in an ThreadPoolExecutor to finish without shutting down the Executor?

Opsse
  • 1,851
  • 2
  • 22
  • 38
0

I think it's ok. It's probably incrementing the pool ID because old ones are still waiting for the garbage collector.

Leo
  • 751
  • 4
  • 29
  • Hi Leo.. For test I redeployed the war in weblogic environment and surprisingly the pool ID again got incremented. Earlier it was 'pool-309-thread-1' and after redeployment it started from 'pool-310-thread-1'. Is it still ok ? or is there seriously something wrong ? Please help !! – Xerxis Jan 15 '16 at 16:10
  • @Xerxis did you find the reason behind this? I am facing a similar situation where threadpool count is increasing everytime. – Sandeepan Nath Aug 31 '19 at 20:20