Java checkAccess()方法
线程类的
activeCount()方法确定当前正在运行的线程是否有权修改该线程。
语法
public final void checkAccess()
返回
它不返回任何值。
异常
SecurityException: 如果不允许当前线程访问该线程,则抛出此异常。
示例
public class JavaCheckAccessExp extends Thread
{
public void run()
{
System.out.println(Thread.currentThread().getName()+" finished executing");
}
public static void main(String arg[]) throws InterruptedException, SecurityException
{
// creating the thread
JavaCheckAccessExp t1 = new JavaCheckAccessExp();
JavaCheckAccessExp t2 = new JavaCheckAccessExp();
// this will call the run() method
t1.start();
t2.start();
// Check for access permission of current running thread
t1.checkAccess();
System.out.println(t1.getName() + " has access");
t2.checkAccess();
System.out.println(t2.getName() + " has access");
}
}
输出:
Thread-0 has access
Thread-1 has access
Thread-0 finished executing
Thread-1 finished executing