Monday, 16 January 2017

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


Click here to watch in Youtube :
https://www.youtube.com/watch?v=r_CVKSKFepo&list=UUhwKlOVR041tngjerWxVccw

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:
https://sites.google.com/site/ramj2eev1/home/javabasics/ThreadDemo_ThreadGroup_App.zip?attredirects=0&d=1

Github Link:
https://github.com/ramram43210/Java/tree/master/BasicJava/ThreadDemo_ThreadGroup_App

Bitbucket Link:
https://bitbucket.org/ramram43210/java/src/0e65b153b8d122db36492a0722000b1e2ed1fbd9/BasicJava/ThreadDemo_ThreadGroup_App/?at=master

See also:
  • All JavaEE Viedos Playlist
  • All JavaEE Viedos
  • All JAVA EE Links
  • Servlets Tutorial
  • All Design Patterns Links
  • JDBC Tutorial
  • Java Collection Framework Tutorial
  • JAVA Tutorial
  • Kids Tutorial
  • No comments:

    Post a Comment