[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:
parent
4193cc9a56
commit
fd1bf7a029
|
@ -1,5 +1,6 @@
|
||||||
package com.baeldung.concurrent.executorservice;
|
package com.baeldung.concurrent.executorservice;
|
||||||
|
|
||||||
|
|
||||||
import java.util.concurrent.Callable;
|
import java.util.concurrent.Callable;
|
||||||
import java.util.concurrent.CountDownLatch;
|
import java.util.concurrent.CountDownLatch;
|
||||||
|
|
|
@ -1,15 +1,24 @@
|
||||||
package com.baeldung.concurrent.executorservice;
|
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.junit.Test;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
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 {
|
public class WaitingForThreadsToFinishManualTest {
|
||||||
|
|
||||||
private static final Logger LOG = LoggerFactory.getLogger(WaitingForThreadsToFinishManualTest.class);
|
private static final Logger LOG = LoggerFactory.getLogger(WaitingForThreadsToFinishManualTest.class);
|
|
@ -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";
|
||||||
|
}
|
||||||
|
}
|
|
@ -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();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -1,13 +1,15 @@
|
||||||
package com.baeldung.concurrent.Scheduledexecutorservice;
|
package com.baeldung.concurrent.executorservice;
|
||||||
|
|
||||||
import java.util.concurrent.Executors;
|
import java.util.concurrent.Executors;
|
||||||
|
import java.util.concurrent.Future;
|
||||||
import java.util.concurrent.ScheduledExecutorService;
|
import java.util.concurrent.ScheduledExecutorService;
|
||||||
import java.util.concurrent.ScheduledFuture;
|
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
import java.util.function.Function;
|
import java.util.function.Function;
|
||||||
|
|
||||||
public class ScheduledExecutorServiceDemo {
|
public class ScheduledExecutorServiceDemo {
|
||||||
|
|
||||||
|
private Task runnableTask;
|
||||||
|
private CallableTask callableTask;
|
||||||
private void execute() {
|
private void execute() {
|
||||||
ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor();
|
ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor();
|
||||||
getTasksToRun().apply(executorService);
|
getTasksToRun().apply(executorService);
|
||||||
|
@ -21,23 +23,14 @@ public class ScheduledExecutorServiceDemo {
|
||||||
}
|
}
|
||||||
|
|
||||||
private Function<ScheduledExecutorService, Void> getTasksToRun() {
|
private Function<ScheduledExecutorService, Void> getTasksToRun() {
|
||||||
|
|
||||||
|
runnableTask = new Task();
|
||||||
|
callableTask = new CallableTask();
|
||||||
|
|
||||||
return (executorService -> {
|
return (executorService -> {
|
||||||
ScheduledFuture<?> scheduledFuture1 = executorService.schedule(() -> {
|
Future<String> resultFuture = executorService.schedule(callableTask, 1, TimeUnit.SECONDS);
|
||||||
// Task
|
executorService.scheduleAtFixedRate( runnableTask, 100, 450, TimeUnit.SECONDS);
|
||||||
}, 1, TimeUnit.SECONDS);
|
executorService.scheduleWithFixedDelay( runnableTask, 100, 150, 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);
|
|
||||||
return null;
|
return null;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -48,5 +41,4 @@ public class ScheduledExecutorServiceDemo {
|
||||||
demo.executeWithMultiThread();
|
demo.executeWithMultiThread();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
Loading…
Reference in New Issue