About several common queues in the Java Collection Class library

Source: Internet
Author: User

Several common queues in Java

The difference between a blocking queue and a normal queue is that when the queue is empty, the operation to get elements from the queue is blocked, or when the queue is full, the operation to add elements to the queue is blocked. Threads that attempt to fetch elements from an empty blocking queue are blocked until other threads insert new elements into the empty queue. Similarly, threads that attempt to add new elements to the full blocking queue are also blocked until other threads make the queue idle again, such as removing one or more elements from the queue, or completely emptying the queue.

First type: Concurrentlinkedqueue, high performance nonblocking unbounded queue

Code Demo:

1concurrentlinkedqueue<string> q =NewConcurrentlinkedqueue<string>();2Q.offer ("a");3Q.offer ("B");4Q.offer ("C");5Q.offer ("D");6Q.add ("E");7         8System.out.println (Q.poll ());//a remove elements from the head and delete them from the queue9System.out.println (Q.size ());//4TenSystem.out.println (Q.peek ());//b OneSystem.out.println (Q.size ());//4

Second type: Arrayblockingqueue, array-based blocking queue, bounded queue

1  New Arrayblockingqueue<string> (5); 2         Array.put ("a"); 3         Array.put ("B"); 4         Array.add ("C"); 5         Array.add ("D"); 6         Array.add ("E"); 7         // Array.add ("F"); 8         System.out.println (Array.offer ("A", 3, Timeunit.seconds));     

Third type: linkedblockingqueue, blocking queue, no queue

1linkedblockingqueue<string> q =NewLinkedblockingqueue<string>();2Q.offer ("a");3Q.offer ("B");4Q.offer ("C");5Q.offer ("D");6Q.offer ("E");7Q.add ("F");8         //System.out.println (Q.size ());9         Ten //For (Iterator Iterator = Q.iterator (); Iterator.hasnext ();) { One //String string = (string) iterator.next (); A //System.out.println (string); - //        } -          thelist<string> list =NewArraylist<string>(); -System.out.println (Q.drainto (list, 3)); - System.out.println (List.size ()); -          for(String string:list) { + System.out.println (string); -}

Fourth type: Synchronousqueue

1 Finalsynchronousqueue<string> q =NewSynchronousqueue<string>();2Thread T1 =NewThread (NewRunnable () {3 @Override4              Public voidrun () {5                 Try {6 System.out.println (Q.take ());7}Catch(interruptedexception e) {8 e.printstacktrace ();9                 }Ten             } One         }); A T1.start (); -Thread t2 =NewThread (NewRunnable () { -              the @Override -              Public voidrun () { -Q.add ("ASDASD"); -             } +         }); -T2.start ();

About several common queues in the Java Collection Class library

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.