The bitter lesson of using Concurrentlinkedqueue

Source: Internet
Author: User
Tags static class

The service side originally had a timed task to handle the messages in a collection ArrayList. Because the processing message is considered to be the FIFO principle, the optimization should be considered instead of concurrentlinkedqueue at that time did not carefully in-depth study of the collection on the rush to the online. The result just came online the next day it was a problem. Service-side optimization has evolved into a flaw, fortunately timely return of the version, the consequences are not very serious.

Back off. A simple test code for Concurrentlinkedqueue is as follows:

Import Java.util.concurrent.ConcurrentLinkedQueue; Import Java.util.concurrent.CountDownLatch; Import Java.util.concurrent.ExecutorService; Import java.util.concurrent.Executors; public class Concurrentlinkedqueuetest {private static concurrentlinkedqueue<integer> queue = new Concurrentlinkedqueue<integer> (); private static int count = 100000; private static int count2 = 2; Number of threads private static Countdownlatch cd = new Countdownlatch (COUNT2); public static void Dothis () {to (int i = 0; i < count; i++) {Queue.offer (i)}} public static void Main (string[) AR GS) throws Interruptedexception {Long Timestart = System.currenttimemillis (); Executorservice es = Executors.newfixedthreadpool (4); Concurrentlinkedqueuetest.dothis (); for (int i = 0; i < Count2 i++) {es.submit (New Poll ());} cd.await (); System.out.println ("Cost time" + (System.currenttimemillis ()-Timestart) + "MS"); Es.shutdown (); The static class Poll implements Runnable {@Override public void run () {// while (Queue.size () >0) {while (!queue.isempty ()) {System.out.println (Queue.poll ());} cd.countdown ();} }

Run Result:

Costtime 2360ms

When you switch to while (Queue.size () >0)

Run Result:

Cost Time 46422ms

The result is so different, look at the Concurrentlinkedqueue API original. Size () is to traverse the collection, no wonder so slow, so try to avoid using size instead of IsEmpty ().

summed up, in the unit lack of performance testing, the requirements of their own programming more stringent, especially in the production environment is to be cautious.

Related Article

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.