1

The thread.run is delayed dozens of ms. I know it depends on the os's scheduler, but the delay in Executor version is much more smaller. What the different between Thread.start() and Executor.execute().

        final long start = System.currentTimeMillis();
        Thread thr = new Thread(() -> {
            long now = System.currentTimeMillis();
            System.out.printf("start: %d, now: %d, delay %d \n", start, now, now - start);
        });
        thr.start()

start: 1556463160295, now: 1556463160371, delay 76

        final long start = System.currentTimeMillis();
        ExecutorService pool = Executors.newFixedThreadPool(1);
        pool.execute(() ->{
            long now = System.currentTimeMillis();
            System.out.printf("start: %d, now: %d, delay %d \n", start, now, now - start);
        });

start: 1556465388875, now: 1556465388882, delay 7

Swap "ExecutorService pool = Executors.newFixedThreadPool(1);" and "start = System.currentTimeMillis();" and the test it again

        ExecutorService pool = Executors.newFixedThreadPool(1);
        final long start = System.currentTimeMillis();
        pool.execute(() ->{
            long now = System.currentTimeMillis();
            System.out.printf("start: %d, now: %d, delay %d \n", start, now, now - start);
        });

start: 1556465668982, now: 1556465668983, delay 1

As per Sotirios Delimanolis's comment, I found it only happens when these two pieces of code one after the other. But why the first one alway slow?

DQP
  • 21
  • 4
  • 1
    Use `System.nanoTime()`, it is more accurate and guaranteed to give reproducible results. – devgianlu Apr 28 '19 at 15:20
  • In second example try to swap `final long start = System.currentTimeMillis();` with `ExecutorService pool = Executors.newFixedThreadPool(1);`. – Pshemo Apr 28 '19 at 15:25
  • @Pshemo Thank you Pshemo, just update the result as per your suggestion, but the difference is still exists. – DQP Apr 28 '19 at 15:32
  • 1
    Are these two pieces of code one after the other? – Sotirios Delimanolis Apr 28 '19 at 15:34
  • 1
    The `ExecutorService` is keeping a set of threads warmed up for execution. For the threads inside `ExecutorService`, the things between `start` and `run` have already been run. – rdas Apr 28 '19 at 15:34
  • @SotiriosDelimanolis . Thank you Sotirios! You are correct. Why the first one is so slow. – DQP Apr 28 '19 at 15:42
  • Because initializing the infrastructure to evaluate the lambda expression is slow, once. – Sotirios Delimanolis Apr 28 '19 at 15:43
  • You won't see a difference with an anonymous class. – Sotirios Delimanolis Apr 28 '19 at 15:44
  • Hello Sotirios, Just try the anonmous class the different still exists. And another issue is the code of these two pieces are not same, why the code runs first are always slower than the second. – DQP Apr 28 '19 at 15:57
  • `thr = new Thread(...)` does not create a new thread. It only creates the new Java object that your program will use to manage the thread. The actual operating system thread is not created until your program calls `thr.start()`. Creating the thread takes time, and it takes time for the OS to decide to actually start executing the thread's code. – Solomon Slow Apr 29 '19 at 01:55

0 Answers0