java - 如何停止悬吊线程池中的多个线程执行同一条语句?

标签 java multithreading aem

如何防止吊索线程池中的线程执行以下语句:serv.getService()。start(tic,info);
如果执行已由一个线程完成多次?

我尝试使用_alreadyExecuted标志,但这似乎无济于事。

日志:/image/x8jZw.png

我的代码如下,

     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);
        }
    }
}

最佳答案

将字段更改为volatile:

 private volatile Boolean _alreadyExecuted = Boolean.FALSE;

在多线程执行环境中,允许线程缓存字段的值(通常这样做)。关键字volatile确保所有线程使用分配给该字段的当前(相同)值。

关于java - 如何停止悬吊线程池中的多个线程执行同一条语句?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45702132/

相关文章:

osgi - FOP/AEM : Deserialization not allowed for class org. apache.fop.fonts.FontCache

java - 如何处理Android应用程序的更多用户(拥有自己的帐户)?多线程?

java - 我不明白为什么这个简单的语句不起作用

java - Mongo 的 Java 驱动程序,如何构建复杂的查询

java - python有自动资源管理吗?

java - 自动编译线程类中的类。请解释

java - 如何使在主线程中运行的某个类同时等待其他类达到某种特定状态?

extjs - 如何将 xtype 值存储为字符串数组 CQ5/AEM

aem - 如何创建系统用户 (rep :SystemUser) in AEM 6. 1?

java - 通过 java 客户端的 netty websocket 连接