Monday, August 10, 2020

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

Oracle Java Certification, Oracle Java Exam Prep, Oracle Java Prep, Oracle Java Study Materials

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.

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


Oracle Java Certification, Oracle Java Exam Prep, Oracle Java Prep, Oracle Java Study Materials
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

Related Posts

0 comments:

Post a Comment