How to create a group of a thread in Java

1 Answer

0 votes
class MyThread extends Thread {
    MyThread(String threadname, ThreadGroup tgroup) {
        super(tgroup, threadname);
        start();
    }
    public void run() {
        System.out.println(Thread.currentThread().getName() + " is running");
    }
}

class Program {
    public static void main(String[] args) {
        try {
            ThreadGroup group = new ThreadGroup("Parent thread");

            MyThread trd1 = new MyThread("Child Thread1", group);
            MyThread trd2 = new MyThread("Child Thread2", group);
            MyThread trd3 = new MyThread("Child Thread3", group);

        } catch (Exception e) {
            System.out.println("Exception: " + e);
        }
    }
}



/*
run:

Child Thread3 is running
Child Thread1 is running
Child Thread2 is running

*/

 



answered Feb 2, 2024 by avibootz

Related questions

1 answer 168 views
1 answer 161 views
1 answer 198 views
1 answer 209 views
1 answer 185 views
185 views asked Feb 2, 2024 by avibootz
...