From 020fcd9229cf29513632bf816b1faea171c35db3 Mon Sep 17 00:00:00 2001 From: musibs Date: Sat, 23 May 2020 15:23:26 +0530 Subject: [PATCH] BAEL 4064 Out of Memory Error --- .../error/oom/TestExecutorService.java | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 core-java-modules/core-java-lang/src/test/java/com/baeldung/error/oom/TestExecutorService.java diff --git a/core-java-modules/core-java-lang/src/test/java/com/baeldung/error/oom/TestExecutorService.java b/core-java-modules/core-java-lang/src/test/java/com/baeldung/error/oom/TestExecutorService.java new file mode 100644 index 0000000000..d51faa055d --- /dev/null +++ b/core-java-modules/core-java-lang/src/test/java/com/baeldung/error/oom/TestExecutorService.java @@ -0,0 +1,39 @@ +package com.baeldung.error.oom; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertThat; + +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.ThreadPoolExecutor; +import java.util.concurrent.TimeUnit; +import java.util.stream.IntStream; + +import org.junit.jupiter.api.Test; + +public class TestExecutorService { + + @Test + public void givenAnExecutorService_WhenMoreTasksSubmitted_ThenAdditionalTasksWait() { + + //Given + int noOfThreads = 5; + ExecutorService executorService = Executors.newFixedThreadPool(noOfThreads); + + Runnable runnableTask = () -> { + try { + TimeUnit.HOURS.sleep(1); + } catch (InterruptedException e) { + e.printStackTrace(); + } + }; + + //When + IntStream.rangeClosed(1, 10) + .forEach(i -> executorService.submit(runnableTask)); + + //Then + assertThat(((ThreadPoolExecutor )executorService).getQueue().size(), is(equalTo(5))); + } +}