Pre-java Security Class review, and what classes to use later when thread safety is required

Source: Internet
Author: User

Previously learned Thread-safe Classes:

Stringbuffer: A variable sequence of characters for thread Safety. A String string buffer that is similar to, but cannot be modified.

Vector: Vector classes can implement an array of objects that can grow.

Hashtable: This class implements a hash table that maps the keys to the corresponding Values. Any non- null object can be used as a key or a Value.

1 // Thread-safe Classes 2 New StringBuffer (); 3 New vector<string>(); 4 New hashtable<string, string> ();

As mentioned before, vector is a thread-safe time to consider, but even if it is safe, do not use it.

so, What do we use? Use the Following:

  API Search Collections, viewing methods

A:public static <T> Collection<T> synchronizedcollection(Collection<t > c): Returns the synchronization (thread-safe) collection that the specified collection Supports.

B:public static <T> list<T> synchronizedlist(list<T> list) : Returns a list of synchronized (thread-safe) support for the specified List.

C:public static <K,V> map<K,V> synchronizedmap(map<K,V> M) : Returns the synchronous (thread-safe) mappings supported by the specified mappings.

D:public static <T> set<T> synchronizedset(set<T> s): returns the specified set Supported Synchronous (thread-safe) Set.

E:public static <K,V> sortedmap<K,V> synchronizedsortedmap(sortedmap <K,V> m): Returns a synchronous (thread-safe) ordered map supported by the specified ordered MAP.

F:public static <T> SortedSet<T> synchronizedsortedset(SortedSet<T> S) : Returns a synchronous (thread-safe) ordered set supported by the specified ordered Set.

Choose b as an example:

The previous thread is not secure:

New Arraylist<string> (); // Thread not secure

  Now use b, thread-safe:

List<string> List2 = collections.synchronizedlist (new//  thread safe

Pre-java Security Class review, and what classes to use later when thread safety is required

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.