Modifier isSynchronized(mod) method in Java with Examples
The isSynchronized(mod) method of java.lang.reflect.Modifier is used to check if the integer argument includes the synchronized modifier or not. If this integer parameter represents synchronized type Modifier then method returns true else false.
Syntax:
public static boolean isSynchronized(int mod)
Parameters: This method accepts a integer names as mod represents a set of modifiers.
Return: This method returns true if mod includes the synchronized modifier; false otherwise.
Below programs illustrate isSynchronized() method:
Program 1:
// Java program to illustrate isSynchronized() method import java.lang.reflect.*; public class GFG { public static void main(String[] args) throws NoSuchFieldException, SecurityException { // get Method class object Method[] methods = GFGTest. class .getMethods(); // get Modifier Integer value int mod = methods[ 0 ].getModifiers(); // check Modifier is synchronized or not boolean result = Modifier.isSynchronized(mod); System.out.println( "Mod integer value " + mod + " is synchronized : " + result); } class GFGTest { public synchronized String method1() { return null ; } } } |
Output:
Mod integer value 33 is synchronized : true
Program 2:
// Java program to illustrate isSynchronized() import java.lang.reflect.*; public class GFG { public static void main(String[] args) throws NoSuchFieldException, SecurityException { // get Method class object Method[] methods = Thread. class .getMethods(); // loop through methods and // print synchronized methods for ( int i = 0 ; i < methods.length; i++) { // get Modifier Integer value int mod = methods[i].getModifiers(); // check Modifier is synchronized or not boolean result = Modifier.isSynchronized(mod); if (result) { System.out.println( "synchronized Method: " + methods[i]); } } } } |
Output:
synchronized Method: public final synchronized void java.lang.Thread.join(long) throws java.lang.InterruptedException synchronized Method: public final synchronized void java.lang.Thread.join(long, int) throws java.lang.InterruptedException synchronized Method: public synchronized void java.lang.Thread.start() synchronized Method: public final synchronized void java.lang.Thread.stop(java.lang.Throwable) synchronized Method: public final synchronized void java.lang.Thread.setName(java.lang.String)
References: https://docs.oracle.com/javase/10/docs/api/java/lang/reflect/Modifier.html#isSynchronized(int)
Please Login to comment...