我只是无法找出这个问题的原因。
问题:
1) 我正在使用 Thread.Sleep 函数为我的应用程序设置计时器。
2)现在,只要用户进入网页,计时器就会启动,如果用户单击任何链接,计时器(线程)就会停止,然后新的计时器启动。
3) 如果在 3 秒内没有任何 Activity ,计时器就会结束,与网页相关的工作流程也会随之结束。
代码:
下载SummariesPage.java
public DownloadSummariesPage(){
abc = new SimpleThread(this);
Link<Void> link = new Link<Void>("downloadSummaryLink") {
public void onClick() {
boolean threadStatus = abc.checkStatus();
if (threadStatus) {
abc.interrupt();
abc.stop();
abc = new SimpleThread(DownloadSummariesPage.this);
abc.start();
}
else
{
LOG.debug("thread is dead now");
endWorkflow();
LOG.debug("ending the workflow");
setResponsePage(MenuPage.class);
}
}
};
abc.start();
}
public void endWorkflow() {
abc.interrupt();
abc.stop();
boolean downloadReport = false;
LOG.debug("before action 201 inside endworkflow");
Map<String, Serializable> inputs = new HashMap<String, Serializable>();
inputs.put("downloadReport", downloadReport);
try {
wf.doAction(id, 201, inputs);//do not worry about this its for workflow
String empty = "";
populateDownloadReportDatabase(empty);
setResponsePage(MenuPage.class);
} catch (Exception e) {
LOG.debug("Exception while performing 201 workflow, getControlCancel "
+ e);
}
}
}
我的下一个创建线程的类是 SimpleThread.java
class SimpleThread extends Thread {
private static final Logger LOG = Logger.getLogger(SimpleThread.class);
boolean stillActive = true;
DownloadSummariesPage dsp;
SimpleThread(DownloadSummariesPage dp) {
this.dsp = dp;
}
public void run() {
// 5sec timer
LOG.debug("inside run method");
try {
Thread.sleep(3000);
stillActive = false;
} catch (InterruptedException e) {
LOG.debug("Exception in thread " + e);
e.printStackTrace();
}
LOG.debug("before endworkflow");
dsp.endWorkflow();//so this is what i actually want to do...
LOG.debug("at the end of simplethread");
}
public boolean checkStatus() {
return stillActive;
}
}
案例:
1) 发生了什么:用户登录线程 hibernate ,用户单击链接线程停止并创建一个新线程,如果用户再次单击它会再次发生,现在如果用户 3 秒内不执行任何操作,SimpleThread 类中的 stillAlive 变量将设置为 false,当用户单击它时,它会完美结束工作流程...
2) 我想要的:如果用户登录线程启动,并且用户没有 Activity stillAlive 变量设置为 false 并且 dsp.endWorkflow();
语句现在应该结束工作流程。正确的 ?但它只是在到达 endWorkflow()
函数内部后停止,并且实际上并没有结束工作流程...
希望您能明白这一点,我已尽力使其易于理解。感谢您的宝贵时间..
非常感谢任何帮助..
最佳答案
所以这里有一些奇怪的事情。
首先假设线程 hibernate 3 秒,没有任何中断,因此它将调用 dsp.endWorkflow()
,您可以在 onClick
方法中再次调用它。
其次,stillAlive
标志应该是 volatile
boolean volatile stillActive = true;
本节可能会引发错误/错误。
if (threadStatus) {
abc.interrupt();
abc.stop();// you cannot get to this like just after interrupt, maybe thread abc goes first
abc = new SimpleThread(DownloadSummariesPage.this);
abc.start();
}
因为假设当线程正在 sleep 时,您中断它,然后停止它,但是线程有可能在您中断它之后(在您停止它之前)完成它的工作。所以最好停止它,或者当线程到达捕获时返回
。
try {
Thread.sleep(3000);
stillActive = false;
} catch (InterruptedException e) {
LOG.debug("Exception in thread " + e);
e.printStackTrace();
return;//////give up the rest work. so you don't need to call the stop too.
}
错误就在这里
假设用户没有单击“取消”或“新下载”,并且线程刚刚完成 hibernate 并调用dsp.endWorkflow()
,那么这个方法发生了什么?
public void endWorkflow() {
abc.interrupt();
abc.stop();//error here!
boolean downloadReport = false;//this is unreachable
}
看看,您正在通过 abc
线程调用此方法,而错误是您在将 downloadReport
设置为 false
之前杀死了该方法内部的线程。所以可能会是这样的。
public void endWorkflow() {
boolean downloadReport = false;//this is unreachable
abc.interrupt();
abc.stop();//error here!
}
希望可以帮到你。
关于java - 线程停止时无法结束工作流程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19657447/