Java-concurrency-advanced concurrent object 2, java-concurrent object

Source: Internet
Author: User

Java-concurrency-advanced concurrent object 2, java-concurrent object

Before browsing the following content, click and readStatement

Concurrent set

The java. util. concurrent package contains many supplements to the java Collection framework. Based on the provided interfaces, these sets can be divided into the following categories:

  • BlockingQueue defines the data structure of first in first out. If you add an element to a full pair of columns or retrieve an element from an empty pair of columns, a timeout occurs, or be blocked.
  • ConcurrentMap is a sub-interface of java. util. Map and defines some atomic operations. These atomic operations can remove or replace an existing key-value pair, or add a key-value pair that does not exist (the key does not exist. Atomic operations can avoid synchronization. The general ConcurrentMap implementation is ConcurrentHashMap, which is the concurrent synonym of HashMap.
  • ConcurrentNavigableMap is a subinterface of ConcurrentMap and supports approximate matching. The standard universal implementation is ConcurrentSkipListMap, which is the concurrent synonym of TreeMap.

All these sets can help avoid Memory consistency errors by establishing a relationship between the actions for adding objects to the set and subsequent access and removal actions.

Atomic variable

Java. util. concurrent. the atomic package defines classes that support atomic operations on a variable. All classes contain the set and get methods for reading and writing, just like reading and writing volatile variables. That is to say, the set method corresponding to the same variable has a preference relationship with the next get method. Atomic MethodcompareAndSetIt also has these memory consistency features, just like the atomic arithmetic method used for integer atomic variables. Atomic variables are used as follows:

Import java. util. concurrent. atomic. atomicInteger; class AtomicCounter {// defines the atomic FIELD private AtomicInteger c = new AtomicInteger (0); public void increment () {c. incrementAndGet ();} public void decrement () {c. decrementAndGet ();} // returns the Basic public int value () {return c. get ();}}
Concurrent random numbers

Java. util. concurrent contains a convenient class, ThreadLocalRandom, because some programs want to use random numbers in multiple threads or ForkJoinTask. For concurrent access, use ThreadLocalRandom relative to Math. random () can reduce resource competition and ultimately achieve better performance.

To use ThreadLocalRandom, you only need to call its static method current method to obtain a ThreadLocalRandom object, and then call one of the methods to obtain a random number:

// The nextInt method can specify two integers as the upper limit and lower limit. The following returns a random integer int r = ThreadLocalRandom. current (). nextInt (4, 77 );

 

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.