You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@rocketmq.apache.org by GitBox <gi...@apache.org> on 2019/03/17 03:46:54 UTC

[GitHub] [rocketmq] mypgether opened a new issue #1092: why use SingleThreadScheduledExecutor, may cause bug?

mypgether opened a new issue #1092: why use SingleThreadScheduledExecutor, may cause bug?
URL: https://github.com/apache/rocketmq/issues/1092
 
 
   Hello, When I read rocketmq source code, I found In MQClientInstance class, scheduledExecutorService variable is instance of **SingleThreadScheduledExecutor**. And In startScheduledTask method it submit almost _4 fixedRate scheduledTasks_. 
   
   I had a test on SingleThreadScheduledExecutor, it show **if one task is timeout or blocking, may cause other tasks delay, and not working as fixedRate writting in code.** So  I wonder why use SingleThreadScheduledExecutor?
   
   Here is the source code and log.
   
   ```
   import java.util.concurrent.Executors;
   import java.util.concurrent.ScheduledExecutorService;
   import java.util.concurrent.TimeUnit;
   import lombok.extern.slf4j.Slf4j;
   import org.junit.Test;
   
   @Slf4j
   public class SingleThreadExecutorTest {
   
     private final ScheduledExecutorService scheduledExecutorService = Executors
         .newSingleThreadScheduledExecutor(r -> new Thread(r, "singleThread"));
   
     @Test
     public void testSingle() throws InterruptedException {
       scheduledExecutorService.scheduleAtFixedRate(() -> {
         log.info("start working a ,threadId: {},threadName: {}", Thread.currentThread().getId(),
             Thread.currentThread().getName());
       }, 10, 2000 * 1, TimeUnit.MILLISECONDS);
   
       scheduledExecutorService.scheduleAtFixedRate(() -> {
         log.info("start working b ,threadId: {},threadName: {}", Thread.currentThread().getId(),
             Thread.currentThread().getName());
   
         try {
           TimeUnit.SECONDS.sleep(2);
         } catch (InterruptedException e) {
           e.printStackTrace();
         }
       }, 10, 1000 * 1, TimeUnit.MILLISECONDS);
   
       scheduledExecutorService.scheduleAtFixedRate(() -> {
         log.info("start working c ,threadId: {},threadName: {}", Thread.currentThread().getId(),
             Thread.currentThread().getName());
       }, 10, 2000 * 1, TimeUnit.MILLISECONDS);
   
       Thread.sleep(10000);
     }
   }
   ```
   ```
   2019-03-17 11:32:52 [singleThread] INFO  c.g.r.t.r.SingleThreadExecutorTest - start working a ,threadId: 14,threadName: singleThread
   2019-03-17 11:32:52 [singleThread] INFO  c.g.r.t.r.SingleThreadExecutorTest - start working b ,threadId: 14,threadName: singleThread
   2019-03-17 11:32:54 [singleThread] INFO  c.g.r.t.r.SingleThreadExecutorTest - start working c ,threadId: 14,threadName: singleThread
   2019-03-17 11:32:54 [singleThread] INFO  c.g.r.t.r.SingleThreadExecutorTest - start working b ,threadId: 14,threadName: singleThread
   2019-03-17 11:32:56 [singleThread] INFO  c.g.r.t.r.SingleThreadExecutorTest - start working a ,threadId: 14,threadName: singleThread
   2019-03-17 11:32:56 [singleThread] INFO  c.g.r.t.r.SingleThreadExecutorTest - start working b ,threadId: 14,threadName: singleThread
   2019-03-17 11:32:58 [singleThread] INFO  c.g.r.t.r.SingleThreadExecutorTest - start working c ,threadId: 14,threadName: singleThread
   2019-03-17 11:32:58 [singleThread] INFO  c.g.r.t.r.SingleThreadExecutorTest - start working b ,threadId: 14,threadName: singleThread
   2019-03-17 11:33:00 [singleThread] INFO  c.g.r.t.r.SingleThreadExecutorTest - start working a ,threadId: 14,threadName: singleThread
   2019-03-17 11:33:00 [singleThread] INFO  c.g.r.t.r.SingleThreadExecutorTest - start working b ,threadId: 14,threadName: singleThread
   Disconnected from the target VM, address: '127.0.0.1:61138', transport: 'socket'
   Process finished with exit code 0
   ```

----------------------------------------------------------------
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.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services