Running a task on a different thread in Java 8
September 9, 2017 Leave a comment
Occasionally it can be worth putting a task on a different thread so that it doesn’t block the main thread. Examples include a task that analyses heavy files, a task that sends out emails etc. If we put these tasks on a different thread and don’t wait for it to return a result then it’s called the fire-and-forget pattern. We start a new thread and let it run in the background. The task on the different thread is expected to carry out its functions independently of the main thread.
Let’s imagine that the following greetCustomer method is something we want to run on separate thread so that the main thread is not blocked: