Home » Java programming language

Java Thread Class final void checkAccess() method with Example

Java Thread Class final void checkAccess() method: Here, we are going to learn about the final void checkAccess() method of Thread class with its syntax and example.
Submitted by Preeti Jain, on July 17, 2019

Thread Class final void checkAccess()

  • This method is available in package java.lang.Thread.checkAccess().
  • This method is used to check whether currently executing thread has permission to modify or not.
  • This method is final so we are not allowed to override this method in our class.
  • The return type of this method is void, it does not return anything.
  • This method does throw SecurityException if calling thread does not have permission to modify the thread and throw NullPointerException, if the thread parameter is null.

Syntax:

    final void checkAccess(){
    }

Parameter(s):

We don't pass any object as a parameter in the method of the File.

Return value:

The return type of this method is void, it does not return anything.

Java program to demonstrate example of checkAccess()() method

/*  We will use Thread class methods 
so we are importing the package but it is not 
mandate because it is imported by default
*/
import java.lang.Thread;

public class MainThread extends Thread {
	public static void main(String[] args) throws Exception {
		// creating two thread
		MainThread mt1 = new MainThread();
		MainThread mt2 = new MainThread();

		// By using checkAccess() method is used to 
		// check whether current thread has permission 
		// to modify the thread or not
		mt1.checkAccess();
		mt2.checkAccess();

		// Display current thread name with permission
		System.out.println(mt1.getName() + " has access");
		System.out.println(mt2.getName() + " has access");
	}
}

Output

E:\Programs>javac MainThread.java

E:\Programs>java MainThread
Thread-0 has access
Thread-1 has access


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.