资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

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

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

创新互联建站专业为企业提供临清网站建设、临清做网站、临清网站设计、临清网站制作等企业网站建设、网页设计与制作、临清企业网站模板建站服务,十余年临清做网站经验,不只是建网站,更提供有价值的思路和整体网络服务。

方法:

  1.   public static  Set synchronizedSet(Set s) {

  2.     return new SynchronizedSet(s);

  3.     }

  1.  static class SynchronizedSet

  2.       extends SynchronizedCollection

  3.       implements Set {

  4.     private static final long serialVersionUID = 487447009682186044L;

  5.     SynchronizedSet(Set s) {

  6.             super(s);

  7.         }

  8.     SynchronizedSet(Set 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 c;  // Backing Collection

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

  5.     SynchronizedCollection(Collection c) {

  6.             if (c==null)

  7.                 throw new NullPointerException();

  8.         this.c = c;

  9.             mutex = this;

  10.         }

  11.     SynchronizedCollection(Collection 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[] toArray(T[] a) {

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

  29.         }

  30.     public Iterator 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 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的线程安全怎么实现”这篇文章的内容,相信大家都有了一定的了解,希望小编分享的内容对大家有帮助,若想了解更多相关的知识内容,请关注创新互联行业资讯频道。


分享名称:JDK中Collections的线程安全怎么实现
路径分享:http://cdkjz.cn/article/goijge.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220