欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

JDK中Collections的線程安全怎么實現(xiàn)

這篇“JDK中Collections的線程安全怎么實現(xiàn)”文章的知識點大部分人都不太理解,所以小編給大家總結(jié)了以下內(nèi)容,內(nèi)容詳細,步驟清晰,具有一定的借鑒價值,希望大家閱讀完這篇文章能有所收獲,下面我們一起來看看這篇“JDK中Collections的線程安全怎么實現(xiàn)”文章吧。

創(chuàng)新互聯(lián)建站專業(yè)為企業(yè)提供臨清網(wǎng)站建設(shè)、臨清做網(wǎng)站、臨清網(wǎng)站設(shè)計、臨清網(wǎng)站制作等企業(yè)網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計與制作、臨清企業(yè)網(wǎng)站模板建站服務(wù),十余年臨清做網(wǎng)站經(jīng)驗,不只是建網(wǎng)站,更提供有價值的思路和整體網(wǎng)絡(luò)服務(wù)。

方法:

  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穿上了交通協(xié)管員的制服,減少了類爆炸,這就是裝實模式;

  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標識符是不影響接口的實現(xiàn)和繼承的

以上就是關(guān)于“JDK中Collections的線程安全怎么實現(xiàn)”這篇文章的內(nèi)容,相信大家都有了一定的了解,希望小編分享的內(nèi)容對大家有幫助,若想了解更多相關(guān)的知識內(nèi)容,請關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。

當前題目:JDK中Collections的線程安全怎么實現(xiàn)
標題來源:http://chinadenli.net/article14/goijge.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站設(shè)計域名注冊定制網(wǎng)站網(wǎng)站收錄網(wǎng)站策劃面包屑導(dǎo)航

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

外貿(mào)網(wǎng)站制作