Skip to content
Advertisement

How to call a method and run it asynchronously?

Is there a way to run getUsers() asynchronously only from main() in Java 8?

Running getUsers() on the main thread would take 300 seconds. I wish to make it in less than 180 seconds with 4 cores.

JavaScript

Without modifying getUsers() (the objective), running the following is still taking 300 seconds:

JavaScript

Advertisement

Answer

You can divide your loop of users creation into smaller loops, and run them in parallel. Consider this example, which runs 4 times faster :

JavaScript
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement