Introduction to the use of Blockingqueue based on Java _java

Source: Internet
Author: User
Recently in the maintenance of a Java project, in the group also chat up the pros and cons of Java! But some of the ultimate Java fans, always known as performance has no need for C + +, and many standard class libraries are master-level people write, how to stabilize and so on. Simply to study it carefully, they gave me a note is that the message between the threads, with Java has been encapsulated good blockingqueue, it is enough to use.

Since it's enough to write code tests, simply write a small program to do a test:
Copy Code code 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 ();
}
}
}
}


Packages that need 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;
}

Pass 100,000 data, on my test machine, probably need about 50ms, but also can! Just looked at the bottom of the Blockingqueue implementation

The offer and poll I used in the test code above, look at these two implementations, first of all, an offer.
Copy Code code 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, just add a signal, in learning UNIX environment advanced programming, see the condition variable for the synchronization between the threads, you can achieve the thread in a competitive way to achieve synchronization!
The implementation of the poll function is similar!
Copy Code code 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.