import java.lang.*;

class NewThread extends Thread 

{

    NewThread(String threadname, ThreadGroup tgob)

    {

        super(tgob, threadname);

    }

public void run()

    {

 

        for (int i = 0; i < 10; i++) 

        {

            try

            {

                Thread.sleep(10);

            }

            catch (InterruptedException ex) 

            {

              System.out.println("Thread " + Thread.currentThread().getName()

                                   + " interrupted");

            }

        }

        System.out.println(Thread.currentThread().getName() + 

                 " [priority = "

           Thread.currentThread().getPriority() + "]

                finished executing.");

    }

public class ThreadGroupDemo 

{

    public static void main(String arg[]) throws InterruptedException,

        SecurityException, Exception

    {

        

        ThreadGroup gfg = new ThreadGroup("Parent thread");

        ThreadGroup gfg_child = new ThreadGroup(gfg, "child thread");

        gfg.setMaxPriority(Thread.MAX_PRIORITY - 2);

        gfg_child.setMaxPriority(Thread.NORM_PRIORITY);

 

        NewThread t1 = new NewThread("one", gfg);

        t1.setPriority(Thread.MAX_PRIORITY);

        System.out.println("Starting " + t1.getName());

        t1.start();

        NewThread t2 = new NewThread("two", gfg_child);

        t2.setPriority(Thread.MAX_PRIORITY);

        System.out.println("Starting " + t2.getName());

        t2.start();

    }

}

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *