线程饥饿死锁在代码中发生了吗? [英] Is thread starvation deadlock happening here in the code?
本文介绍了线程饥饿死锁在代码中发生了吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
//code taken from java concurrency in practice
package net.jcip.examples;
import java.util.concurrent.*;
public class ThreadDeadlock
{
ExecutorService exec = Executors.newSingleThreadExecutor();
public class LoadFileTask implements Callable<String> {
private final String fileName;
public LoadFileTask(String fileName) {
this.fileName = fileName;
}
public String call() throws Exception {
// Here's where we would actually read the file
return "";
}
}
public class RenderPageTask implements Callable<String>
{
public String call() throws Exception
{
Future<String> header, footer;
header = exec.submit(new LoadFileTask("header.html"));
footer = exec.submit(new LoadFileTask("footer.html"));
String page = renderBody();
// Will deadlock -- task waiting for result of subtask
return header.get() + page + footer.get();
}
}
}
这段代码是从Java并发在实践中和作者ThreadStarvtionDeadlock发生在这里。请帮助我找到ThreadStarvationDeadlock如何发生这里和哪里?先感谢。
This code is take from Java concurrency in practice and as per the authors "ThreadStarvtionDeadlock" is happening here. Please help me finding how ThreadStarvationDeadlock is happening here and where? Thanks in advance.
推荐答案
Deadlock&饥饿发生在以下行:
Deadlock & Starvation is occurring at following line:
return header.get() + page + footer.get();
如何?
向程序添加一些额外的代码。可能是这个:
HOW?
It will happen if we add some extra code to the program. It might be this one:
public void startThreadDeadlock() throws Exception
{
Future <String> wholePage = exec.submit(new RenderPageTask());
System.out.println("Content of whole page is " + wholePage.get());
}
public static void main(String[] st)throws Exception
{
ThreadDeadLock tdl = new ThreadDeadLock();
tdl.startThreadDeadLock();
}
导致deadLock的步骤:
Steps that leading to deadLock:
- 任务提交到
exec
,用于通过Callable
RenderPageTask c> c> c>
在单独的
两个任务提交到线程
中,将执行提交到<$ c的其他任务的唯一线程
c> c> c> c>exec
。首先是LoadFileTask(header.html)
,第二个是LoadFileTask(footer.html)
。但是因为ExecutorServiceexec
通过代码Executors.newSingleThreadExecutor();
获得在这里 使用单个工作线程关闭一个无界的queueThread ,并且线程已经分配给RenderPageTask,所以LoadFileTask(header.html)
和LoadFileTask html)
将被排队到无界队列,等待由线程
执行。 -
RenderPageTask
正在返回一个字符串,其中包含LoadFileTask(header.html)
和LoadFileTask(footer.html)
的输出。在这三个部分中,RenderPageTask
成功获得了页
。但是其他两个部分只有在两个任务都由由ExecutorService
分配的单个线程执行之后才能获得。只有call()
方法RenderPageTask
返回时,线程才会自由。但调用方法
RenderPageTask
只会在后返回LoadFileTask(header.html)
和返回LoadFileTask(footer.html)
。因此,不要让执行LoadFileTask
导致 Starvation 。每个任务等待其他任务完成导致 DeadLock
我希望这清楚为什么线程饥饿死锁正在上面的代码中发生。
- Task is submitted to
exec
for Rendering the page viaCallable
implemented classRenderPageTask
. exec
started theRenderPageTask
in separateThread
, the onlyThread
that would execute other tasks submitted toexec
sequentially .- Inside
call()
method ofRenderPageTask
two more tasks are submitted toexec
. First isLoadFileTask("header.html")
and second isLoadFileTask("footer.html")
. But since the the ExecutorServiceexec
obtained via codeExecutors.newSingleThreadExecutor();
as mentioned here uses a single worker thread operating off an unbounded queueThread and the thread is already allocated to RenderPageTask , SoLoadFileTask("header.html")
andLoadFileTask("footer.html")
will be en queued to the unbounded queue waiting for there turn to be executed by thatThread
. RenderPageTask
is returning a String containing the concatenation of output ofLoadFileTask("header.html")
, body of page and output ofLoadFileTask("footer.html")
. Out of these three partspage
is obtained successfully byRenderPageTask
. But other two parts can only be obtained after both tasks are executed by the single Thread allocated byExecutorService
. And Thread will be free only aftercall()
method ofRenderPageTask
returns . Butcall
method ofRenderPageTask
will return only afterLoadFileTask("header.html")
andLoadFileTask("footer.html")
is returned. So Not lettingLoadFileTask
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.
这篇关于线程饥饿死锁在代码中发生了吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文