Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • ThreadGroup Class

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 179
    Comment on it

    ThreadGroup in Java:

    It represents combination of threads within a group. A thread group can also include other thread groups .With the help of thread group we can easily suspend, resume or interrupt group of threads by a single method call.

     

    It is implemented by java.lang.ThreadGroup class.

    Example:

        public class ThreadDemo implements Runnable{  
            public void run() {  
                  System.out.println(Thread.currentThread().getName());  
            }  
           public static void main(String[] args) {  
              ThreadDemo runnable = new ThreadDemo();  
                  ThreadGroup tg1 = new ThreadGroup("Parent ThreadGroup"); //creating new group named Parent ThreadGroup 
                    
                  Thread t1 = new Thread(tg1, runnable,"one");  
                  t1.start();  
                  Thread t2 = new Thread(tg1, runnable,"two");  
                  t2.start();  
                  Thread t3 = new Thread(tg1, runnable,"three");  
                  t3.start();  
                       
                  System.out.println("Thread Group Name: "+tg1.getName());  
                 tg1.list();  
          
            }  
           }  

    Output:

    one
    two
    three
    Thread Group Name: Parent ThreadGroup
    java.lang.ThreadGroup[name=Parent ThreadGroup,maxpri=10]
        Thread[one,5,Parent ThreadGroup]
        Thread[two,5,Parent ThreadGroup]
        Thread[three,5,Parent ThreadGroup]

    In the above program we are creating a new thread group named Parent ThreadGroup which contains three threads (One,Two and Three) with priority 5.


    By default  Java runtime system creates a ThreadGroup named main. Unless specified , all new threads that are created become members of the main thread group.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: