[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,14 +1,15 @@
|
|||||||
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;
|
||||||
|
|
||||||
public class DelayedCallable implements Callable<String> {
|
public class DelayedCallable implements Callable<String> {
|
||||||
|
|
||||||
private String name;
|
private String name;
|
||||||
private long period;
|
private long period;
|
||||||
private CountDownLatch latch;
|
private CountDownLatch latch;
|
||||||
|
|
||||||
public DelayedCallable(String name, long period, CountDownLatch latch) {
|
public DelayedCallable(String name, long period, CountDownLatch latch) {
|
||||||
this(name, period);
|
this(name, period);
|
||||||
this.latch = latch;
|
this.latch = latch;
|
||||||
@ -23,11 +24,11 @@ public class DelayedCallable implements Callable<String> {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
Thread.sleep(period);
|
Thread.sleep(period);
|
||||||
|
|
||||||
if (latch != null) {
|
if (latch != null) {
|
||||||
latch.countDown();
|
latch.countDown();
|
||||||
}
|
}
|
||||||
|
|
||||||
} catch (InterruptedException ex) {
|
} catch (InterruptedException ex) {
|
||||||
// handle exception
|
// handle exception
|
||||||
ex.printStackTrace();
|
ex.printStackTrace();
|
||||||
@ -36,4 +37,4 @@ public class DelayedCallable implements Callable<String> {
|
|||||||
|
|
||||||
return name;
|
return name;
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -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);
|
||||||
@ -26,18 +35,18 @@ public class WaitingForThreadsToFinishManualTest {
|
|||||||
Thread.currentThread().interrupt();
|
Thread.currentThread().interrupt();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenMultipleThreads_whenUsingCountDownLatch_thenMainShoudWaitForAllToFinish() {
|
public void givenMultipleThreads_whenUsingCountDownLatch_thenMainShoudWaitForAllToFinish() {
|
||||||
|
|
||||||
ExecutorService WORKER_THREAD_POOL = Executors.newFixedThreadPool(10);
|
ExecutorService WORKER_THREAD_POOL = Executors.newFixedThreadPool(10);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
long startTime = System.currentTimeMillis();
|
long startTime = System.currentTimeMillis();
|
||||||
|
|
||||||
// create a CountDownLatch that waits for the 2 threads to finish
|
// create a CountDownLatch that waits for the 2 threads to finish
|
||||||
CountDownLatch latch = new CountDownLatch(2);
|
CountDownLatch latch = new CountDownLatch(2);
|
||||||
|
|
||||||
for (int i = 0; i < 2; i++) {
|
for (int i = 0; i < 2; i++) {
|
||||||
WORKER_THREAD_POOL.submit(() -> {
|
WORKER_THREAD_POOL.submit(() -> {
|
||||||
try {
|
try {
|
||||||
@ -69,13 +78,13 @@ public class WaitingForThreadsToFinishManualTest {
|
|||||||
ExecutorService WORKER_THREAD_POOL = Executors.newFixedThreadPool(10);
|
ExecutorService WORKER_THREAD_POOL = Executors.newFixedThreadPool(10);
|
||||||
|
|
||||||
List<Callable<String>> callables = Arrays.asList(
|
List<Callable<String>> callables = Arrays.asList(
|
||||||
new DelayedCallable("fast thread", 100),
|
new DelayedCallable("fast thread", 100),
|
||||||
new DelayedCallable("slow thread", 3000));
|
new DelayedCallable("slow thread", 3000));
|
||||||
|
|
||||||
try {
|
try {
|
||||||
long startProcessingTime = System.currentTimeMillis();
|
long startProcessingTime = System.currentTimeMillis();
|
||||||
List<Future<String>> futures = WORKER_THREAD_POOL.invokeAll(callables);
|
List<Future<String>> futures = WORKER_THREAD_POOL.invokeAll(callables);
|
||||||
|
|
||||||
awaitTerminationAfterShutdown(WORKER_THREAD_POOL);
|
awaitTerminationAfterShutdown(WORKER_THREAD_POOL);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@ -100,7 +109,7 @@ public class WaitingForThreadsToFinishManualTest {
|
|||||||
|
|
||||||
} catch (ExecutionException | InterruptedException ex) {
|
} catch (ExecutionException | InterruptedException ex) {
|
||||||
ex.printStackTrace();
|
ex.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -109,7 +118,7 @@ public class WaitingForThreadsToFinishManualTest {
|
|||||||
CompletionService<String> service = new ExecutorCompletionService<>(WORKER_THREAD_POOL);
|
CompletionService<String> service = new ExecutorCompletionService<>(WORKER_THREAD_POOL);
|
||||||
|
|
||||||
List<Callable<String>> callables = Arrays.asList(
|
List<Callable<String>> callables = Arrays.asList(
|
||||||
new DelayedCallable("fast thread", 100),
|
new DelayedCallable("fast thread", 100),
|
||||||
new DelayedCallable("slow thread", 3000));
|
new DelayedCallable("slow thread", 3000));
|
||||||
|
|
||||||
for (Callable<String> callable : callables) {
|
for (Callable<String> callable : callables) {
|
||||||
@ -142,4 +151,4 @@ public class WaitingForThreadsToFinishManualTest {
|
|||||||
awaitTerminationAfterShutdown(WORKER_THREAD_POOL);
|
awaitTerminationAfterShutdown(WORKER_THREAD_POOL);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -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…
x
Reference in New Issue
Block a user