Pages

Thursday, July 9, 2020

Create Thread without extending Thread and implementing Runnable

1. Introduction


In this quick article, You'll learn how to create a thread without extending the Thread class and implementing the Runnable interface.

Create Thread without extending Thread and implementing Runnable




2. Create Thread Without Extending Thread Class - Anonymous Thread 

package com.javaprogramto.threads;

public class AnonymousThreadExample {
    public static void main(String[] args) {
        new Thread() {

            public void run() {

                for (int i = 0; i <= 10; i++) {
                    System.out.println(
                            "Executing " + i + " thread name - " + Thread.currentThread().getName());
                }

            }

        }.start();

        for (int i = 0; i <= 10; i++) {
            System.out.println(
                    "Executing " + i + " thread name - " + Thread.currentThread().getName());
        }
    }
}
Output:

Executing 0 thread name - main
Executing 0 thread name - Thread-0
Executing 1 thread name - main
Executing 1 thread name - Thread-0
Executing 2 thread name - main
Executing 2 thread name - Thread-0
Executing 3 thread name - main
Executing 3 thread name - Thread-0
Executing 4 thread name - main
Executing 4 thread name - Thread-0
Executing 5 thread name - main
Executing 5 thread name - Thread-0
Executing 6 thread name - main
Executing 6 thread name - Thread-0
Executing 7 thread name - main
Executing 7 thread name - Thread-0
Executing 8 thread name - main
Executing 8 thread name - Thread-0
Executing 9 thread name - main
Executing 9 thread name - Thread-0
Executing 10 thread name - main
Executing 10 thread name - Thread-0

3. Create Thread Without Implementing Runnable Interface - Anonymous Runnable 


package com.javaprogramto.threads;
public class AnonymousRunnableExample {
    public static void main(String[] args) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i <= 10; i++) {
                    System.out.println(
                            "Executing " + i + " thread name - " + Thread.currentThread().getName());
                }
            }
        }).start();
        for (int i = 0; i <= 10; i++) {
            System.out.println(
                    "Executing " + i + " thread name - " + Thread.currentThread().getName());
        }
    }
}



Output:

Executing 0 thread name - Thread-0
Executing 0 thread name - main
Executing 1 thread name - Thread-0
Executing 1 thread name - main
Executing 2 thread name - Thread-0
Executing 2 thread name - main
Executing 3 thread name - Thread-0
Executing 3 thread name - main
Executing 4 thread name - Thread-0
Executing 4 thread name - main
Executing 5 thread name - Thread-0
Executing 5 thread name - main
Executing 6 thread name - Thread-0
Executing 6 thread name - main
Executing 7 thread name - Thread-0
Executing 7 thread name - main
Executing 8 thread name - Thread-0
Executing 8 thread name - main
Executing 9 thread name - Thread-0
Executing 9 thread name - main
Executing 10 thread name - Thread-0
Executing 10 thread name - main

4. Conclusion


In this article, You have seen how to create a thread without using Thread and Runnable.
Examples to create a thread using Anonymous Implementations.

As usual, All examples are over GitHub.

No comments:

Post a Comment

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