當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Java Modifier isSynchronized(mod)用法及代碼示例


java.lang.reflect.Modifier的isSynchronized(mod)方法用於檢查整數參數是否包含同步修飾符。如果此整數參數表示同步類型Modifier,則方法返回true,否則返回false。

用法:

public static boolean isSynchronized(int mod)

參數:此方法接受整數名稱,因為mod代表一組修飾符。


返回:如果mod包含synced修飾符,則此方法返回true;否則,此方法返回true。否則為假。

以下示例程序旨在說明isSynchronized()方法:
示例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; 
        } 
    } 
}
輸出:
Mod integer value 33 is synchronized : true

示例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]); 
            } 
        } 
    } 
}
輸出:
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)

參考文獻: https://docs.oracle.com/javase/10/docs/api/java/lang/reflect/Modifier.html#isSynchronized(int)



相關用法


注:本文由純淨天空篩選整理自AmanSingh2210大神的英文原創作品 Modifier isSynchronized(mod) method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。