Java Collections 类的 synchronizedNavigableSet() 方法用于获取由指定的可导航集支持的同步(线程安全)可导航集。
用法
以下是 synchronizedNavigableSet() 方法的声明:
public static <T> NavigableSet<T> synchronizedNavigableSet(NavigableSet<T> s)
参数
参数 | 描述 | 必需/可选 |
---|---|---|
s | 它是将被包装在同步可导航集中的可导航集。 | Required |
返回
synchronizedNavigableSet() 方法返回指定 Navigable Set 的同步视图。
异常
NA
兼容版本
Java 1.8 及以上
例子1
import java.util.*;
public class CollectionsSynchronizedNavigableSetExample1 {
public static void main(String[] args) {
NavigableSet<String> set = new TreeSet<>();
set.add("Instagram");
set.add("JavaTpoint");
set.add("Facebook");
set.add("Google");
Set<String> synset = Collections.synchronizedNavigableSet(set);
System.out.println("Synchronized navigable set is:" + synset);
}
}
输出:
Synchronized navigable set is:[Facebook, Google, Instagram, JavaTpoint]
例子2
import java.util.*;
public class CollectionsSynchronizedNavigableSetExample2 {
public static void main(String[] args) {
NavigableSet<Integer> set = new TreeSet<>();
set.add(1001);
set.add(1002);
set.add(1003);
set.add(1004);
set.add(1005);
System.out.println("Set before Synchronized navigable set:" + set);
Set<Integer> synset = Collections.synchronizedNavigableSet(set);
set.remove(1004);
System.out.println("Synchronized navigable set after remove(1004):" + synset);
}
}
输出:
Set before Synchronized navigable set:[1001, 1002, 1003, 1004, 1005] Synchronized navigable set after remove(1004):[1001, 1002, 1003, 1005]
例子3
import java.util.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
public class CollectionsSynchronizedNavigableSetExample3 {
private static AtomicInteger atomicInteger = new AtomicInteger(0);
public static void main(String[] args) throws InterruptedException {
NavigableSet<Integer> s = new TreeSet<>();
NavigableSet<Integer> theSet = Collections.synchronizedNavigableSet(s);
final ExecutorService e = Executors.newFixedThreadPool(1000);
for (int i = 1; i <= 1000; i++) {
e.execute(() -> {
int n = atomicInteger.incrementAndGet();
try {
theSet.add(n);
} catch (Exception e1) {
System.out.println(e1 + " " + n);
}
});
}
e.shutdown();
e.awaitTermination(1000, TimeUnit.SECONDS);
System.out.println(theSet.size());//should be 1000
}
}
输出:
1000
示例 4
import java.util.*;
public class CollectionsSynchronizedNavigableSetExample4 {
public static void main(String[] args) {
NavigableSet<Integer> set = new TreeSet<>();
set.add(1001);
set.add(1002);
set.add(1003);
set.add(1004);
set.add(1005);
System.out.println("Set before Synchronized navigable set:" + set);
Collections.synchronizedNavigableSet(set);
Iterator<Integer> itr=set.iterator();
System.out.println("Set after Synchronized navigable set-");
while(itr.hasNext()){
System.out.println(itr.next());
}
}
}
输出:
Set before Synchronized navigable set:[1001, 1002, 1003, 1004, 1005] Set after Synchronized navigable set- 1001 1002 1003 1004 1005
相关用法
- Java Collections synchronizedNavigableMap()用法及代码示例
- Java Collections synchronizedSortedSet()用法及代码示例
- Java Collections synchronizedSet()用法及代码示例
- Java Collections synchronizedSortedMap()用法及代码示例
- Java Collections synchronizedMap()用法及代码示例
- Java Collections synchronizedCollection()用法及代码示例
- Java Collections synchronizedList()用法及代码示例
- Java Collections singleton()用法及代码示例
- Java Collections sort()用法及代码示例
- Java Collections swap()用法及代码示例
- Java Collections shuffle()用法及代码示例
- Java Collections singletonList()用法及代码示例
- Java Collections singletonMap()用法及代码示例
- Java Collections checkedQueue()用法及代码示例
- Java Collections unmodifiableNavigableSet()用法及代码示例
- Java Collections checkedSet()用法及代码示例
- Java Collections copy()用法及代码示例
- Java Collections checkedMap()用法及代码示例
- Java Collections fill()用法及代码示例
- Java Collections nCopies()用法及代码示例
注:本文由纯净天空筛选整理自 Java Collections synchronizedNavigableSet() Method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。