Home Tutorial Java Core Java Thread checkAccess

 
 

Share on Google+Share on Google+
Java Thread checkAccess
Posted on: November 8, 2012 at 12:00 AM
Advertisement
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.

Advertisement

Related Tags for Java Thread checkAccess :


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: November 8, 2012

Recommend the tutorial

Advertisements Advertisements
 

 

 

DMCA.com