ThreadGroup類checkAccess()方法
- checkAccess() 方法可在
java.lang
包。 - checkAccess() 方法用於檢查當前運行的線程是否具有更新線程組的權限。
- checkAccess() 方法不是靜態方法,因此不能通過類名訪問(即隻能通過類對象訪問此方法)。
- checkAccess() 方法是 final 方法,因此它不可重寫(即此方法在子類中不可重寫)。
- 該方法可能在訪問線程組時拋出異常。
SecurityException:在安全管理器存在時當前線程未被授權訪問線程組時的此異常。
用法:
public final void checkAccess();
參數:
- 此方法不接受任何參數。
返回值:
這個方法的返回類型是void
,它不返回任何東西。
例:
// Java program to demonstrate the example of
// checkAccess () method of ThreadGroup Class.
import java.lang.*;
class CheckAccess extends Thread {
// Override run() of Thread class
public void run() {
String name = Thread.currentThread().getName();
System.out.println(name + " " + "finish executing");
}
}
public class Main {
public static void main(String[] args) {
CheckAccess ca = new CheckAccess();
try {
// We are creating an object of ThreadGroup class
ThreadGroup tg1 = new ThreadGroup("ThreadGroup 1");
ThreadGroup tg2 = new ThreadGroup("ThreadGroup 2");
// We are creating an object of Thread class and
// we are assigning the ThreadGroup of both the thread
Thread th1 = new Thread(tg1, ca, "First Thread");
Thread th2 = new Thread(tg2, ca, "Second Thread");
// Calling start() method with Thread class object
// of Thread class
th1.start();
th2.start();
// Here we are checking access of ThreadGroup
try {
tg1.checkAccess();
System.out.println(tg1.getName() + " " + "has access");
tg2.checkAccess();
System.out.println(tg2.getName() + " " + "has access");
} catch (SecurityException se) {
System.out.println(se.getMessage());
}
th1.join();
th2.join();
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
}
}
輸出
E:\Programs>javac Main.java E:\Programs>java Main ThreadGroup 1 has access ThreadGroup 2 has access First Thread finish executing Second Thread finish executing
相關用法
- Java ThreadGroup enumerate()用法及代碼示例
- Java ThreadGroup getMaxPriority()用法及代碼示例
- Java ThreadGroup getParent()用法及代碼示例
- Java ThreadGroup getName()用法及代碼示例
- Java ThreadGroup parentOf()用法及代碼示例
- Java ThreadGroup interrupt()用法及代碼示例
- Java ThreadGroup suspend()用法及代碼示例
- Java ThreadGroup setDaemon()用法及代碼示例
- Java ThreadGroup isDaemon()用法及代碼示例
- Java ThreadGroup resume()用法及代碼示例
- Java ThreadGroup isDestroyed()用法及代碼示例
- Java ThreadGroup toString()用法及代碼示例
- Java ThreadGroup destroy()用法及代碼示例
- Java ThreadGroup list()用法及代碼示例
- Java ThreadGroup stop()用法及代碼示例
- Java ThreadGroup activeCount()用法及代碼示例
- Java ThreadGroup setMaxPriority()用法及代碼示例
- Java ThreadGroup activeGroupCount()用法及代碼示例
- Java Thread toString()用法及代碼示例
- Java Thread interrupted()用法及代碼示例
注:本文由純淨天空篩選整理自Preeti Jain大神的英文原創作品 Java ThreadGroup class checkAccess() method with example。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。