diff --git a/core-java-concurrency/src/main/java/com/baeldung/concurrent/parameter/AverageCalculator.java b/core-java-concurrency/src/main/java/com/baeldung/concurrent/parameter/AverageCalculator.java new file mode 100644 index 0000000000..a548b5d4a7 --- /dev/null +++ b/core-java-concurrency/src/main/java/com/baeldung/concurrent/parameter/AverageCalculator.java @@ -0,0 +1,20 @@ +package com.baeldung.concurrent.parameter; + +import java.util.concurrent.Callable; +import java.util.stream.IntStream; + +public class AverageCalculator implements Callable { + + int[] numbers; + + public AverageCalculator(int... parameter) { + this.numbers = parameter == null ? new int[0] : parameter; + } + + @Override + public Double call() throws Exception { + return IntStream.of(this.numbers) + .average() + .orElse(0d); + } +} diff --git a/core-java-concurrency/src/main/java/com/baeldung/concurrent/parameter/ParameterizedThreadExample.java b/core-java-concurrency/src/main/java/com/baeldung/concurrent/parameter/ParameterizedThreadExample.java new file mode 100644 index 0000000000..73c61f3fb1 --- /dev/null +++ b/core-java-concurrency/src/main/java/com/baeldung/concurrent/parameter/ParameterizedThreadExample.java @@ -0,0 +1,32 @@ +package com.baeldung.concurrent.parameter; + +import java.util.concurrent.Callable; +import java.util.stream.IntStream; + +public class ParameterizedThreadExample { + + public static void parameterisedThreadAnonymousClass() { + final String parameter = "123"; + Thread parameterizedThread = new Thread(new Runnable() { + + @Override + public void run() { + System.out.println(Thread.currentThread() + .getName() + " : " + parameter); + } + }); + + parameterizedThread.start(); + } + + public static Callable sumCalculator(int... numbers) { + return () -> numbers != null ? IntStream.of(numbers) + .sum() : 0; + } + + public static Callable averageCalculator(int... numbers) { + return () -> numbers != null ? IntStream.of(numbers) + .average() + .orElse(0d) : 0d; + } +} diff --git a/core-java-concurrency/src/test/java/com/baeldung/parameters/ParameterizedThreadUnitTest.java b/core-java-concurrency/src/test/java/com/baeldung/parameters/ParameterizedThreadUnitTest.java new file mode 100644 index 0000000000..21b374e609 --- /dev/null +++ b/core-java-concurrency/src/test/java/com/baeldung/parameters/ParameterizedThreadUnitTest.java @@ -0,0 +1,43 @@ +package com.baeldung.parameters; + +import com.baeldung.concurrent.parameter.AverageCalculator; +import org.junit.Test; + +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.Future; +import java.util.stream.IntStream; + +import static org.junit.Assert.assertEquals; + +public class ParameterizedThreadUnitTest { + + @Test + public void whenSendingParameterToCallable_thenSuccessful() throws Exception { + ExecutorService executorService = Executors.newSingleThreadExecutor(); + Future result = executorService.submit(new AverageCalculator(1, 2, 3)); + try { + assertEquals(Double.valueOf(2.0), result.get()); + } finally { + executorService.shutdown(); + } + } + + @Test + public void whenParametersToThreadWithLamda_thenParametersPassedCorrectly() throws Exception { + ExecutorService executorService = Executors.newFixedThreadPool(2); + int[] numbers = new int[] { 4, 5, 6 }; + try { + Future sumResult = executorService.submit(() -> IntStream.of(numbers) + .sum()); + Future averageResult = executorService.submit(() -> IntStream.of(numbers) + .average() + .orElse(0d)); + assertEquals(Integer.valueOf(15), sumResult.get()); + assertEquals(Double.valueOf(5.0), averageResult.get()); + } finally { + executorService.shutdown(); + } + } + +}