Ignore:
Timestamp:
2016-01-09T15:41:47+01:00 (4 years ago)
Author:
simon04
Message:

Refactoring: use Fork/Join Tasks for parallel execution

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/tools/Utils.java

    r9297 r9351  
    5151import java.util.concurrent.ExecutorService;
    5252import java.util.concurrent.Executors;
     53import java.util.concurrent.ForkJoinPool;
     54import java.util.concurrent.ForkJoinWorkerThread;
    5355import java.util.concurrent.ThreadFactory;
    5456import java.util.concurrent.atomic.AtomicLong;
     
    14111413
    14121414    /**
    1413      * Returns a pair containing the number of threads (n), and a thread pool (if n > 1) to perform
    1414      * multi-thread computation in the context of the given preference key.
    1415      * @param pref The preference key
     1415     * Returns a {@link ForkJoinPool} with the parallelism given by the preference key.
     1416     * @param pref The preference key to determine parallelism
    14161417     * @param nameFormat see {@link #newThreadFactory(String, int)}
    14171418     * @param threadPriority see {@link #newThreadFactory(String, int)}
    1418      * @return a pair containing the number of threads (n), and a thread pool (if n > 1, null otherwise)
    1419      * @since 7423
    1420      */
    1421     public static Pair<Integer, ExecutorService> newThreadPool(String pref, String nameFormat, int threadPriority) {
     1419     * @return a {@link ForkJoinPool}
     1420     */
     1421    public static ForkJoinPool newForkJoinPool(String pref, final String nameFormat, final int threadPriority) {
    14221422        int noThreads = Main.pref.getInteger(pref, Runtime.getRuntime().availableProcessors());
    1423         ExecutorService pool = noThreads <= 1 ? null : Executors.newFixedThreadPool(noThreads, newThreadFactory(nameFormat, threadPriority));
    1424         return new Pair<>(noThreads, pool);
     1423        return new ForkJoinPool(noThreads, new ForkJoinPool.ForkJoinWorkerThreadFactory() {
     1424            final AtomicLong count = new AtomicLong(0);
     1425            @Override
     1426            public ForkJoinWorkerThread newThread(ForkJoinPool pool) {
     1427                final ForkJoinWorkerThread thread = ForkJoinPool.defaultForkJoinWorkerThreadFactory.newThread(pool);
     1428                thread.setName(String.format(Locale.ENGLISH, nameFormat, count.getAndIncrement()));
     1429                thread.setPriority(threadPriority);
     1430                return thread;
     1431            }
     1432        }, null, true);
    14251433    }
    14261434
Note: See TracChangeset for help on using the changeset viewer.