Introduction to BlockingQueue Based on java

Source: Internet
Author: User

Recently I am maintaining a java project, so I will talk about the advantages and disadvantages of java in the group! However, some ultimate Java fans always claim that the performance is no longer inferior to C ++, and many standard class libraries are written by masters and how to stabilize them. I simply studied it carefully. One of the instructions they gave me is that it is enough to deliver messages between threads and use BlockingQueue encapsulated by java.

If it is enough, write the code for testing. Simply write a small program and perform a test:Copy codeThe Code is as follows: // default package
Import java. util. concurrent .*;

Import base. MyRunnable;

Public class Test
{
Public static void main (String [] args)
{
BlockingQueue <Integer> queue = new LinkedBlockingQueue <Integer> ();
Java. lang. Runnable r = new MyRunnable (queue );
Thread t = new Thread (r );
T. start ();

While (true)
{
Try
{
While (true)
{
For (int I = 0; I <10000; I ++)
{
Queue. offer (I );
}
}
}
Catch (Exception e)
{
E. printStackTrace ();
}
}
}
}

// Package to be added
Package base;

Import java. lang. Runnable;
Import java. util. concurrent .*;
Import java. util .*;

Public class MyRunnable implements Runnable
{
Public MyRunnable (BlockingQueue <Integer> queue)
{
This. queue = queue;
}
Public void run ()
{
Date d = new Date ();
Long starttime = d. getTime ();
System. err. println (starttime );
Int count = 0;
While (true)
{
Try
{
Integer I = this. queue. poll ();
If (I! = Null)
{
Count ++;
}
If (count = 100000)
{
Date e = new Date ();
Long endtime = e. getTime ();
System. err. println (count );
System. err. println (endtime );
System. err. print (endtime-starttime );
Break;
}

}
Catch (Exception e)
{

}
}
}
Private BlockingQueue <Integer> queue;
}

It takes about 50 ms to transmit 100,000 pieces of data on my testing machine! Simply look at the underlying implementation of BlockingQueue

I used offer and poll in the above test code. Let's take a look at the two implementation functions. The first is offer.Copy codeThe Code is as follows: public E poll (){
Final AtomicInteger count = this. count;
If (count. get () = 0)
Return null;
E x = null;
Int c =-1;
Final ReentrantLock takeLock = this. takeLock;
TakeLock. lock ();
Try {
If (count. get ()> 0 ){
X = extract ();
C = count. getAndDecrement ();
If (c> 1)
NotEmpty. signal ();
}
} Finally {
TakeLock. unlock ();
}
If (c = capacity)
SignalNotFull ();
Return x;
}

Similar to the general synchronization thread, only a signal is added. When learning advanced programming in the unix environment, we can see that the condition variable is used for synchronization between threads to achieve synchronization in a competitive manner!
The implementation of poll functions is similar!Copy codeThe Code is as follows: public boolean offer (E e ){
If (e = null) throw new NullPointerException ();
Final AtomicInteger count = this. count;
If (count. get () = capacity)
Return false;
Int c =-1;
Final ReentrantLock putLock = this. putLock;
PutLock. lock ();
Try {
If (count. get () <capacity ){
Insert (e );
C = count. getAndIncrement ();
If (c + 1 <capacity)
NotFull. signal ();
}
} Finally {
PutLock. unlock ();
}
If (c = 0)
SignalNotEmpty ();
Return c> = 0;
}

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.