Java Threading - Runnable Interface

| Page Views: 82 java runnable

A Runnable is an abstraction for an executable command.

It's an interface that defined in the java.lang package:

package java.lang;
public interface Runnable{}


The Runnable interface is used extensively to execute code in Threads.

The Runnable interface has one abstract method called run():

public abstract void run ()

Normally you implement that method whenever a your class implements Runnable interface.

The method will then get called to start executing the active part of the class' code.

Runnable Interface Grandchildren

Well we mean indirect subclasses. Runnable has alot of them so we list only afew commonly used:

No. Class Description
1. Thread A concurrent unit of execution.This class implements Runnable interface.The Thread class makes use of the Runnable's abstract run() method by calling it's(the Thread's) concrete run() method. That run() method then calls the Runnable's abstract run() method internally.
2. TimerTask An abstract class used to describe a task that should run at a specified time.Maybe once,Maybe recurringly.TimerTask implements Runnable interface.The tasks done by the TimerTask do get specified in the run() method.
3. FutureTask<> A class that represents a ceancellable asynchronous computation.This class implements the RunnableFuture interface, which implements the Runnable interface.

Let's now see some examples:

Java Runnable Example

Let's see an example of Java Runnable with Threads.

  1. First specify your application's package:
    package info.tutorialsloop;

    Packages group classes.

  2. Create the class:
    public class Main{}
  3. Let's make our class implement Runnable interface:
    public class Main implements Runnable {}
  4. This will force us override the run() method.So we do so:

    public void run() {
  5. Let's create two fields at the class level.One to represent the distance our Spaceship has travelled while another a boolean to be flagged to false or true depending on whether we've arrived or not:
    private static int spaceship_distance=0;
    private boolean arrived=false;
  6. Then go back to the run() method and add the following code:
        while (!arrived)
                System.out.println("Distance in Light Years : "+spaceship_distance++);
                    System.out.println("Reached Destination. Thanks.");
            } catch (InterruptedException e) {
  7. We then create our main method:

    public static void main(String[] args) {
  8. Then add the following code in the main method:
        Main m=new Main();
        Thread myThread=new Thread(m);

    Basically we are instantiating a thread and passing in our Main class instance. We then start the thread via the start() method.

Here's what we get:

Distance in Light Years : 0
Distance in Light Years : 1
Distance in Light Years : 2
Distance in Light Years : 3
Distance in Light Years : 4
Distance in Light Years : 5
Distance in Light Years : 6
Distance in Light Years : 7
Distance in Light Years : 8
Distance in Light Years : 9
Reached Destination. Thanks.

How do You Feel after reading this?

According to scientists, we humans have 8 primary innate emotions: joy, acceptance, fear, surprise, sadness, disgust, anger, and anticipation. Feel free to tell us how you feel about this article using these emotes or via the comment section.

About Me.

After completing his Software Engineering bachelors program, Oclemy(Clement Ochieng) these days is a man of two lives. At day he works for a startup in Nairobi, Kenya. At night he works tirelessly on building ProgrammingWizards TV, a tv channel for student coders and this website to help share the source code. In between he practices Meditation and Self actualization to help him keep balance. He also likes going for long solo walks to connect more with nature.


What do You Think

Previous Post Next Post