0

I was going though ThreadGroups in java. As per the Javadoc, it is written that,

A thread is allowed to access information about its own thread group but not to access information about its thread group’s parent thread group or any other thread group.

But when I implemented the following code it was working,

public static void main(String args[]){
    //parent thread group It_Firm

    ThreadGroup It_Firm=new ThreadGroup("It_Firm");

    //Child thread group web

    ThreadGroup web=new ThreadGroup(It_Firm,"webdeveloper");

    /*
     * A thread entry in child thread group set in which i am trying to call parent's thread group activecount()   
     * method,as per the docs it will stop me to call for any information from parent's thread group or any other 
     * thread group but it  is not doing it.
    */

    Thread th=new Thread(web,new Runnable(){
        @Override
        public void run() {
            while(true){
                try {
                    Thread.sleep(500);
                    Thread ths[]=new Thread[Thread.currentThread().getThreadGroup().getParent().activeCount()];
                    Thread.currentThread().getThreadGroup().getParent().enumerate(ths);
                                    for(int i=0;i<ths.length;i++){
                        System.out.println("group name"+ths[i].getThreadGroup().getName()+" : name : "+ths[i].getName());
                        System.out.println("state"+ths[i].isAlive());
                    }

                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }

    },"prashank");

    th.start();

    //some dummy code in parent thread group 

    Thread th_pthread=new Thread(It_Firm,new Runnable(){

        @Override
        public void run() {
            boolean flag=true;
            while(flag){
                Scanner sc=new Scanner(System.in);
                char ch=sc.nextLine().charAt(0);
                if(ch=='N')
                    flag=false;
            }
        }

    },"abc pvt ltd");
    th_pthread.start();
}

Now I can't understand what is happening, I am new to this, why i am able to get information about current thread's thread group's parent. Am i missing something,any information on this?

Ajay Kumar
  • 71
  • 8
Thinker
  • 518
  • 1
  • 6
  • 22

1 Answers1

1

I believe what the documentation intends to say is that there's no security check when a thread accesses it's own ThreadGroup, i.e. it cannot fail with a SecurityException :

public final ThreadGroup getThreadGroup() {
    return group;
}

But there is a security check when accessing the parent of a ThreadGroup, i.e. it can fail with a SecurityException :

public final ThreadGroup getParent() {
    if (parent != null)
        parent.checkAccess();
    return parent;
}

The default SecurityManager will check for the modifyThreadGroup permission only when trying to access the root thread group (the default thread group):

public void checkAccess(ThreadGroup g) {
    if (g == null) {
        throw new NullPointerException("thread group can't be null");
    }
    if (g == rootGroup) {
        checkPermission(SecurityConstants.MODIFY_THREADGROUP_PERMISSION);
    } else {
        // just return
    }
}

But you can install your own security manager that can override the checkAccess(ThreadGroup g) method.

See Permissions in the Java Development Kit

Bax
  • 4,260
  • 5
  • 43
  • 65