[JAVA-13962] Align code with article + clean up (#13012)

* [JAVA-13962] Align code with article + clean up

* [JAVA-13962] Clean up

Co-authored-by: panagiotiskakos <panagiotis.kakos@libra-is.com>
This commit is contained in:
panos-kakos 2022-11-24 19:23:09 +00:00 committed by GitHub
parent 4193cc9a56
commit fd1bf7a029
5 changed files with 50 additions and 65 deletions

View File

@ -1,14 +1,15 @@
package com.baeldung.concurrent.executorservice;
import java.util.concurrent.Callable;
import java.util.concurrent.CountDownLatch;
public class DelayedCallable implements Callable<String> {
private String name;
private long period;
private CountDownLatch latch;
public DelayedCallable(String name, long period, CountDownLatch latch) {
this(name, period);
this.latch = latch;
@ -23,11 +24,11 @@ public class DelayedCallable implements Callable<String> {
try {
Thread.sleep(period);
if (latch != null) {
latch.countDown();
}
} catch (InterruptedException ex) {
// handle exception
ex.printStackTrace();
@ -36,4 +37,4 @@ public class DelayedCallable implements Callable<String> {
return name;
}
}
}

View File

@ -1,15 +1,24 @@
package com.baeldung.concurrent.executorservice;
import static junit.framework.TestCase.assertTrue;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.CompletionService;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorCompletionService;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.RejectedExecutionException;
import java.util.concurrent.TimeUnit;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.*;
import static junit.framework.TestCase.assertTrue;
public class WaitingForThreadsToFinishManualTest {
private static final Logger LOG = LoggerFactory.getLogger(WaitingForThreadsToFinishManualTest.class);
@ -26,18 +35,18 @@ public class WaitingForThreadsToFinishManualTest {
Thread.currentThread().interrupt();
}
}
@Test
public void givenMultipleThreads_whenUsingCountDownLatch_thenMainShoudWaitForAllToFinish() {
ExecutorService WORKER_THREAD_POOL = Executors.newFixedThreadPool(10);
try {
long startTime = System.currentTimeMillis();
// create a CountDownLatch that waits for the 2 threads to finish
CountDownLatch latch = new CountDownLatch(2);
for (int i = 0; i < 2; i++) {
WORKER_THREAD_POOL.submit(() -> {
try {
@ -69,13 +78,13 @@ public class WaitingForThreadsToFinishManualTest {
ExecutorService WORKER_THREAD_POOL = Executors.newFixedThreadPool(10);
List<Callable<String>> callables = Arrays.asList(
new DelayedCallable("fast thread", 100),
new DelayedCallable("fast thread", 100),
new DelayedCallable("slow thread", 3000));
try {
long startProcessingTime = System.currentTimeMillis();
List<Future<String>> futures = WORKER_THREAD_POOL.invokeAll(callables);
awaitTerminationAfterShutdown(WORKER_THREAD_POOL);
try {
@ -100,7 +109,7 @@ public class WaitingForThreadsToFinishManualTest {
} catch (ExecutionException | InterruptedException ex) {
ex.printStackTrace();
}
}
}
@Test
@ -109,7 +118,7 @@ public class WaitingForThreadsToFinishManualTest {
CompletionService<String> service = new ExecutorCompletionService<>(WORKER_THREAD_POOL);
List<Callable<String>> callables = Arrays.asList(
new DelayedCallable("fast thread", 100),
new DelayedCallable("fast thread", 100),
new DelayedCallable("slow thread", 3000));
for (Callable<String> callable : callables) {
@ -142,4 +151,4 @@ public class WaitingForThreadsToFinishManualTest {
awaitTerminationAfterShutdown(WORKER_THREAD_POOL);
}
}
}
}

View File

@ -0,0 +1,10 @@
package com.baeldung.concurrent.executorservice;
import java.util.concurrent.Callable;
public class CallableTask implements Callable<String> {
@Override
public String call() throws Exception {
return "Hello world";
}
}

View File

@ -1,27 +0,0 @@
package com.baeldung.concurrent.executorservice;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
public class ExecutorServiceDemo {
ExecutorService executor = Executors.newFixedThreadPool(10);
public void execute() {
executor.submit(() -> {
new Task();
});
executor.shutdown();
executor.shutdownNow();
try {
executor.awaitTermination(20l, TimeUnit.NANOSECONDS);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}

View File

@ -1,13 +1,15 @@
package com.baeldung.concurrent.Scheduledexecutorservice;
package com.baeldung.concurrent.executorservice;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;
public class ScheduledExecutorServiceDemo {
private Task runnableTask;
private CallableTask callableTask;
private void execute() {
ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor();
getTasksToRun().apply(executorService);
@ -21,23 +23,14 @@ public class ScheduledExecutorServiceDemo {
}
private Function<ScheduledExecutorService, Void> getTasksToRun() {
runnableTask = new Task();
callableTask = new CallableTask();
return (executorService -> {
ScheduledFuture<?> scheduledFuture1 = executorService.schedule(() -> {
// Task
}, 1, TimeUnit.SECONDS);
ScheduledFuture<?> scheduledFuture2 = executorService.scheduleAtFixedRate(() -> {
// Task
}, 1, 10, TimeUnit.SECONDS);
ScheduledFuture<?> scheduledFuture3 = executorService.scheduleWithFixedDelay(() -> {
// Task
}, 1, 10, TimeUnit.SECONDS);
ScheduledFuture<String> scheduledFuture4 = executorService.schedule(() -> {
// Task
return "Hellow world";
}, 1, TimeUnit.SECONDS);
Future<String> resultFuture = executorService.schedule(callableTask, 1, TimeUnit.SECONDS);
executorService.scheduleAtFixedRate( runnableTask, 100, 450, TimeUnit.SECONDS);
executorService.scheduleWithFixedDelay( runnableTask, 100, 150, TimeUnit.SECONDS);
return null;
});
}
@ -48,5 +41,4 @@ public class ScheduledExecutorServiceDemo {
demo.executeWithMultiThread();
}
}