「Java」synchronized staticメソッドで排他制御をするサンプル

書式
public synchronized static List<クラス名> 関数名()
クラス名.synchronized関数名
使用例
1.クラスのsynchronized staticメソッドの定義

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
package com.arkgame.study;
import java.util.Arrays;
import java.util.List;
public class SampleA {
protected String key;
protected String Value;
public SampleA(String key, String value) {
this.key = key;
this.Value = value;
}
//synchronized staticメソッドの定義
public synchronized static List<SampleA> getElementFunc() {
List<SampleA> lsA = Arrays.asList(
new SampleA("key1", "101 value"),
new SampleA("key2", "202 value"),
new SampleA("key3", "303 value"),
new SampleA("key4", "404 value"));
return lsA;
}
}
package com.arkgame.study; import java.util.Arrays; import java.util.List; public class SampleA { protected String key; protected String Value; public SampleA(String key, String value) { this.key = key; this.Value = value; } //synchronized staticメソッドの定義 public synchronized static List<SampleA> getElementFunc() { List<SampleA> lsA = Arrays.asList( new SampleA("key1", "101 value"), new SampleA("key2", "202 value"), new SampleA("key3", "303 value"), new SampleA("key4", "404 value")); return lsA; } }
package com.arkgame.study;

import java.util.Arrays;
import java.util.List;

public class SampleA {

      protected String key;
      protected String Value;

      public SampleA(String key, String value) {
            this.key = key;
            this.Value = value;
      }

      //synchronized staticメソッドの定義
      public synchronized static List<SampleA> getElementFunc() {
            List<SampleA> lsA = Arrays.asList(
                        new SampleA("key1", "101 value"),
                        new SampleA("key2", "202 value"),
                        new SampleA("key3", "303 value"),
                        new SampleA("key4", "404 value"));
            return lsA;

      }
}

2.動作確認クラスの定義

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
package com.arkgame.study;
import java.util.List;
public class CftInfo {
public static void main(String[] args) {
List<SampleA> resLst;
//synchronized staticメソッドを呼び出す
resLst = SampleA.getElementFunc();
printMsg(resLst);
}
public static void printMsg(List<SampleA> resLst) {
for (SampleA sa : resLst) {
System.out.println("キー:" + sa.key + " 値: " + sa.Value);
}
}
}
package com.arkgame.study; import java.util.List; public class CftInfo { public static void main(String[] args) { List<SampleA> resLst; //synchronized staticメソッドを呼び出す resLst = SampleA.getElementFunc(); printMsg(resLst); } public static void printMsg(List<SampleA> resLst) { for (SampleA sa : resLst) { System.out.println("キー:" + sa.key + " 値: " + sa.Value); } } }
package com.arkgame.study;

import java.util.List;

public class CftInfo {

      public static void main(String[] args) {
            List<SampleA> resLst;
            //synchronized staticメソッドを呼び出す
            resLst = SampleA.getElementFunc();
            printMsg(resLst);

      }

      public static void printMsg(List<SampleA> resLst) {

            for (SampleA sa : resLst) {
                  System.out.println("キー:" + sa.key + " 値: " + sa.Value);
            }
      }

}

実行結果
キー:key1 値: 101 value
キー:key2 値: 202 value
キー:key3 値: 303 value
キー:key4 値: 404 value

Java

Posted by arkgame