Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/312.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
断线程组java api_Java_Multithreading_Threadgroup - Fatal编程技术网

断线程组java api

断线程组java api,java,multithreading,threadgroup,Java,Multithreading,Threadgroup,我正在浏览java中的线程组。根据Javadoc,它写道 允许线程访问有关其自身线程组的信息,但 不访问其线程组的父线程组或 任何其他线程组 但是当我实现下面的代码时,它正在工作 public static void main(String args[]){ //parent thread group It_Firm ThreadGroup It_Firm=new ThreadGroup("It_Firm"); //Child thread group web

我正在浏览java中的线程组。根据Javadoc,它写道

允许线程访问有关其自身线程组的信息,但 不访问其线程组的父线程组或 任何其他线程组

但是当我实现下面的代码时,它正在工作

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();
}

现在我不明白发生了什么,我是新手,为什么我能够获得关于当前线程的线程组的父线程的信息。我是否遗漏了一些信息?

我相信文档想要说的是,当线程组(thread ThreadGroup)发生故障时,不会进行安全检查,也就是说,它不能出现安全异常:

但是,当线程组发生故障时,会进行安全检查,即它可能会因SecurityException而失败:

只有在尝试访问根线程组(默认线程组)时,默认SecurityManager才会检查modifyThreadGroup权限:

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
    }
}
但您可以使用安全管理器来覆盖该方法


请参见

请添加注释,或指定代码中与您的问题相关的部分
public final ThreadGroup getParent() {
    if (parent != null)
        parent.checkAccess();
    return parent;
}
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
    }
}