Collection类方法详解____(三)同步控制类synchrosized

     synchrosized是java语言中的一种关键字,是一种同步锁,主要用于线程安全处理上,synchrosized 常用对象如下几种:

       1、修饰代码块:被修饰的代码块称为同步语句块,{}里面的元素是作用范围,作用于引用这个代码块的对象 ;

        2、修饰一个方法:被修饰的代码块称为同步方法,{}里面的元素是作用范围,作用于引用这个方法的对象;

        3、修饰一个静态的方法:作用范围是整个静态方法,作用于调用这个方法的对象;

        4、修饰一个类:被修饰的类称为同步类,{}里面的属性和方法是作用范围,作用于引用这个类的对象。

         synchrosized简单来说就是解决Java中出现的一些并发的问题,他确保同一个时间段类只能进入一个线程执行代码,保证了多线程环境下并发的安全效果。当对个对象执行这个代码块的时候,会一个一个进行执行,互不干扰。

           synchrosized在Collections类中的应用也同样如此,Collections类中 synchrosized同步控制类方法主要解决的是线程的安全难问题。在其中,有以下几个方法。

注意:在使用迭代方法遍历集合时需要手工同步返回原来的集合。

1、synchronizedSet

          解决Set集合的线程安全问题。

Set set = new HashSet(Arrays.asList(3 , 5 , 7 , 4 , 8 , 4));
        Set sym = Collections.synchronizedSet(set);
        sym.add(6);
        //使用迭代器遍历
        synchronized (sym) {
            Iterator iterator = sym.iterator();
            while (iterator.hasNext()) {
                System.out.println(iterator.next());
            }
        }

2、synchronizedMap

                 解决Map集合的线程安全问题

Map<Integer,String> map=new HashMap<Integer,String>();
        map.put(1,"q");
        map.put(2,"t");
        Map<Integer, String> sym = Collections.synchronizedMap(map);
        sym.put(6,"fg");
        //使用迭代器遍历
        synchronized (sym) {
            Set<Integer> s = sym.keySet();
            Iterator  iterator=s.iterator();
            while (iterator.hasNext()) {
                Object b = iterator.next();
                String d = sym.get(b);
                System.out.println(b+","+d);
            }
        }

3、synchronizedList

       解决List集合的线程安全问题

 List list = new ArrayList(Arrays.asList(2 , 3 , 5 , 7 , 3 , 5));
        List a = Collections.synchronizedList(list);
        list.add(9);
        synchronized (a) {
            Iterator iterator = a.iterator();
            while (iterator.hasNext()) {
                System.out.println(iterator.next());
            }
        }

4、synchronizedSortSet

              解决SortedSet集合的线性问题

public class CollectionDome2 {
    public static void main(String[] args) throws Exception {
           SortedSet<String> set = new TreeSet<String>(Arrays.asList("as","ffe","sdf"));
            // populate the set
            // create a synchronized sorted set
            SortedSet sorset = Collections.synchronizedSortedSet(set);
            System.out.println("Sorted set is :"+sorset);
    }
}

5、synchronizedSortMap

              解决SortedMap集合的线性问题,TreeMap是SortedMap的子类

public class CollectionDome2 {
    public static void main(String[] args) throws Exception {
          SortedMap<Integer,String> sortedMap= (SortedMap<Integer, String>) new TreeMap<Integer,String>();
          sortedMap.put(1,"dff");
          sortedMap.put(2,"fd");
        SortedMap<Integer, String> sm = Collections.synchronizedSortedMap(sortedMap);
        sm.put(3,"gfjkkf");
        synchronized (sm){
            Set<Integer> item= sm.keySet();
            Iterator<Integer> iterator = item.iterator();
            while(iterator.hasNext()){
                int a=iterator.next();
                String b=sm.get(a);
                System.out.println(a+","+b);
            }
        }

    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值