Thread: Find the child threads of the main or parent thread

Forum : Threads and Synchronization   6 Days ago
User Image
ahasan

Newbie

Joined: 10 Jun, 2019

Points: 100

Threads: 1

Posts: 0

Find the child threads of the main or parent thread

| Quote Date : 10 Jul, 2019    Views:31    

Is there any way to find all the child threads of the main/parent thread.



package com.test.thread;

class MyMoreThreads implements Runnable {

    String threadName;

    public MyMoreThreads(String threadName) {
        this.threadName = threadName;
        new Thread(this, this.threadName).start();
    }

    @SuppressWarnings("static-access")
    @Override
    public void run() {

        for (int i = 0; i < 10; i++) {
            System.out.println(this.threadName + " : " + i);
            try {
                Thread.currentThread().sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

}

public class MoreThreadsExample {

    /**
     * @param args
     */
    public static void main(String[] args) {

        for (int i = 0; i < 5; i++) {
            new MyMoreThreads("Child-Thread-" + i);
        }

        for (int i = 0; i < 10; i++) {
            System.out.println(Thread.currentThread().getName() + " : " + i);
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

}

Load More

No More Comments