Pages

Footer Pages

Spring Boot

Java String API

Java Conversions

Kotlin Programs

Kotlin Conversions

Java Threads Tutorial

Java 8 Tutorial

Saturday, July 4, 2020

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

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.

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




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.

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


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:


Creating a Thread in two ways
Thread Priorities
Thread join() method examples
How to kill or Stop a Thread in Java?

Ref

No comments:

Post a Comment

Please do not add any spam links in the comments section.