Core Java

How to create a thread without implementing the Runnable interface in Java?

A quick programming guide to creating a thread without using the Runnable interface in java. This can be achieved using new Thread ( new Runnable() { public void run(){}});.

1. Introduction

In this tutorial, You’ll learn how to create a thread without implementing the Runnable interface in Java.

Thread is a lightweight process and every program in java starts in a thread. So by default when you run the main program that has the main() method, JVM will create a thread to run the main program. The default thread is called “main thread“.

Additionally, Java supports multithreading which means you can one or more threads at the same time.

Let us see the different ways to create a thread in java using Anonymous implementation for the Runnable interface.

2. Anonymous Runnable Implementation to Create A Thread

Instead of creating a separate class and implements the Runnable interface directly, you can create as below using Anonymous implementation concept.

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
package com.javaprogramto.threads;
 
 
public class AnonymousRunnableThread {
 
    public static void main(String[] args) {
 
        new Thread(new Runnable() {
            @Override            public void run() {
 
                for (int i = 0; i < 10; i++) {
                    System.out.println(Thread.currentThread().getName()+", i value from thread- "+i);
                }
            }
        }).start();
 
        for (int i = 0; i < 10; i++) {
            System.out.println(Thread.currentThread().getName()+", i value from main thread - "+i);
        }
    }
}

Output:

main, i value from main thread – 0

Thread-0, i value from thread- 0

main, i value from main thread – 1

Thread-0, i value from thread- 1

main, i value from main thread – 2

Thread-0, i value from thread- 2

main, i value from main thread – 3

Thread-0, i value from thread- 3

main, i value from main thread – 4

Thread-0, i value from thread- 4

main, i value from main thread – 5

Thread-0, i value from thread- 5

main, i value from main thread – 6

Thread-0, i value from thread- 6

main, i value from main thread – 7

Thread-0, i value from thread- 7

main, i value from main thread – 8

Thread-0, i value from thread- 8

main, i value from main thread – 9

Thread-0, i value from thread- 9

3. Anonymous Runnable Implementation to print even numbers

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package com.javaprogramto.threads;
 
 
public class AnonymousRunnableThreadPrintEvenNumbers {
 
    public static void main(String[] args) {
 
        new Thread(new Runnable() {
            @Override            public void run() {
 
                for (int i = 0; i <= 10; i++) {
                     if(i % 2 == 0){
                         System.out.println(Thread.currentThread().getName() +" - "+i);
                     }
                }
 
                System.out.println("Child thread ended "+Thread.currentThread().getName());
            }
        }).start();
 
        System.out.println("main thread ended.");
    }
}

Output:

main thread ended.

Thread-0 – 0

Thread-0 – 2

Thread-0 – 4

Thread-0 – 6

Thread-0 – 8

Thread-0 – 10

Child thread ended Thread-0

4. Conclusion

In this article,  You have seen how to create a thread without implementing the Runnable interface.

Example programs using Anonymous implementation of the interface to print the first 10 numbers and even numbers.

As usual, Examples shown are over GitHub.


Read more on:

Published on Java Code Geeks with permission by Venkatesh Nukala, partner at our JCG program. See the original article here: How to create a thread without implementing the Runnable interface in Java?

Opinions expressed by Java Code Geeks contributors are their own.

Venkatesh Nukala

Venkatesh Nukala is a Software Engineer working for Online Payments Industry Leading company. In my free time, I would love to spend time with family and write articles on technical blogs. More on JavaProgramTo.com
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

2 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
JoeHx
4 years ago

I mean, these anonymous classes still implement the Runnable interface, even if they are not in their own declared, named class files.

You could also use lambdas (Java 8+):

new Thread(() -> doTheWork()).start();

Whether that example “implements” Runnable is up to interpretation.

ANIRUDH RAGHAVENDRACHAR MATHAD

Misleading title!

Back to top button