Skip to main content

Multi-threading enhancement

ThreadGroup:

Based on the functionality we can group thread into a single unit which is nothing but a thread group i.e a thread group contains a group of threads.
In addition to that, it also contains a sub-thread group





The main advantage of maintaining threads in the form of the thread group is we can perform common operations very easily.

Note:-1. Every thread in java belong to some group, main thread belong to the main group
           2. Every thread group in java is the child group of the system group either directly or indirectly, hence a system group act as root/parent for all thread groups.

System group contains several system levels thread:
 1. finalizer
 2. Reference handler
 3. Attach Listener
 4. Signal Dispatcher, etc.

Program to illustrate that every thread in java belongs to some group and directly or indirectly child of system group:

 import java.lang.*;
class ThreadGroupDemo1{
   public static void main(String args[])
     {
       System.out.println(Thread.currentThread().getThreadGroup().getName());
      System.out.println(Thread.currentThread().getThreadGroup().getParent().getName());
     }
}


Note:-ThreadGroup is a java class which is present in java.lang package and it is the direct child class of object.

Constructors:

  • ThreadGroup g=new ThreadGroup(String name)- create a new thread group with the specified group name. The parent of these new groups is the thread group of currently executing threads.
  • ThreadGroup g=new ThreadGroup(ThreadGroup g, String name)-create a new thread group with the specified group name the parent of this new thread group is specified parent group.

Methods:

  • String getName()- return the name of the thread group.
  • int getMaxPriority()- return maximum priority of thread group.
  • void setMaxPriority()- To set the maximum priority of the thread group. The default maximum priority is 10.
  Note-Threads in the thread group that already have higher priority won't be affected but for the newly added thread, this maximum priority is applicable.
  • void list()- it will print information about the thread group to the console.
  • int activeCount()- return the number of active thread present in a thread group.

Programs:

import java.lang.*;
import java.util.*;
class ThreadGroupDemo2 extends Thread
{
  public static void main(String args[])
   {
    ThreadGroup g=new ThreadGroup("tg");
     Thread t1=new Thread(g,"Thread 1");
     Thread t2=new Thread(g,"Thread 2");
      g.setMaxPriority(3);//setting priority for ThreadGroup
     Thread t3=new Thread(g,"Thread 3");
      System.out.println("Default Priority t1: "+t1.getPriority());
      System.out.println("Default Priority t2: "+t2.getPriority());
      System.out.println("Default Priority t3: "+t3.getPriority());
   }
}



class MyThread extends Thread 
{
  MyThread(ThreadGroup g,String name)
    {
       super(g,name);
    }
 public void run()
   {
      System.out.println("child Thread");
      try{Thread.sleep(5000);}catch(Exception e){}
    }
 }
class ThreadGroupDemo3
{
   public static void main(String args[])throws Exception
    {
      ThreadGroup p=new ThreadGroup("Parent");
      ThreadGroup g=new ThreadGroup(p,"child group");
      MyThread t1=new MyThread(g,"childThread1");
      MyThread t2=new MyThread(g,"childThread2");
       t1.start();
       t2.start();
      System.out.println("Thread count: "+p.activeCount()); //2
      System.out.println("Group count: "+p.activeGroupCount());//1
      System.out.println("child group: "+g.activeGroupCount());//0
      p.list();
      Thread.sleep(10000);
      System.out.println("Thread count: "+p.activeCount()); 
      System.out.println("Group count: "+p.activeGroupCount());
      System.out.println("child group: "+g.activeGroupCount());
   }
}
     
        


import java.lang.*;
class ThreadGroupDemo4 extends Thread
{
  public static void main(String args[])
   {
     ThreadGroup system=Thread.currentThread().getThreadGroup().getParent();
  Thread[] t=new Thread[system.activeCount()];
   system.enumerate(t);
   for(Thread t1:t)
     System.out.println(t1.getName()+"...."+t1.isDaemon());
   }
}










Comments

Popular posts from this blog

DeadLock

What is DeadLock? if two thread executing on two different objects.  Let's assume thread t1,t2 and objects ob1 ,ob2   t1 got the lock of object ob1.   t2 got the lock of object ob2. let's assume the situation when t1 wann a execute method which is present in ob2(t2 has a lock on ob2) and  t2 wanna execute method which is present in ob1(t1 has a lock on ob1).  Note-Both methods are synchronized.     Thread t1 asked t2 to release the lock of object ob2. So that t1 can execute the method present in ob2. But t2 disagree to release the lock of object ob2 and t2 asked t1 the same thing what t1 has asked for t2 to do.t1 also disagrees with t2. Both threads not to release lock is simple stubbornness(zidd in Hindi). This is the only reason for infinite waiting which is called deadlock. Points about DeadLock: If two thread is waiting for each other forever. Such type of infinite waiting is called DeadLock. Synchronized Keyword is the only reason for the DeadLock...

Deep Analysis of Algorithm(part 1)

 What is the need for speed in the algorithm? Suppose there is the software that provides lots of features including security but it takes almost 30 min to perform one task. And on the other hand, there is the software that provides you the same feature but it takes 2 min to perform one task. which one would you prefer? This is the reason why we should design an algorithm that is faster. Given two algorithms for a task, how do we find out which one is better? One naive way of doing this is – implement both the algorithms and run the two programs on your computer for different inputs and see which one takes less time. There are many problems with this approach for the analysis of algorithms. 1) It might be possible that for some inputs, the first algorithm performs better than the second. And for some inputs second performs better. 2) It might also be possible that for some inputs, the first algorithm performs better on one machine and the second works better on other machines for s...

Multi-threading enhancement

java.util.concurrent.lock package{1.5 version} Problem with tradition synchronized  keyword: We are not having any flexibility to try for a lock without waiting. There is no way to specify the maximum waiting   time for a thread to get the lock whereas in tradition synchronized thread will wait until getting the lock which may cause performance problems or deadlock. In  tradition synchronized,  if a thread releases the lock then which waiting thread will get that lock we are not having control over this To overcome this problem sun organization introduces java.util.lock package in JDK 1.5v. It also provides several enhancement to the programmers.  Lock Interface: Lock object is similar to the implicit lock to execute synchronized methods or blocks. Lock interface provides more extensive operation than traditional lock(synchronized). Important methods of Lock interface: void lock() - we can use this method to acquire a lock.       Different Cases:...