szetszwo commented on PR #955:
URL: https://github.com/apache/ratis/pull/955#issuecomment-1784208441

   Another way is to use `CompletableFuture` as below.  It may be easier to 
understand.
   ```java
       private static final 
AtomicReference<CompletableFuture<ReferenceCountedObject<EventLoopGroup>>> 
SHARED_WORKER_GROUP
           = new AtomicReference<>();
   
       static WorkerGroupGetter newInstance(RaftProperties properties) {
         final boolean shared = 
NettyConfigKeys.DataStream.Client.workerGroupShare(properties);
         if (shared) {
           final CompletableFuture<ReferenceCountedObject<EventLoopGroup>> 
created = new CompletableFuture<>();
           final CompletableFuture<ReferenceCountedObject<EventLoopGroup>> 
current
               = SHARED_WORKER_GROUP.updateAndGet(g -> g != null ? g : created);
           if (current == created) {
             
created.complete(ReferenceCountedObject.wrap(newWorkerGroup(properties)));
           }
           return new WorkerGroupGetter(current.join().retain()) {
             @Override
             void shutdownGracefully() {
               final CompletableFuture<ReferenceCountedObject<EventLoopGroup>> 
returned
                   = SHARED_WORKER_GROUP.updateAndGet(previous -> {
                 Preconditions.assertSame(current, previous, 
"SHARED_WORKER_GROUP");
                 return previous.join().release() ? null : previous;
               });
               if (returned == null) {
                 workerGroup.shutdownGracefully();
               }
             }
           };
         } else {
           return new WorkerGroupGetter(newWorkerGroup(properties));
         }
       }
   ```


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to