Java - ThreadGroup checkAccess() 方法



描述

Java ThreadGroup checkAccess() 方法用于确定当前正在运行的线程是否具有修改此线程组的权限。

声明

以下是 java.lang.ThreadGroup.checkAccess() 方法的声明

public void checkAccess()

参数

返回值

此方法不返回值。

异常

SecurityException - 如果当前线程不允许访问此线程组。

检查 ThreadObject 对象访问权限示例

以下示例展示了在单个 ThreadGroup 对象的情况下使用 ThreadGroup checkAccess() 方法。我们创建了一个 ThreadGroup 对象并为其分配了一个名称。然后,我们使用之前创建的 ThreadGroup 对象创建了两个线程。使用 checkAccess() 方法,我们打印 ThreadGroup 对象的访问权限。

package com.tutorialspoint;

public class ThreadGroupDemo implements Runnable {
   public static void main(String[] args) {
      ThreadGroupDemo tg = new ThreadGroupDemo();
      tg.start();
   }

   public void start() {
      try {     
         // create a ThreadGroup
         ThreadGroup threadGroup = new ThreadGroup("ThreadGroup");

         // create a thread
         Thread t1 = new Thread(threadGroup, this);
         System.out.println("Starting " + t1.getName() + "...");
         t1.start();
            
         // create another thread
         Thread t2 = new Thread(threadGroup, this);
         System.out.println("Starting " + t2.getName() + "...");
         t2.start();
            
         // checking the access for the ThreadGroup.
         try {
        	threadGroup.checkAccess();
            System.out.println(threadGroup.getName() + " has access" );
         } catch (SecurityException ex) {
            System.out.println("No access: " + ex.toString());
         }

         // block until the other threads finish
         t1.join();
         t2.join();
      } catch (InterruptedException ex) {
         System.out.println(ex.toString());
      }
   }

   // implements run()
   public void run() {

      for(int i = 0; i < 4;i++) {
         i++;
         try {
			Thread.sleep(50);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
      }
      System.out.println(Thread.currentThread().getName() + " finished executing.");
   }
} 

让我们编译并运行以上程序,这将产生以下结果:

Starting Thread-0...
Starting Thread-1...
ThreadGroup has access
Thread-0 finished executing.
Thread-1 finished executing.

在多个 ThreadGroup 对象中检查 ThreadObject 对象访问权限示例

以下示例展示了在多个 ThreadGroup 对象的情况下使用 ThreadGroup checkAccess() 方法。我们创建了一个 ThreadGroup 对象并为其分配了一个名称。接下来,我们创建了一个子 ThreadGroup 对象。然后,我们使用之前创建的 ThreadGroup 对象创建了两个线程。使用 checkAccess() 方法,我们打印两个 ThreadGroup 对象的访问权限。

package com.tutorialspoint;

public class ThreadGroupDemo implements Runnable {
   public static void main(String[] args) {
      ThreadGroupDemo tg = new ThreadGroupDemo();
      tg.start();
   }

   public void start() {
      try {     
         // create a parent ThreadGroup
         ThreadGroup pThreadGroup = new ThreadGroup("Parent ThreadGroup");
		 
         // create a child ThreadGroup for parent ThreadGroup
         ThreadGroup cThreadGroup = new ThreadGroup(pThreadGroup, "Child ThreadGroup");

         // create a thread
         Thread t1 = new Thread(pThreadGroup, this);
         System.out.println("Starting " + t1.getName() + "...");
         t1.start();
            
         // create another thread
         Thread t2 = new Thread(cThreadGroup, this);
         System.out.println("Starting " + t2.getName() + "...");
         t2.start();
            
         // checking the access for these ThreadGroups.
         try {
        	pThreadGroup.checkAccess();
            System.out.println(pThreadGroup.getName() + " has access" );
        	cThreadGroup.checkAccess();
            System.out.println(cThreadGroup.getName() + " has access" );
         } catch (SecurityException ex) {
            System.out.println("No access: " + ex.toString());
         }

         // block until the other threads finish
         t1.join();
         t2.join();
      } catch (InterruptedException ex) {
         System.out.println(ex.toString());
      }
   }

   // implements run()
   public void run() {

      for(int i = 0; i < 4;i++) {
         i++;
         try {
			Thread.sleep(50);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
      }
      System.out.println(Thread.currentThread().getName() + " finished executing.");
   }
} 

让我们编译并运行以上程序,这将产生以下结果:

Starting Thread-0...
Starting Thread-1...
Parent ThreadGroup has access
Child ThreadGroup has access
Thread-0 finished executing.
Thread-1 finished executing.

在子/孙对象情况下检查 ThreadObject 对象访问权限示例

以下示例展示了在子 ThreadGroup 对象和孙 ThreadGroup 对象的情况下使用 ThreadGroup checkAccess() 方法。我们创建了一个 ThreadGroup 对象并为其分配了一个名称。接下来,我们创建了一个子 ThreadGroup 对象。然后,我们使用之前创建的子 ThreadGroup 对象和孙 ThreadGroup 对象创建了两个线程。使用 checkAccess() 方法,我们打印所有 ThreadGroup 对象的访问权限。

package com.tutorialspoint;

public class ThreadGroupDemo implements Runnable {
   public static void main(String[] args) {
      ThreadGroupDemo tg = new ThreadGroupDemo();
      tg.start();
   }

   public void start() {
      try {     
         // create a parent ThreadGroup
         ThreadGroup pThreadGroup = new ThreadGroup("Parent ThreadGroup");
		 
         // create a child ThreadGroup for parent ThreadGroup
         ThreadGroup cThreadGroup = new ThreadGroup(pThreadGroup, "Child ThreadGroup");
		 
         // create a grandchild ThreadGroup for parent ThreadGroup
         ThreadGroup gThreadGroup = new ThreadGroup(cThreadGroup, "GrandChild ThreadGroup");

         // create a thread
         Thread t1 = new Thread(cThreadGroup, this);
         System.out.println("Starting " + t1.getName() + "...");
         t1.start();
            
         // create another thread
         Thread t2 = new Thread(gThreadGroup, this);
         System.out.println("Starting " + t2.getName() + "...");
         t2.start();
            
         // checking the access for these ThreadGroups.
         try {
        	pThreadGroup.checkAccess();
            System.out.println(pThreadGroup.getName() + " has access" );
        	cThreadGroup.checkAccess();
            System.out.println(cThreadGroup.getName() + " has access" );
			gThreadGroup.checkAccess();
            System.out.println(gThreadGroup.getName() + " has access" );
         } catch (SecurityException ex) {
            System.out.println("No access: " + ex.toString());
         }

         // block until the other threads finish
         t1.join();
         t2.join();
      } catch (InterruptedException ex) {
         System.out.println(ex.toString());
      }
   }

   // implements run()
   public void run() {
      System.out.println(Thread.currentThread().getName() + " started executing.");
      for(int i = 0; i < 4;i++) {
         i++;
         try {
			Thread.sleep(50);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
      }
      System.out.println(Thread.currentThread().getName() + " finished executing.");
   }
} 

让我们编译并运行以上程序,这将产生以下结果:

Starting Thread-0...
Starting Thread-1...
Parent ThreadGroup has access
Child ThreadGroup has access
GrandChild ThreadGroup has access
Thread-0 finished executing.
Thread-1 finished executing.
java_lang_threadgroup.htm
广告