用法:
public void checkAccess (Thread th); public void checkAccess (ThreadGroup tg);
SecurityManager類checkAccess()方法
- checkAccess(Thread th) 方法由線程類 stop()、suspend()、resume()、setName() 和 setDaemon() 的這些方法調用當前安全管理器。
- checkAccess(ThreadGroup tg) 方法調用當前安全管理器通過使用 ThreadGroup 類的這些方法(如 setDaemon()、stop()、resume()、suspend() 和 destroy())在線程組上創建新的子線程。
- checkAccess(線程th)、checkAccess(ThreadGroup tg)方法在修改時可能會拋出異常。SecurityException:當調用線程不允許修改Thread或ThreadGroup時可能會拋出這個異常。
- 這些方法是非靜態方法,隻能通過類對象訪問,如果我們嘗試使用類名訪問這些方法,則會出現錯誤。
參數:
- 在第一種情況下,
Thread th
- 該參數表示要檢查的線程。 - 在第二種情況下,
ThreadGroup tg
- 此參數表示要檢查的線程組。
返回值:
這個方法的返回類型是void
,它什麽都不返回。
例:
// Java program to demonstrate the example
// of checkAccess () method of SecurityManager class
public class CheckAccess extends SecurityManager {
// Override checkAcess(Thread th) of SecurityManager class
public void checkAccess(Thread th) {
throw new SecurityException("Restricted...");
}
// Override checkAcess(ThreadGroup tg) of SecurityManager //class
public void checkAccess(ThreadGroup tg) {
throw new SecurityException("Restricted...");
}
public static void main(String[] args) {
ThreadGroup tg1 = new ThreadGroup("New Thread Group");
// By using setProperty() method is to set the policy property
// with security manager
System.setProperty("java.security.policy", "file:/C:/java.policy");
// Instantiating a CheckAccept object
CheckAccess ca = new CheckAccess();
// By using setSecurityManager() method is to set the
// security manager
System.setSecurityManager(ca);
// By using checkAccess(Thread th) method is to check that
// current thread is enabled for access or not
ca.checkAccess(Thread.currentThread());
// By using checkAccess(ThreadGroup tg) method is to check
// that current thread group is enabled for access or not
ca.checkAccess(tg1);
// Display the message when thread is enabled
System.out.println("Not Restricted..");
}
}
輸出
Exception in thread "main" java.lang.SecurityException:Restricted... at CheckAccess.checkAccess(CheckAccess.java:5) at CheckAccess.main(CheckAccess.java:30)
相關用法
- Java SecurityManager checkAccept()用法及代碼示例
- Java SecurityManager checkAwtEventQueueAccess()用法及代碼示例
- Java SecurityManager checkPackageAccess()用法及代碼示例
- Java SecurityManager checkPrintJobAccess()用法及代碼示例
- Java SecurityManager checkSystemClipboardAccess()用法及代碼示例
- Java SecurityManager checkPropertiesAccess()用法及代碼示例
- Java SecurityManager checkLink()用法及代碼示例
- Java SecurityManager checkWrite()用法及代碼示例
- Java SecurityManager checkPropertyAccess()用法及代碼示例
- Java SecurityManager checkSetFactory()用法及代碼示例
- Java SecurityManager checkSecurityAccess()用法及代碼示例
- Java SecurityManager checkPermission()用法及代碼示例
- Java SecurityManager checkRead()用法及代碼示例
- Java SecurityManager checkTopLevelWindow()用法及代碼示例
- Java SecurityManager checkDelete()用法及代碼示例
- Java SecurityManager checkConnect()用法及代碼示例
- Java SecurityManager checkMemberAccess()用法及代碼示例
- Java SecurityManager checkListen()用法及代碼示例
- Java SecurityManager checkMulticast()用法及代碼示例
- Java SecurityManager checkExec()用法及代碼示例
注:本文由純淨天空篩選整理自Preeti Jain大神的英文原創作品 Java SecurityManager checkAccess() method with example。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。