[concurrency-interest] Replacement for new Thread and a run loop?
davidcholmes at aapt.net.au
Thu Oct 7 21:09:09 EDT 2010
Robert Nicholson writes:
> If you're concerned about your thread dying or disappearing
> whenever you use new Thread with run forever/running loop what
> is the "concurrent" equivalent?
If an exception is encountered, and not handled, then the thread will
terminate, regardless of whether it is your own thread or one in a pool.
Either way your loop is terminated.
> Is it a ScheduledExecutor where because there's a pool you're
> always guaranteed to have a Thread available to process your tasks?
If you wanted each iteration of the loop to be independent then you'd have
to submit each iteration as a separate task to the executor (a
singleThreadExecutor if the tasks must be executed serially).
If the trigger for each iteration was time-based then you might submit a
periodic tasks to a ScheduledExecutor, but again the need to serialize the
tasks would complicate things.
Further, it may be very difficult to perform the next iteration if the
previous one failed due to an unknown error - there may be corrupt state
shared across iterations. If the iterations are completely independent then
each iteration is really an independent task that would be best submitted to
an executor. An executor is likely a better tool for the job than your own
thread that pulls work from a work-queue.
> Also, if you use a LinkedBlockingQueue is it important to code
> defensively to not keep putting this in your queue if there's no
> consumer taking things from the queue? ie. Do you code for this
> possibility by abandoning your loop where you are doing your
> "offer" if it fails too many times?
Well you could. It all depends on the overal context and semantics. A more
typical defensive strategy would be to use a blocking put with a timeout,
and if you timeout you assume something is wrong with the consumer (or
vice-versa for the consumer doing a take) and "do something".
> Concurrency-interest mailing list
> Concurrency-interest at cs.oswego.edu
More information about the Concurrency-interest