Java design Polling thread performance issues with while loop

Source: Internet
Author: User
Tags cpu usage

Java Design Polling thread performance issues with while loopThe polling thread is widely used in the development process, where I simulate a scenario that has a queue and polling thread, the main thread queues messages into the queue, and the polling thread loops through the queue to read the message and print the message content. A bit like handler in Android sends messages. First define a message class.
public class Message {    private String content;    public Message(String content) { this.content=content; } public void display(){ System.out.println(content); }}
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
        This class is simple, passing in the message content when constructed, and the display () method prints the message content.         Next define a polling thread, and at first I wrote it like this.
public Span class= "Hljs-class" >class pollingthread extends thread implements runnable {public static Queue< message> queue = new linkedtransferqueue<message> ();  @Override public void run () {while (true) {while (!queue.isempty ()) {Queue.poll (). display ();}} }} 
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
This polling thread is very simple and keeps polling the queue, and once the queue has a message coming in, it will be out of the team and call the display () method to output the content. Next, loop through the main () method to create the message and put it in the queue
public Span class= "Hljs-keyword" >class Main {public static void main (string[] args) {pollingthread Pollingthread=new pollingthread (); Pollingthread.start (); int i=1; while (true) {PollingThread.queue.offer ( new message ( "new Message" +i)); i++; try {thread.sleep (10);} catch (interruptedexception e) {e.printstacktrace ();}} }} 
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
Run results
新消息1新消息2新消息3新消息4新消息5新消息6新消息7新消息8新消息9新消息10新消息11新消息12新消息13新消息14新消息15......
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
While doing so, the functionality is implemented, but let's take a look at the CPU usage

Cheng compile starts when the CPU occupancy rate is 100%, and has been at around 44% since it started running. This occupancy rate is still relatively high, then we analyze this polling thread, assuming that there is no message queued, or the queue time interval is longer, it will loop to execute while (!queue.isempty ()) to determine whether the queues are empty, In fact, this judgment operation is very performance-consuming. We should design this polling thread a bit more rationally. So how to design more reasonable? Since the loop on the queue is a waste of performance of the operation, then if we can let this polling line at the beginning of the blocking state, the main thread in each queued message notification polling thread loop out the queue output message content, when the queues are empty when the polling thread automatically into the blocking state, You can avoid polling thread dead loops to empty the queue. Next we'll change the code for the polling thread and the main thread
PublicClassPollingthreadextends thread implements runnable {public static Queue<Message > queue = new linkedtransferqueue<message> ();  @Override public void run () {while (true) {while (!queue.isempty ()) {Queue.poll (). display (); //the message in the queue is all printed and then the thread is blocked synchronized (lock.class) {try {Lock.class.wait ();} catch (interruptedexception e) {e.printstacktrace ();}} } }} 
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21st
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21st
public class Main { Span class= "Hljs-keyword" >public static void main (string[] args) {pollingthread pollingthread=new PollingThread (); Pollingthread.start (); int i=1; while (true) {PollingThread.queue.offer ( new message ( "new Message" +i)); i++; //has message queued after activating polling thread synchronized (lock.class) {Lock.class.notify ();} try {thread.sleep (10);} catch (interruptedexception e) {e.printstacktrace ();}} }} 
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21st
    • 22
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21st
    • 22
Let's run the results again.
新消息1新消息2新消息3新消息4新消息5新消息6新消息7新消息8新消息9新消息10新消息11......
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
And take a look at CPU usage.

The CPU occupancy rate of the polling thread is basically stable at 11%, which is much lower than the previous 44%. Therefore, when writing a polling thread, try to use the notification method to let the polling thread perform the operation, avoiding the performance waste caused by duplicate condition judgment.

http://blog.csdn.net/q15858187033/article/details/60583631

Java design Polling thread performance issues with while loop

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.