Last active
June 17, 2017 16:51
-
-
Save mguilherme/79dcd4628585d4e7d5b9d094e2c33182 to your computer and use it in GitHub Desktop.
Start 5 tasks and wait until they finish
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.guilherme.miguel; | |
import io.vavr.control.Try; | |
import lombok.extern.slf4j.Slf4j; | |
import java.util.List; | |
import java.util.concurrent.ExecutorService; | |
import java.util.concurrent.Executors; | |
import java.util.concurrent.Future; | |
import java.util.stream.Collectors; | |
import java.util.stream.IntStream; | |
import static java.lang.String.format; | |
@Slf4j | |
public class TaskExecutor { | |
public static void main(String[] args) { | |
ExecutorService executorService = Executors.newFixedThreadPool(5); | |
// Create 5 Tasks | |
List<Future<String>> futures = IntStream.rangeClosed(1, 5) | |
.mapToObj(i -> executorService.submit(() -> doTask(i))) | |
.collect(Collectors.toList()); | |
// Wait until everything finishes | |
log.info("Waiting for everything to finish..."); | |
List<String> list = futures.stream() | |
.map(i -> Try.of(() -> i.get()).getOrElse("")) | |
.collect(Collectors.toList()); | |
executorService.shutdown(); | |
log.info("Done..."); | |
list.forEach(i -> log.info(i.toString())); | |
} | |
/** | |
* Dummy Task | |
*/ | |
private static String doTask(int i) { | |
log.info("Start Task {}", i); | |
Try.run(() -> Thread.sleep((long) (Math.random() * 5000))); | |
log.info("End Task {}", i); | |
return format("Task %s", i); | |
} | |
} | |
// Expected Result | |
// [pool-1-thread-2] INFO com.guilherme.miguel.TaskExecutor - Start Task 2 | |
// [pool-1-thread-3] INFO com.guilherme.miguel.TaskExecutor - Start Task 3 | |
// [pool-1-thread-4] INFO com.guilherme.miguel.TaskExecutor - Start Task 4 | |
// [main] INFO com.guilherme.miguel.TaskExecutor - Waiting for everything to finish... | |
// [pool-1-thread-5] INFO com.guilherme.miguel.TaskExecutor - Start Task 5 | |
// [pool-1-thread-1] INFO com.guilherme.miguel.TaskExecutor - Start Task 1 | |
// [pool-1-thread-5] INFO com.guilherme.miguel.TaskExecutor - End Task 5 | |
// [pool-1-thread-2] INFO com.guilherme.miguel.TaskExecutor - End Task 2 | |
// [pool-1-thread-1] INFO com.guilherme.miguel.TaskExecutor - End Task 1 | |
// [pool-1-thread-4] INFO com.guilherme.miguel.TaskExecutor - End Task 4 | |
// [pool-1-thread-3] INFO com.guilherme.miguel.TaskExecutor - End Task 3 | |
// [main] INFO com.guilherme.miguel.TaskExecutor - Done... | |
// [main] INFO com.guilherme.miguel.TaskExecutor - Task 1 | |
// [main] INFO com.guilherme.miguel.TaskExecutor - Task 2 | |
// [main] INFO com.guilherme.miguel.TaskExecutor - Task 3 | |
// [main] INFO com.guilherme.miguel.TaskExecutor - Task 4 | |
// [main] INFO com.guilherme.miguel.TaskExecutor - Task 5 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment