1

I can't understand below code to create Runnable object. Could someone please explain clearly what is happening here:

public class ThreadStart {
  public static void main(String[] args) {  
    Thread t1 = new Thread(new Runnable() { 
      @Override
      public void run() {
        for(int i=0;i<5;i++) {
          System.out.println(i+" runnable thread...");
        }
      }
    });
    t1.start();
  }
} 
1
  • Check Anonymous class in java Commented Jan 13, 2015 at 7:32

5 Answers 5

5

This code creates an instance of an anonymous class (meaning it has no name) that implements the Runnable interface. This instance is passed to the constructor of the Thread class.

Sign up to request clarification or add additional context in comments.

Comments

4

Imagine the following implementation of your Runnable. A simple class that implements an interface.

public class MyRunnable implements Runnable {
    @Override
    public void run() {
        for (int i = 0; i < 5; i++) {
            System.out.println(i + " runnable thread...");
        }
    }
}

So, in order to create your Thread the way you intended you must provide an implementation of a Runnable object. There are various ways to achieve this.

The most straightforward approach is perhaps to use an instance of the Runnable from above:

Thread t1 = new Thread(new MyRunnable());

An alternative to that is to provide the Runnable directly in the code as an anonymous inner class which you can find out more about here.

An anonymous inner class simply means that you create a class as an expression in the code where you mean to use it - the class must follow the same signature (i.e. in this case it implements the Runnable interface).

Thread t2 = new Thread(new Runnable() {
    @Override
    public void run() {
        for (int i = 0; i < 5; i++) {
            System.out.println(i + " runnable thread...");
        }
    }
});

From the Oracle Docs:

The anonymous class expression consists of the following:

  • The new operator
  • The name of an interface to implement or a class to extend. In this example, the anonymous class is implementing the interface HelloWorld.
  • Parentheses that contain the arguments to a constructor, just like a normal class instance creation expression. Note: When you implement an interface, there is no constructor, so you use an empty pair of parentheses, as in this example.
  • A body, which is a class declaration body. More specifically, in the body, method declarations are allowed but statements are not.

So, basically what you are doing is passing a Runnable to your Thread constructor. If you are passing it as an anonymous class you simply pass it as an expression where you actually define your entire inner class.

Furthermore, in Java 8 you can replace the anonymous inner class with a lambda expression like this:

Thread t3 = new Thread(() -> {
    for (int i = 0; i < 5; i++) {
        System.out.println(i + " runnable thread...");
    }
});

1 Comment

Thank you Wassgren for your answer.
1

The code uses an anonymous class. You can learn everything about anonymous classes here.

Basically it means that you are going to create a class that implements Runnable by implementing the methods directly into your method call.

Comments

0

you use Anonymous class that implements Runnable interface and be passed into your Thread constructor which accept Runnable type

Anonymous Classes

Anonymous classes enable you to make your code more concise. They enable you to declare and instantiate a class at the same time. They are like local classes except that they do not have a name. Use them if you need to use a local class only once.

Comments

0

You are creating an instance of an anonymous inner class that implements the Runnable interface. If you go to your directory where the .class files will be placed, you can see the anonymous class created with the name ThreadStart$1.class. i.e, OuterClass$1.class

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.