Use of Java-BlockingQueue, java-blockingqueue

Source: Internet
Author: User

Use of Java-BlockingQueue, java-blockingqueue

Every time I write something in my blog after a long time, I can say that I am busy. This is an excuse for myself. In fact, I am still lazy. Ah...

There is a need for comparison in recent projects. You need to obtain the parameters from the log file, and then call different APIs to compare the results. But I don't know how to use it, so I checked the jdk manual and found BlockingQueue.

About BlockingQueue introduction, you are interested can look at their own: http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/LinkedBlockingQueue.html

 

The requirement is simply to place the parameter in the Queue and then submit it to the next policy for consumption. At the beginning, data is stored in the queue through different threads, and then a BlockingQueue object is returned to the next service. The next policy is consumed from the queue. The code is as follows:

@ SuppressWarnings ("rawtypes") @ Overridepublic BlockingQueue getTxtLogContent (String path) {File file File = new File (path); BufferedReader reader = null; String tempStr = null; final BlockingQueue queue = new LinkedBlockingQueue (); try {reader = new BufferedReader (new FileReader (file); while (tempStr = reader. readLine ())! = Null) {final InputOutputPrameters parameter = new InputOutputPrameters (); String [] list = tempStr. split (";"); if (list! = Null & list. length> 0) {parameter. setInputParamters (list [0]); parameter. setOutputParameters (list [1]);} new Thread () {@ SuppressWarnings ("unchecked") public void run () {try {Thread. sleep (long) (Math. random () * 100); log.info ("start to store data! "); Queue. put (parameter); log.info (" stored data, there are currently "+ queue. size () +" queues in the queue! Input parameter: "+ parameter. getInputParamters () + "; \ n output parameter:" + parameter. getOutputParameters ();} catch (Exception e) {log. error ("system exception:" + e );}}}. start ();} reader. close ();} catch (FileNotFoundException e) {e. printStackTrace ();} catch (IOException e) {e. printStackTrace ();} finally {if (reader! = Null) {try {reader. close ();} catch (Exception e) {e. printStackTrace () ;}} return queue ;}

However, in actual operation, due to the large log size, the next policy may have to wait for 1hour or longer to start processing. This obviously does not meet the requirements, so it is optimized again, change BlockingQueue to Global static, and then the next policy can directly monitor whether there is a value in this queue. If there is a value, it will be consumed. If there is no value, it will block other processing such as thread waiting or timeout.

Improved code:

1. Create a queue class:

Public class ParameterQueue extends blockingqueue <InputOutputPrameters> {/*** @ Fields serialVersionUID: */private static final long serialVersionUID = 6032356446145302484L; private static BlockingQueue <InputOutputPrameters> queue = new LinkedBlockingQueue <InputOutputPrameters> ();/*** @ Fields log: log record */private static final Logger log = LoggerFactory. getLogger (ParameterQueue. class);/*** get the object in the queue * @ Method: getParameter * @ Description: Get the object in the queue * @ return get object information */public static InputOutputPrameters getParameter () {InputOutputPrameters result = null; try {result = (InputOutputPrameters) queue. take ();} catch (Exception e) {log. error ("Get queue exception, exception information:" + e);} return result;}/*** get the number of queues * @ Method: getQueueSize * @ Description: obtain the number of queues * @ return quantity */public static Integer getQueueSize () {return queue. size ();}/*** place the parameter in the queue * @ Method: putParameter * @ Description: place parameters in the queue * @ param parameter object to be placed */public static void putParameter (InputOutputPrameters parameter) {try {queue. put (parameter);} catch (Exception e) {log. error ("queue insertion exception, exception information:" + e );}}}

2. When reading a file, directly operate the queue and put the value in the queue. the next policy gets the value from the queue. The put code is as follows:

Public void getSource (String path) {try {File file = new File (path); BufferedReader reader = null; String tempStr = null; try {reader = new BufferedReader (new FileReader (file); while (tempStr = reader. readLine ())! = Null) {final InputOutputPrameters parameter = new InputOutputPrameters (); String [] list = tempStr. split (";"); if (list! = Null & list. length> 0) {parameter. setInputParamters (list [0]); parameter. setOutputParameters (list [1]);} putInQueue (parameter);} reader. close ();} catch (FileNotFoundException e) {e. printStackTrace ();} catch (IOException e) {e. printStackTrace ();} finally {if (reader! = Null) {try {reader. close ();} catch (Exception e) {e. printStackTrace () ;}}} catch (Exception e) {log. error ("system exception:" + e) ;}/ *** store parameters in the queue * @ Method: putInQueue * @ Description: store parameters in the queue * @ param parameter object to be stored */private void putInQueue (final InputOutputPrameters parameter) {new Thread () {public void run () {try {Thread. sleep (long) (Math. random () * 100); log.info ("start to store data! "); ParameterQueue. putParameter (parameter); log.info (" saved data. Currently, "+ ParameterQueue. getQueueSize () +" Queues exist in the queue! Input parameter: "+ parameter. getInputParamters () + "; \ n output parameter:" + parameter. getOutputParameters ();} catch (Exception e) {log. error ("system exception:" + e );}}}. start ();}

  

Therefore, this requirement is met. Record this small requirement for future reference.

To put it simply, BlockingQueue is thread-safe and commonly used are ArrayBlockingQueue and LinkedBlockingQueue.

ArrayBlockingQueue requires capacity, while LinkedBlockingQueue does not.

At the same time, take () will block the thread during consumption. If it is a single thread running, the whole thread will be stuck when take () is not there.

Therefore, depending on the specific environment requirements, whether to use take or other services. I usually use poll because a timeout time can be set.

I don't know how to write it.

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.