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?
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.
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With