Saturday, 10 June 2017

Java Tutorial: Java Threads (Thread group in java | Java Thread group) ~ foundjava


Click here to watch in Youtube :

Click the below Image to Enlarge
Java Tutorial: Java Threads (Thread group in java | Java Thread group) 
Java Tutorial: Java Threads (Thread group in java | Java Thread group) 
MyRunnable.java
public class MyRunnable implements Runnable
{
    public void run()
    {
        System.out.println("Run by = "+Thread.currentThread().getName());
    }
}
ThreadGroupDemo.java
public class ThreadGroupDemo
{

    public static void main(String[] args) throws InterruptedException
    {
        MyRunnable myRunnable = new MyRunnable();

        ThreadGroup tg = new ThreadGroup("Group A");

        Thread t1 = new Thread(tg, myRunnable, "thread1");
        Thread t2 = new Thread(tg, myRunnable, "thread2");
        Thread t3 = new Thread(tg, myRunnable, "thread3");

        t1.start();
        t2.start();
        t3.start();

        /*
         * Returns the name of this thread group.
         */
        String threadGroupName = tg.getName();
        System.out.println("Thread Group Name: " + threadGroupName);
        /*
         * Prints information about this thread group to the
         * standard output.
         */
        tg.list();

    }
}
Output
Thread Group Name: Group A
Run by = thread3
java.lang.ThreadGroup[name=Group A,maxpri=10]
Run by = thread2
Run by = thread1
    Thread[thread1,5,Group A]
    Thread[thread2,5,Group A]
    Thread[thread3,5,Group A]

Refer: 
https://docs.oracle.com/javase/8/docs/api/index.html?java/lang/ThreadGroup.html
Click the below link to download the code:

CLICK HERE

No comments:

Post a Comment