Java Collections synchronizedSortedSet()

synchronizedSortedSet() 用于获取由指定的SortedSet支持的同步(线程安全)SortedSet。

1 语法

public static <T> SortedSet<T> synchronizedSortedSet(SortedSet<T> s)  

2 参数

s:

3 返回值

返回指定SortedSet的同步SortedSet。

4 synchronizedSortedSet()示例1

package com.yiidian;

/**
 * 一点教程网: http://www.yiidian.com
 */
/**
 * Java Collections.synchronizedSortedSet的例子
 */
import java.util.*;

public class Demo {

    public static void main(String[] args) {
        //Create set
        SortedSet<String> set = new TreeSet<String>();
        //Add values in the set
        set.add("Facebook");
        set.add("Twitter");
        set.add("Whatsapp");
        set.add("Instagram");
        //Create a synchronized sorted set
        Set<String> synset = Collections.synchronizedSortedSet(set);
        System.out.println("Synchronized Sorted set is :"+synset);
    }
}

输出结果为:

Synchronized Sorted set is :[Facebook, Instagram, Twitter, Whatsapp]

5 synchronizedSortedSet()示例2

package com.yiidian;

/**
 * 一点教程网: http://www.yiidian.com
 */
/**
 * Java Collections.synchronizedSortedSet的例子
 */
import java.util.*;

public class Demo {

    public static void main(String[] args) {
        SortedSet<Integer> set = Collections.synchronizedSortedSet(new TreeSet<>());
        set.add(101);
        set.add(104);
        set.add(103);
        set.add(102);
        set.add(105);
        System.out.println("Set after Synchronized sorted set-");
        //Using iterator must be synchronized manually
        synchronized (set) {
            Iterator<Integer> iterator = set.iterator();
            while (iterator.hasNext()) {
                Integer num = iterator.next();
                System.out.println(num);
            }
        }
    }
}

输出结果为:

Set after Synchronized sorted set-
101
102
103
104
105

6 synchronizedSortedSet()示例3

package com.yiidian;

/**
 * 一点教程网: http://www.yiidian.com
 */
/**
 * Java Collections.synchronizedSortedSet的例子
 */
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 Demo {

    private static AtomicInteger atomicInteger = new AtomicInteger(0);
    public static void main(String[] args) throws InterruptedException {
        SortedSet<Integer> set = new TreeSet<>();
        SortedSet<Integer> theSet = Collections.synchronizedSortedSet(set);
        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

 

热门文章

优秀文章