Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize the thread safety of Collections in JDK

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Most people do not understand the knowledge points of this article "how to achieve thread safety of Collections in JDK", so the editor summarizes the following contents, detailed contents, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "how to achieve thread safety of Collections in JDK" article.

Methods:

Public static Set synchronizedSet (Set s) {

Return new SynchronizedSet (s)

}

Static class SynchronizedSet

Extends SynchronizedCollection

Implements Set {

Private static final long serialVersionUID = 48744700982186044L

SynchronizedSet (Set s) {

Super (s)

}

SynchronizedSet (Set s, Object mutex) {

Super (s, mutex)

}

Public boolean equals (Object o) {

Synchronized (mutex) {return c.equals (o);}

}

Public int hashCode () {

Synchronized (mutex) {return c.hashCode ();}

}

}

/ / use serialVersionUID from JDK 1.2.2 for interoperability

Private static final long serialVersionUID = 3053995032091335093L

Final Collection c; / / Backing Collection

Final Object mutex; / / Object on which to synchronize

SynchronizedCollection (Collection c) {

If (c==null)

Throw new NullPointerException ()

This.c = c

Mutex = this

}

SynchronizedCollection (Collection c, Object mutex) {

This.c = c

This.mutex = mutex

}

Public int size () {

Synchronized (mutex) {return c.size ();}

}

Public boolean isEmpty () {

Synchronized (mutex) {return c.isEmpty ();}

}

Public boolean contains (Object o) {

Synchronized (mutex) {return c.contains (o);}

}

Public Object [] toArray () {

Synchronized (mutex) {return c.toArray ();}

}

Public T [] toArray (T [] a) {

Synchronized (mutex) {return c.toArray (a);}

}

Public Iterator iterator () {

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

}

Public boolean add (E e) {

Synchronized (mutex) {return c.add (e);}

}

Public boolean remove (Object o) {

Synchronized (mutex) {return c.remove (o);}

}

Public boolean containsAll (Collection coll) {

Synchronized (mutex) {return c.containsAll (coll);}

}

Public boolean addAll (Collection coll) {

Synchronized (mutex) {return c.removeAll (coll);}

}

Public boolean retainAll (Collection coll) {

Synchronized (mutex) {return c.retainAll (coll);}

}

Public void clear () {

Synchronized (mutex) {c.clear ();}

}

Public String toString () {

Synchronized (mutex) {return c.toString ();}

}

Private void writeObject (ObjectOutputStream s) throws IOException {

Synchronized (mutex) {s.defaultWriteObject ();}

}

}

List and Map method are the same. In this way, we use the loading mode, put our Map and List in the uniform of traffic wardens, and reduce the class explosion, which is the loading mode.

Package org

Public class AImp implements IA {

Public synchronized void say () {

}

}

Package org

Public interface IA {

Public void say ()

}

The sychronized identifier does not affect the implementation and inheritance of the interface

The above is about the content of this article on "how to achieve thread safety of Collections in JDK". I believe we all have some understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please pay attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report