Java Thread checkAccess


 

Java Thread checkAccess

In this tutorial you will learn how to check permission of thread modification by using checkAccess () method.

In this tutorial you will learn how to check permission of thread modification by using checkAccess () method.

Java Thread checkAccess

In this tutorial you will learn how to check permission of thread modification by using checkAccess () method.

Thread checkAccess() :

Thread class provides you to check the permission of thread modification by using method checkAccess(). If there is a security manager the checkAccess() method throws a SecurityException if thread is not allowed to modify.

public final void checkAccess() : This method checks that the current executing thread has permission to modify the thread.

Throws : It throws SecurityException if the current thread don't have permission to access the thread.

Example : In this example we are checking that current thread has permission to apply changes to the thread by using checkAccess() method.

class CheckAccess implements Runnable {
	Thread thread;
	String str;

	public CheckAccess(String str) {
		this.str = str;
		thread = new Thread(this);
		thread.start();
	}

	public void run() {
		try {
			Thread.sleep(500);
		} catch (InterruptedException e) {

			e.printStackTrace();
		}
	}
}

public class ThreadCheckAccess {
	public static void main(String args[]) {
		new CheckAccess("One");
		Thread thread = Thread.currentThread();
		try {
			
			/* Check for access permission of current running thread */
			thread.checkAccess();
			System.out.println("You have permission to modify.");
		}
		
		catch (Exception e) {
			System.out.println(e);
		}

	}
}

Output :

You have permission to modify.

Ads