[英]How to stop multiple threads in the sling threadpool from executing the same statement?
How do I prevent threads in the sling threadpool from executing the statement: serv.getService().start( tic, info ); 如何防止吊索线程池中的线程执行以下语句:serv.getService()。start(tic,info); multiple times if the execution has already been completed by one thread?
如果一个线程已经完成了多次执行?
I tried using a flag, _alreadyExecuted but that does not seem to help. 我尝试使用_alreadyExecuted标志,但这似乎无济于事。
Log: https://i.stack.imgur.com/x8jZw.png 日志: https : //i.stack.imgur.com/x8jZw.png
My code is as follows, 我的代码如下,
private void sendBlock( service serv, String tic, Resource block,
Update update, Info info, ResourceResolver resolver ) throws
IOException {
private Boolean _alreadyExecuted = Boolean.FALSE;
if( tic != null ) {
String tic = null;
if(!_alreadyExecuted){
getLogger().info("Before startSubmission. Currently _alreadyexecuted is " +_alreadyExecuted);
serv.getService().start( tic, info );
getLogger().info("After startSubmission, _alreadyexecuted is about to change to true. Currently _alreadyexecuted is " +_alreadyExecuted);
_alreadyExecuted = Boolean.TRUE;
getLogger().info("After startSubmission, _alreadyexecuted has been changed to true. Currently _alreadyexecuted is " +_alreadyExecuted);
}
}
}
Change the field to volatile
: 将字段更改为
volatile
:
private volatile Boolean _alreadyExecuted = Boolean.FALSE;
In a multi-threaded execution environment, threads are allowed cache the value of a field (and usually do). 在多线程执行环境中,允许线程缓存字段的值(通常这样做)。 The
volatile
keyword ensures all threads use the current (same) value assigned to the field. volatile
关键字确保所有线程都使用分配给该字段的当前(相同)值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.