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
廣告