How to correctly use Java. util. concurrent1_queue

Source: Internet
Author: User

Java 5 provides concurrent1_queue to simplify concurrent operations. However, there is a problem: does it mean that we do not need to perform any synchronization or lock operations on our own after using this class?

 

I found this online: http://stackoverflow.com/questions/435069/java-util-concurrentlinkedqueue/435941 // stackoverflow is really a good place ......

 

That is to say, if you directly use the functions provided by it, such as queue. Add (OBJ); or queue. Poll (OBJ);, we do not need to perform any synchronization on our own. However, if it is a non-atomic operation, for example:

 

If (! Queue. isempty () {<br/> queue. Poll (OBJ); <br/>}

 

It is hard to ensure that this queue is not modified by other threads before poll () after isempty () is called. So in this case, we still need to synchronize ourselves:

 

Synchronized (Queue) {<br/> If (! Queue. isempty () {<br/> queue. Poll (OBJ); <br/>}< br/>}

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.