This post will teach you how to implement a thread in three different ways, all of which rely on the Runnable interface. You will learn that using the Runnable interface is the most flexible way to create multi-threaded code in Java.
Implementing Runnable interface with a class
The easiest way to create a Runnable to run in a thread. It to create a class that implements the Runnable interface. The Runnable interface
only has a single method void run();
, Because of this it is called a functional interface. Implementing functional interfaces is very straight forward
you only implement the single method. In the following example I implemented the Runnable interface and made it print something to the console.
|
|
Implementing the Runnable interface alone is not enough. you need thread to run the Runnable class you just created. In the following example an instance is created of the class implementing the Runnable interface and this instance is passed to a new Thread as a constructor parameter.
To run the Runnable you only need to call the start method on the Thread. This starts the execution of the Runnable.
|
|
This is all the code you need to start with Threads and Runnable. The downside of this method is that creating a class can become quite tiresome if you have lots of different tasks that you want to run in threads. The following examples will show you how to create a runnable without having to create a class each time.
Implement Runnable with a lambda expression
With a lambda expression we can implement the Runnable interface with a short code block. This block of code is everything that
we need to create a thread. In the following example, I created a thread using a Lambda expression that uses the Runnable interface
behind the scenes. The implementation of the Runnable
's run()
method happens after the ->
. In example, it prints something
to the console.
|
|
The previous example does the exact same thing as the first example but with less code, which always nice (as long as it does not hurt readability).
The following example, can run more than one statement inside a thread. The difference is in how the lambda expression is created.
This lambda expression with the two statements looks like this: () -> { system.out...; system.out...; }
. While the previous expression
does not have the {}
parenthesis. The parenthesis allow you to pass more statements to the thread/Lambda expression.
|
|
Lambda expressions are nice when you only need to call a couple of methods. When it's starts to get very long and unreadable it is best to switch to a method reference as is done in the next section.
Method reference
To create a Thread in Java you can use a method reference as well. In the following example the void getAlsoPrintsThis()
matches the
signature of the void run()
method of the Runnable interface. This means that we can use it as a method reference and pass it as
parameter to the Thread class. On line 11, of the following example, I create a method reference to the getAlsoPrintsThis()
method.
|
|
A method reference is nice when:
- it is getting to big for a lambda expression to be readable.
- You want to call the method from somewhere else as well.
- You want to run single method using a thread
Conclusion
In this post, we looked at four ways of implementing a thread using the Runnable interface. Each of these approaches have their own pros and cons. Use the approach that best suits the problem you are facing.