February 3, 2019

Implement runnable interface using a class in java.

RunnableĀ is a Functional Interface. It has one abstract method calledĀ run.

To make use of this class, we need to provide an implementation for this method. In practice, compiler only needs a non-abstract method. You can provide a empty body and that will be enough. The implementation need not be meaningful.

Here is a simple class, which implements a Runnable interface.

public class SimpleRunnable implements Runnable {
    @Override
    public void run() {
    }
}

In this implementation, we are providing an empty implementation. It won’t do anything.

We can modify above implementation to print the current thread’s name.

public class SimpleRunnable implements Runnable {
    @Override
    public void run() {
        System.out.println("Current Thread name :- " + Thread.currentThread().getName());
    }
}

The runnable class is pretty useless, if it is not passed to a Thread class instance.

Let us create a main function.

public static void main(String ... args) {
    //#1 -- By instance of a class - Method 1                                                                                                                                                                  
    //Create an object of class, which implements runnable interface                                                                                                                                           
    SimpleRunnable runnable = new SimpleRunnable();

    //Pass that runnable instance to a thread instance                                                                                                                                                         
    Thread t = new Thread(runnable);
    //set attributes                                                                                                                                                                                           
    t.setName("SimpleRunnable Object from an implementing class" );
 
     //Start thread                                                                                                                                                                                             
     t.start();
}

In the above main function,

  1. We create an instance or SimpleRunnable class.
  2. Then pass that instance to an instance of Thread class.
  3. call the start method of thread class.

To make the output of the thread more useful and identify that our runnable instance is running. We set the name of the thread.

OUTPUT

Current Thread name :- SimpleRunnable Object from implementin class

 

COMPLETE CODE

public class SimpleRunnable implements Runnable {

    @Override
    public void run() {
        System.out.println("Current Thread name :- " + Thread.currentThread().getName());
    }

    public static void main(String ... args) {

        //#1 -- By instance of a class - Method 1                                                                                                                                                                  
        //Create an object of class, which implements runnable interface                                                                                                                                           
        SimpleRunnable runnable = new SimpleRunnable();

        //Pass that runnable instance to a thread instance                                                                                                                                                         
        Thread t = new Thread(runnable);
        
        //set attributes                                                                                                                                                                                           
        t.setName("SimpleRunnable Object from implementin class" );


        //Start thread                                                                                                                                                                                             
        t.start();

    }
}