Polaris
Polaris

Reputation: 396

Java thread start delay

I use JAVA ExecutorService to parallel my task. And there are 48 available processors and each processor has 12 cores. So I suppose that I can start more than 500 threads simultaneously (500 > 12 * 48).

Here is the detailed info

    processor   : 47
    vendor_id   : GenuineIntel
    cpu family  : 6
    model       : 85
    model name  : Intel(R) Xeon(R) Gold 5118 CPU @ 2.30GHz
    stepping    : 4
    microcode   : 0x2000043
    cpu MHz     : 999.985
    cache size  : 16896 KB
    physical id : 1
    siblings    : 24
    core id     : 11
    cpu cores   : 12
    apicid      : 55
    initial apicid  : 55
    fpu     : yes
    fpu_exception   : yes
    cpuid level : 22
    wp      : yes

And I try to parallel the following task. The code looks like,

    int numThread = 500;
    ExecutorService executorService = Executors.newFixedThreadPool(numThread);
    long startParallel = System.currentTimeMillis();
    List<Callable<List<Integer>>> callableTasks = new ArrayList<>();
    for (int i = 0; i < numThread; i++) {
        final int startTupleIndex = i * batchSize;
        final int endTupleIndex = (i + 1) * batchSize;
        callableTasks.add(() -> {
            List<Integer> batchResult = new ArrayList<>();
            long start = System.currentTimeMillis();
            for (int j = startTupleIndex; j < endTupleIndex; j++) {
                //Some evaluation function
            }
            long end = System.currentTimeMillis();
            System.out.println("index from "+startTupleIndex+" to "+endTupleIndex+" ,time from "
                    +start+" to "+end+", duration " + (end - start) +"ms");
            return batchResult;
        });
    }


    List<Future<List<Integer>>> results = executorService.invokeAll(callableTasks);
    for (Future<List<Integer>> result : results)
        validTuples.addAll(result.get());
    } catch (InterruptedException | ExecutionException e) {
        e.printStackTrace();
    }

    executorService.shutdown();
    long endParallel = System.currentTimeMillis();
    System.out.println("total ms:"+ (endParallel - startParallel));

However, the start time of different threads varies a lot.

    index from 144978 to 217467 ,time from 1537729372817 to 1537729372984, duration 167ms
    index from 1159824 to 1232313 ,time from 1537729372819 to 1537729372985, duration 166ms
    index from 1377291 to 1449780 ,time from 1537729372819 to 1537729372987, duration 168ms
    index from 434934 to 507423 ,time from 1537729372818 to 1537729372990, duration 172ms
    index from 942357 to 1014846 ,time from 1537729372819 to 1537729372992, duration 173ms
    index from 579912 to 652401 ,time from 1537729372818 to 1537729372993, duration 175ms
    index from 724890 to 797379 ,time from 1537729372818 to 1537729372996, duration 178ms
    ...
    index from 34794720 to 34867209 ,time from 1537729374532 to 1537729374733, duration 201ms
    index from 36099522 to 36172011 ,time from 1537729374627 to 1537729374733, duration 106ms
    index from 36172011 to 36244344 ,time from 1537729374628 to 1537729374734, duration 106ms
    index from 35302143 to 35374632 ,time from 1537729374563 to 1537729374741, duration 178ms
    index from 35882055 to 35954544 ,time from 1537729374619 to 1537729374741, duration 122ms
    index from 35447121 to 35519610 ,time from 1537729374579 to 1537729374742, duration 163ms
    index from 35157165 to 35229654 ,time from 1537729374551 to 1537729374745, duration 194ms
    index from 35737077 to 35809566 ,time from 1537729374595 to 1537729374746, duration 151ms
    index from 35519610 to 35592099 ,time from 1537729374594 to 1537729374747, duration 153ms
    index from 35374632 to 35447121 ,time from 1537729374587 to 1537729374747, duration 160ms
    index from 35664588 to 35737077 ,time from 1537729374595 to 1537729374748, duration 153ms
    index from 35592099 to 35664588 ,time from 1537729374595 to 1537729374751, duration 156ms
    index from 35809566 to 35882055 ,time from 1537729374619 to 1537729374752, duration 133ms
    total ms:1939.

The problem is that each thread is finished around 200ms, but the total running time takes more than 1900ms. The tasks start time roughly vary from 1537729372817 to 1537729374628. But there are more than 500 available physical cores. Why do those tasks not start concurrently? Thanks a lot.

Upvotes: 2

Views: 483

Answers (1)

Tommy Brettschneider
Tommy Brettschneider

Reputation: 1490

After calling invokeAll on the executorservice there's no guarantee that execution of all tasks will start at the same time. Thread scheduling is highly OS specific and the only thing you can rely on is that you cannot rely on things like "when does a thread really start", "how long will it run"... take for example Java's thread state 'Runnable':

"A thread that is ready to run is moved to runnable state. In this state, a thread might actually be running or it might be ready run at any instant of time. It is the responsibility of the thread scheduler to give the thread, time to run."

https://www.geeksforgeeks.org/lifecycle-and-states-of-a-thread-in-java/

Upvotes: 1

Related Questions