温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

JDK中Collections的线程安全怎么实现

发布时间:2022-04-06 16:32:06 来源:亿速云 阅读:98 作者:iii 栏目:编程语言

这篇“JDK中Collections的线程安全怎么实现”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇“JDK中Collections的线程安全怎么实现”文章吧。

方法:

  1.   public static <T> Set<T> synchronizedSet(Set<T> s) {

  2.     return new SynchronizedSet<T>(s);

  3.     }

  1.  static class SynchronizedSet<E>

  2.       extends SynchronizedCollection<E>

  3.       implements Set<E> {

  4.     private static final long serialVersionUID = 487447009682186044L;


  5.     SynchronizedSet(Set<E> s) {

  6.             super(s);

  7.         }

  8.     SynchronizedSet(Set<E> s, Object mutex) {

  9.             super(s, mutex);

  10.         }


  11.     public boolean equals(Object o) {

  12.         synchronized(mutex) {return c.equals(o);}

  13.         }

  14.     public int hashCode() {

  15.         synchronized(mutex) {return c.hashCode();}

  16.         }

  17.     }


  1.     // use serialVersionUID from JDK 1.2.2 for interoperability

  2.     private static final long serialVersionUID = 3053995032091335093L;


  3.     final Collection<E> c;  // Backing Collection

  4.     final Object mutex;     // Object on which to synchronize


  5.     SynchronizedCollection(Collection<E> c) {

  6.             if (c==null)

  7.                 throw new NullPointerException();

  8.         this.c = c;

  9.             mutex = this;

  10.         }

  11.     SynchronizedCollection(Collection<E> c, Object mutex) {

  12.         this.c = c;

  13.             this.mutex = mutex;

  14.         }


  15.     public int size() {

  16.         synchronized(mutex) {return c.size();}

  17.         }

  18.     public boolean isEmpty() {

  19.         synchronized(mutex) {return c.isEmpty();}

  20.         }

  21.     public boolean contains(Object o) {

  22.         synchronized(mutex) {return c.contains(o);}

  23.         }

  24.     public Object[] toArray() {

  25.         synchronized(mutex) {return c.toArray();}

  26.         }

  27.     public <T> T[] toArray(T[] a) {

  28.         synchronized(mutex) {return c.toArray(a);}

  29.         }


  30.     public Iterator<E> iterator() {

  31.             return c.iterator(); // Must be manually synched by user!

  32.         }


  33.     public boolean add(E e) {

  34.         synchronized(mutex) {return c.add(e);}

  35.         }

  36.     public boolean remove(Object o) {

  37.         synchronized(mutex) {return c.remove(o);}

  38.         }


  39.     public boolean containsAll(Collection<?> coll) {

  40.         synchronized(mutex) {return c.containsAll(coll);}

  41.         }

  42.     public boolean addAll(Collection<? extends E> coll) {

  43.         synchronized(mutex) {return c.addAll(coll);}

  44.         }

  45.     public boolean removeAll(Collection<?> coll) {

  46.         synchronized(mutex) {return c.removeAll(coll);}

  47.         }

  48.     public boolean retainAll(Collection<?> coll) {

  49.         synchronized(mutex) {return c.retainAll(coll);}

  50.         }

  51.     public void clear() {

  52.         synchronized(mutex) {c.clear();}

  53.         }

  54.     public String toString() {

  55.         synchronized(mutex) {return c.toString();}

  56.         }

  57.         private void writeObject(ObjectOutputStream s) throws IOException {

  58.         synchronized(mutex) {s.defaultWriteObject();}

  59.         }

  60.     }

List和Map方法同理,这样,我们利用了装实模式,给我们的Map和List穿上了交通协管员的制服,减少了类爆炸,这就是装实模式;

  1. package org;


  2. public class AImp implements IA {


  3.     public synchronized void say() {

  4.         ;

  5.     }


  6. }


  1. package org;


  2. public interface IA {

  3.  public void say();

  4. }


sychronized标识符是不影响接口的实现和继承的

以上就是关于“JDK中Collections的线程安全怎么实现”这篇文章的内容,相信大家都有了一定的了解,希望小编分享的内容对大家有帮助,若想了解更多相关的知识内容,请关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI