下面的例子顯示java.util.Collections.synchronizedSet()方法的使用
package com.; import java.util.*; public class CollectionsDemo { public static void main(String[] args) { // create set Set<String> set = new HashSet<String>(); // populate the set set.add("TP"); set.add("IS"); set.add("FOR"); set.add("TECHIES"); // create a synchronized set Set<String> synset = Collections.synchronizedSet(set); System.out.println("Synchronized set is :"+synset); } }
現在編譯和運行上面的代碼示例,將產生以下結果。
Synchronized set is :[FOR, IS, TECHIES, TP]