Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it bad to use polling in Java?

I have several ArrayLists which work as queues of data. Each of the queues is linked to an individual thread which checks if that ArrayList has some data in it.

 while (array.size == 0) {     // nothing  }   // do stuff with one element of the array  // remove element from array  // and call the loop again 

I have done similar things in embedded system programming, but is it safe to use this in Java? The concern is about process power waste by iterating around that while loop very fast.

It could be solved by adding Thread.sleep(100) and check every 100ms, but then again - slower response time.

The question is - do I need to add that sleep or I shouldn't be concerned about that?

Any suggestions on safer/better system to check for new data in arrays?

like image 699
Arturs Vancans Avatar asked Aug 28 '12 12:08

Arturs Vancans


People also ask

What is Java polling?

The process of testing a condition repeatedly till it becomes true is known as polling. Polling is usually implemented with the help of loops to check whether a particular condition is true or not. If it is true, certain action is taken. This waste many CPU cycles and makes the implementation inefficient.

How can we prevent polling in Java?

To avoid polling, Java uses three methods, namely, wait(), notify(), and notifyAll(). All these methods belong to object class as final so that all classes have them. They must be used within a synchronized block only.

Which method is used for polling in Java?

Queue poll() method in Java The poll() method of Queue Interface returns and removes the element at the front end of the container. It deletes the element in the container. The method does not throws an exception when the Queue is empty, it returns null instead.

What is polling in multithreading?

Polling repeatedly checks the status of an asynchronous call from within a loop. Polling is the least efficient way to manage threads because it wastes resources by repeatedly checking the status of the various thread properties. For example, the IsAlive property can be used when polling to see if a thread has exited.


2 Answers

ArrayList is not a thread safe collection, so if one thread adds data to your list, and another thread tries to retrieve data from the same list, you have no guarantee that the other thread will ever see the added elements.

And busy waiting like what you describe consumes cpu resources unnecessarily.

Since you seem to need a queue, why don't you use one, like ArrayBlockingQueue. It has a take method which will block, without consuming CPU cycles, until an item gets added to the queue. And it is thread safe.

like image 54
assylias Avatar answered Oct 08 '22 23:10

assylias


Unless the time that you need to wait is very very short, thus making a context switch too expensive, I would not use spinning. It definitely wastes CPU cycles for no good reason.

You should use wait/notify or some other signaling mechanism to suspend the thread and wake it up only when necessary.

Going to more advanced constructs, there are specialized data structures for producer-consumer patterns, like BlockingQueue (choose an implementation):

A Queue that additionally supports operations that wait for the queue to become non-empty when retrieving an element, and wait for space to become available in the queue when storing an element.

like image 20
Tudor Avatar answered Oct 09 '22 00:10

Tudor