bmwcarit / barefoot

Java map matching library for integrating the map into software and services with state-of-the-art online and offline map matching that can be used stand-alone and in the cloud.
Apache License 2.0
664 stars 185 forks source link

what is the function of scheduler package? #125

Open CallumWang opened 5 years ago

CallumWang commented 5 years ago

Hello: I found the schedular usage in Matcher.java. However, it seems that it is related to something about Cocurrent. What is the meaning of it in this "for block".

for (final MatcherCandidate predecessor : predecessors.two()) {
            scheduler.spawn(new Task() {
                @Override
               //something to run.
                }
            });
        }

If I don't use the schedular,what will happen?Does it make any different? Looking forward to your reply. Thanks a lot.

smattheis commented 5 years ago

The scheduler package is for parallelization of route computations. Since we usually have some number, say n, of matching candidates for each position measurement, we must calculate approximagely n^2 routes. The route computation is a single-source-multiple-target step such that we still have n such computations which we can parallelize here. Note, we have here a simple work-stealing adaptation implemented, which is the scheduler package, because thread pools of the JRE are AFAIK not designed for stream workloads, i.e., there is NO guarantee on the order of processing like a FIFO order such that there is no guarantee that one job finishes if you consistently add more jobs in a stream processing fashion. To answer your question more precisely, for each predecessor we spawn a task that does the single-source-multiple-target computation where tasks may be executed in parallel with respect to the number of worker threads or processor cores we have available. This is similar to: https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ForkJoinPool.html