我一直在尝试在我的 java servlet 中执行延迟 1 秒的代码。我需要检查跟踪是否打开或关闭。如果它关闭,那么它会转到其他并关闭调度程序。代码如下。
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
Map m=request.getParameterMap();
Set s = m.entrySet();
Iterator it = s.iterator();
int index=0;
while(it.hasNext()){
Map.Entry<String,String[]> entry = (Map.Entry<String,String[]>)it.next();
String key = entry.getKey();
String[] value = entry.getValue();
System.out.println("Value is "+value[0].toString());
switch(key)
{
case "RegId":
RegId=value[0].toString();
break;
case "isTrackingRequested":
isTrackingRequested=Boolean.valueOf(value[0]);
break;
}
}
boolean isTrackingRequestednew=isTrackingRequested;
ScheduledExecutorService ses = Executors.newSingleThreadScheduledExecutor();
ses.scheduleAtFixedRate(new Runnable() {
@Override
public void run() {
// code to run
if(isTrackingRequestednew){
try {
System.out.println("===========================================================================");
System.out.println("new track status is "+isTrackingRequestednew);
System.out.println("===========================================================================");
}catch(Exception e)
{
}
}
else
{
ses.shutdown();
}
}
}, 0, 1, TimeUnit.SECONDS);
}
现在为了停止跟踪,我的应用程序将 isTrackingRequestednew 发送为“false”,现在这个值根本没有改变。我不知道为什么会这样。请帮助我。
最佳答案
此代码无法编译,您无法访问内部类中的本地(非最终)变量。
每次发布请求时,您都会创建新的 ExecutorService,而不是每个 session 或跟踪实体创建一次。我不知道这个帖子的目的是什么,所以我会保存你奇怪的代码风格
private static class TrackingInfo {
final private AtomicBoolean status;
final private ScheduledExecutorService ses;
TrackingInfo(boolean flagStatus) {
this.status = new AtomicBoolean(flagStatus);
this.ses = Executors.newSingleThreadScheduledExecutor();
ses.scheduleAtFixedRate(new Runnable() {
@Override
public void run() {
// code to run
if (status.get()) {
try {
System.out.println("===========================================================================");
System.out.println("new track status is " + status.get());
System.out.println("===========================================================================");
} catch (Exception e) {
}
} else {
ses.shutdown();
}
}
}, 0, 1, TimeUnit.SECONDS);
}
public void setStatus(boolean status) {
this.status.set(status);
}
}
使用 request.getSession().getAttribute(...)/setAttribute()
来保存此 TrackingInfo 和其中的工作程序,并通过 TrackingInfo.setStatus(newStatus)
实例将标志更改传递给工作程序,或者您可以在 Controller 类中使用一些 Map 变量(不是方法局部变量)并存储与其关联的跟踪 id 和 TrackingInfo
。
恕我直言,如果您真正的跟踪线程终止与发布的代码中一样简单
else {
ses.shutdown();
}
您根本不需要TrackingInfo
。只需存储(如上所述在 session 或缓存中)对调度程序的引用,然后您在 doPost
方法中收到带有 false 值的 isTrackingRequestednew
,获取此调度程序并像这样关闭它
if (!isTrackingRequestednew) {
ScheduledExecutorService scheduler = (ScheduledExecutorService) request.getSession().getAttribute("trackingScheduler");
if (scheduler != null) {
scheduler.shutdown();
}
}
相反,您可以使用一些跟踪 ID 作为标识符,并在每个请求中发送它。
请注意,您还必须清理由于某些网络错误或其他原因而未正确关闭的旧调度程序。
关于java - ScheduledExecutorService 不显示变量的变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34432728/