-1

I have the code sample:

public class ThreadPoolTest {
    public static void main(String[] args) throws InterruptedException {
        for (int i = 0; i < 100; i++) {
            if (test() != 5 * 100) {
                throw new RuntimeException("main");
            }
        }
        test();

    }

    private static long test() throws InterruptedException {
        ExecutorService executorService = Executors.newFixedThreadPool(100);
        CountDownLatch countDownLatch = new CountDownLatch(100 * 5);
        Set<Thread> threads = Collections.synchronizedSet(new HashSet<>());
        AtomicLong atomicLong = new AtomicLong();
        for (int i = 0; i < 5 * 100; i++) {
            Thread.sleep(100);
            executorService.submit(new Runnable() {
                @Override
                public void run() {
                    try {
                        threads.add(Thread.currentThread());
                        atomicLong.incrementAndGet();
                        countDownLatch.countDown();
                        Thread.sleep(1000);
                    } catch (Exception e) {
                        System.out.println(e);
                    }


                }
            });
        }
        executorService.shutdown();
        countDownLatch.await();
        if (threads.size() != 100) {
            throw new RuntimeException("test");
        }
        return atomicLong.get();
    }
}

I especially made application to work long.

And I see jvisualVM.
Each time gap threadpool was recreated.

After several minutes I see:

enter image description here

but if I use newCachedThreadPool instead of newFixedThreadPool I see constant picture:

enter image description here

Can you explain this behaviour?

P.S.

Problem was that exception occures in code and second iteration was not started

gstackoverflow
  • 36,709
  • 117
  • 359
  • 710

3 Answers3

2

To answer your question; just look here:

private static long test() throws InterruptedException {
  ExecutorService executorService = Executors.newFixedThreadPool(100);

The JVM creates a new ThreadPool during each run of test(), because you tell it to do so.

In other words: if you intend to re-use the same threadpool, then avoid creating/shutting down your instances all the time.

In that sense, the simple fix is: move the creation of that ExecutorService into your main() method; and pass the service as argument to your test() method.

Edit: regarding your last comment on cached vs. fixed threadpool; you probably want to look into this question.

Community
  • 1
  • 1
GhostCat
  • 137,827
  • 25
  • 176
  • 248
0

Because you asked it to, in your code ? :)

Try moving the Pool creation code outside the test.

PankajT
  • 145
  • 3
0

From docs:

newFixedThreadPool

Creates a thread pool that reuses a fixed number of threads operating off a shared unbounded queue. At any point, at most nThreads threads will be active processing tasks. If additional tasks are submitted when all threads are active, they will wait in the queue until a thread is available. If any thread terminates due to a failure during execution prior to shutdown, a new one will take its place if needed to execute subsequent tasks. The threads in the pool will exist until it is explicitly shutdown.

newCachedThreadPool

Creates a thread pool that creates new threads as needed, but will reuse previously constructed threads when they are available. These pools will typically improve the performance of programs that execute many short-lived asynchronous tasks. Calls to execute will reuse previously constructed threads if available. If no existing thread is available, a new thread will be created and added to the pool. Threads that have not been used for sixty seconds are terminated and removed from the cache. Thus, a pool that remains idle for long enough will not consume any resources. Note that pools with similar properties but different details (for example, timeout parameters) may be created using ThreadPoolExecutor constructors.

Divers
  • 9,531
  • 7
  • 45
  • 88