本文介绍了线程饥饿死锁在代码中发生了吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
c> c> c> c> c> c> 两个任务提交到 exec 。首先是 LoadFileTask(header.html),第二个是 LoadFileTask(footer.html)。但是因为ExecutorService exec 通过代码 Executors.newSingleThreadExecutor(); 获得 使用单个工作线程关闭一个无界的queueThread ,并且线程已经分配给RenderPageTask,所以 LoadFileTask(header.html)和 LoadFileTask html)将被排队到无界队列,等待由线程执行。
我希望这清楚为什么线程饥饿死锁正在上面的代码中发生。
- Task is submitted to exec for Rendering the page via Callable implemented class RenderPageTask.
- exec started the RenderPageTask in separate Thread , the only Thread that would execute other tasks submitted to exec sequentially .
- Inside call() method of RenderPageTask two more tasks are submitted to exec . First is LoadFileTask("header.html") and second is LoadFileTask("footer.html"). But since the the ExecutorService exec obtained via code Executors.newSingleThreadExecutor(); as mentioned here uses a single worker thread operating off an unbounded queueThread and the thread is already allocated to RenderPageTask , So LoadFileTask("header.html") and LoadFileTask("footer.html") will be en queued to the unbounded queue waiting for there turn to be executed by that Thread.
- RenderPageTask is returning a String containing the concatenation of output of LoadFileTask("header.html") , body of page and output of LoadFileTask("footer.html"). Out of these three parts page is obtained successfully by RenderPageTask . But other two parts can only be obtained after both tasks are executed by the single Thread allocated by ExecutorService . And Thread will be free only after call() method of RenderPageTask returns . But call method of RenderPageTask will return only after LoadFileTask("header.html") and LoadFileTask("footer.html") is returned. So Not letting LoadFileTask to execute is leading to Starvation . And each task waiting for other task for completion is leading to DeadLock
I hope this makes clear of why thread starvation deadlock is occurring in above code.
这篇关于线程饥饿死锁在代码中发生了吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!