Simple Thread Management - Java - Android

后端 未结 3 426
渐次进展
渐次进展 2020-12-08 23:51

I have an application which spawns a new thread when a user asks for an image to be filtered.

This is the only type of task that I have and all are of equal importan

相关标签:
3条回答
  • 2020-12-09 00:28

    You seem to have implemented a version of the Thread Pool design pattern -- the wikipedia article points to many helpful articles on the subject, which may help you refine your implementation. I also recommend this Java-specific article which has clear code and explanation.

    0 讨论(0)
  • 2020-12-09 00:31

    The reason for the RejectedExecutionException is because AsyncTask implements a thread pool of its own (per Mr. Martelli's answer), but one that is capped at a maximum of 10 simultaneous tasks. Why they have that limit, I have no idea.

    Hence, one possibility is for you to clone AsyncTask, raise the limit (or go unbounded, which is also possible with LinkedBlockingQueue), and use your clone. Then, perhaps, submit the change as a patch to AsyncTask for future Android releases.

    Click here to run a Google Code Search for AsyncTask -- the first hit should be the implementation.

    If you just want to raise the limit, adjust MAXIMUM_POOL_SIZE to be as big as you're likely to need. If you want to go unbounded, use the zero-argument LinkedBlockingQueue constructor instead of the one being presently used. AFAICT, the rest of the code probably stays the same.

    0 讨论(0)
  • 2020-12-09 00:43

    Maybe an option is to have the task wait on a blocking queue (of bitmaps) instead of taking bitmap as a parameter, but you will have to add a way for the task(s) to terminate.

    0 讨论(0)
提交回复
热议问题