如果任务已经启动,isstarted()方法将返回true:
public synchronized boolean isstarted() {
return started;
}
如果任务已经完成,iscompleted()方法将返回true:
public synchronized boolean iscompleted() {
return counter == 100;
}
如果任务正在运行,isrunning()方法将返回true:
public synchronized boolean isrunning() {
return running;
}
setrunning()方法由start.jsp或stop.jsp调用,当running参数是true时。setrunning()方法还要将任务标记为“已经启动”。调用setrunning(false)表示要求run()方法停止执行。
public synchronized void setrunning(boolean running) {
this.running = running;
if (running)
started = true;
}
任务执行完毕后,调用getresult()方法返回计算结果;如果任务尚未执行完毕,它返回null:
public synchronized object getresult() {
if (iscompleted())
return new integer(sum);
else
return null;
}
当running标记为true、completed标记为false时,run()方法调用work()。在实际应用中,run()方法也许要执行复杂的sql查询、解析大型xml文档,或者调用消耗大量cpu时间的ejb方法。注意“繁重的任务”可能要在远程服务器上执行。报告结果的jsp页面有两种选择:或者等待任务结束,或者使用一个进度条。
public void run() {
try {
setrunning(true);
while (isrunning() && !iscompleted())
work();
} finally {
setrunning(false);
}
}
Java Asp PHP .Net XML C/C++ CGI VB Jsp J2ee J2se J2me EJB Servlet Tomcat Resin Struts Weblogic Eclipse ANT GUI JMS Web servise IDEA Webphere Hibernate Spring Jboss Applet Swing Socket Javamail Perl Ajax P2P 安全 模式 框架 测试 开源 游戏
Windows XP Windows 2000 Windows 2003 Windows Me Windows 9.x Linux UNIX 注册表 操作系统 服务器 应用服务器