Java Thread checkAccess()方法

2019-10-16 22:24:42

Thread類的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