Featured post
java - What's a good, efficient way to implement run()? -
should contain loop
while (true) { ... }
i find not efficient consumes cpu much. thread keep on waiting what's best way make wait without consuming cpu?
there many ways efficiently using object.wait
, object.notify
(see here), or using various higher level java concurrency classes such blocking queues, latches, futures, semaphores or barriers.
===================================================
you correct repeatedly testing condition bad idea; e.g.
while (!condition) { // bad idea ... burns cpu cycles. }
the following improvement, can still problematic:
while (!condition) { thread.sleep(...); // bad idea }
the problem if set sleep interval short, loop gets expensive. if set longer (e.g. second or so), thread can take amount of time respond condition becoming true. net result can reduced throughput , / or sluggish user interfaces.
sometimes loop{test; sleep}
approach best available option, unfortunately, if can avoid it, should.
- Get link
- X
- Other Apps
Comments
Post a Comment