Is it bad to use polling in Java?

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.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)