[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,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);

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();
}
}