千家信息网

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

发表于:2025-02-03 作者:千家信息网编辑
千家信息网最后更新 2025年02月03日,这篇"JDK中Collections的线程安全怎么实现"文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们
千家信息网最后更新 2025年02月03日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的线程安全怎么实现"这篇文章的内容,相信大家都有了一定的了解,希望小编分享的内容对大家有帮助,若想了解更多相关的知识内容,请关注行业资讯频道。

0