Java教程

Java Collections syncNavigableSet()

Java集合类的 synchronizedNavigableSet()方法用于获取同步(线程安全)的导航集

语法

下面是 synchronizedNavigableSet()方法的声明:
public static <T> NavigableSet<T> synchronizedNavigableSet(NavigableSet<T> s)

参数

参数 说明 必需/可选
s 这是可导航集合,将被包装在一个同步的可导航集合中。 必需

返回

synchronizedNavigableSet()方法返回指定导航集的同步视图。

异常

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("lidihuo");
        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, lidihuo]

示例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

昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4