Java 11 migration: patterns (t-v) (#1085)

* Moves visitor pattern to java 11

* Moves value-object pattern to java 11

* Moves unit-of-work pattern to java 11

* Moves typeobjectpattern pattern to java 11

* Moves twin pattern to java 11

* Moves trampoline pattern to java 11

* Moves tolerant-reader pattern to java 11

* Moves tls pattern to java 11

* Moves throttling pattern to java 11

* Moves thread-pool pattern to java 11

* Moves template-method pattern to java 11
This commit is contained in:
Anurag Agarwal
2019-11-14 11:12:05 +05:30
committed by Ilkka Seppälä
parent 160b737dcc
commit 50467c9e76
45 changed files with 379 additions and 422 deletions

View File

@ -56,7 +56,7 @@ public class App {
LOGGER.info("Program started");
// Create a list of tasks to be executed
List<Task> tasks = List.of(
var tasks = List.of(
new PotatoPeelingTask(3),
new PotatoPeelingTask(6),
new CoffeeMakingTask(2),
@ -82,10 +82,7 @@ public class App {
// Allocate new worker for each task
// The worker is executed when a thread becomes
// available in the thread pool
for (int i = 0; i < tasks.size(); i++) {
var worker = new Worker(tasks.get(i));
executor.execute(worker);
}
tasks.stream().map(Worker::new).forEach(executor::execute);
// All tasks were executed, now shutdown
executor.shutdown();
while (!executor.isTerminated()) {

View File

@ -27,15 +27,13 @@ import org.junit.jupiter.api.Test;
/**
* Application test
*
* @author ilkka
*
* @author ilkka
*/
public class AppTest {
@Test
public void test() {
String[] args = {};
App.main(args);
App.main(new String[]{});
}
}

View File

@ -23,27 +23,25 @@
package com.iluwatar.threadpool;
import org.junit.jupiter.api.Test;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.function.IntFunction;
import java.util.stream.Collectors;
import static java.time.Duration.ofMillis;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTimeout;
import java.util.ArrayList;
import java.util.Objects;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.function.IntFunction;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import org.junit.jupiter.api.Test;
/**
* Date: 12/30/15 - 18:22 PM
* Test for Tasks using a Thread Pool
* Date: 12/30/15 - 18:22 PM Test for Tasks using a Thread Pool
*
* @param <T> Type of Task
* @author Jeroen Meulemeester
*/
@ -87,14 +85,13 @@ public abstract class TaskTest<T extends Task> {
@Test
public void testIdGeneration() throws Exception {
assertTimeout(ofMillis(10000), () -> {
final ExecutorService service = Executors.newFixedThreadPool(THREAD_COUNT);
final var service = Executors.newFixedThreadPool(THREAD_COUNT);
final List<Callable<Integer>> tasks = new ArrayList<>();
for (int i = 0; i < TASK_COUNT; i++) {
tasks.add(() -> factory.apply(1).getId());
}
final var tasks = IntStream.range(0, TASK_COUNT)
.<Callable<Integer>>mapToObj(i -> () -> factory.apply(1).getId())
.collect(Collectors.toCollection(ArrayList::new));
final List<Integer> ids = service.invokeAll(tasks)
final var ids = service.invokeAll(tasks)
.stream()
.map(TaskTest::get)
.filter(Objects::nonNull)
@ -102,7 +99,7 @@ public abstract class TaskTest<T extends Task> {
service.shutdownNow();
final long uniqueIdCount = ids.stream()
final var uniqueIdCount = ids.stream()
.distinct()
.count();
@ -117,7 +114,7 @@ public abstract class TaskTest<T extends Task> {
*/
@Test
public void testTimeMs() {
for (int i = 0; i < 10; i++) {
for (var i = 0; i < 10; i++) {
assertEquals(this.expectedExecutionTime * i, this.factory.apply(i).getTimeMs());
}
}

View File

@ -23,13 +23,13 @@
package com.iluwatar.threadpool;
import org.junit.jupiter.api.Test;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;
import static org.mockito.Mockito.verifyZeroInteractions;
import org.junit.jupiter.api.Test;
/**
* Date: 12/30/15 - 18:21 PM
*
@ -42,8 +42,8 @@ public class WorkerTest {
*/
@Test
public void testRun() {
final Task task = mock(Task.class);
final Worker worker = new Worker(task);
final var task = mock(Task.class);
final var worker = new Worker(task);
verifyZeroInteractions(task);
worker.run();