简体   繁体   中英

Java 8 concurrency in AWS lambda?

We have an AWS lambda function that needs to perform a few checks done by calling remote services. As long as one of them returning false, lambda can return; otherwise, all the checks need to be finished to make sure none returning false. Right now I am using a parallel stream to run the tasks, as they can go independently.

In a may-not-be-rare situation, the main thread returns while one of the tasks is still running with its thread, or thread blocked waiting for I/O, as short-circuiting has seen a false with another task. AWS lambda documentation says that all threads in Lambda will be frozen when main thread returns. And they will thaw once lambda is handling the next request. Will the busy/blocked thread keep working on the original task after getting re-activated, or it will take on the new task for current request?

Would really appreciate it if Lambda gurus can share some insights.

I hope I understood correctly. You want to perform parallel activities while waiting for them to finish.

I just read in StackOverflow a comment saying the following:

Streams is about data-parallelism; data parallel problems are CPU-bound, not IO-bound. It seems that you're simply looking to run a number of mostly unrelated IO-intensive tasks concurrently. Use a plain-old thread pool for that; your first example is an ideal candidate for ExecutorService.invokeAll()

Maybe ExecutorService can help.

I don't know how your code is being structured but I can propose something like this:

int processors = Runtime.getRuntime().availableProcessors();
ExecutorService executorService = Executors.newFixedThreadPool(processors);

List<Callable<Boolean>> services = getURLToCheck().parallelStream()
       .map(this::checkService)
       .collect(Collectors.toList());

try {
    List<Future<Boolean>> futures = executorService.invokeAll(services);
    // do your validation with the concurrent tasks. 
} catch (InterruptedException e) {
    // Handle as you wish
}

Where also:

private List<URL> getURLToCheck() {
    // Fetch your URL from wherever :)
}

private Callable<Boolean> checkService(URL url){
    // Logic to check the service
}

The Future class has to key methods that may be useful for you. The isDone() method and the .get() .

The first one indicates whether the task finished or not, and the second one will wait for it to finish throwing all the exceptions that occurred inside but wrapped in ExecutionException . Maybe you can combine those methods to have the validation done. Having a quick think, I imagined a while loop where you ask if the future finished, and if so, have the validation result and with that, break that loop if false. But I don't like it haha.

Hope I made my self clear. And also I hope that can help. If not, i tried my best.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM