Ignore:
Timestamp:
2018-08-12T02:52:44+02:00 (6 years ago)
Author:
Don-vip
Message:

see #15229 - move Main initialization methods to lifecycle SPI + new class MainInitialization

Location:
trunk/src/org/openstreetmap/josm/spi/lifecycle
Files:
1 added
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/spi/lifecycle/Lifecycle.java

    r14131 r14139  
    22package org.openstreetmap.josm.spi.lifecycle;
    33
     4import java.util.List;
    45import java.util.Objects;
     6import java.util.concurrent.ExecutionException;
     7import java.util.concurrent.ExecutorService;
     8import java.util.concurrent.Executors;
     9import java.util.concurrent.Future;
     10
     11import org.openstreetmap.josm.tools.Logging;
     12import org.openstreetmap.josm.tools.Utils;
     13import org.openstreetmap.josm.tools.bugreport.BugReport;
    514
    615/**
     
    3140        initStatusListener = Objects.requireNonNull(listener);
    3241    }
     42
     43    /**
     44     * Initializes the main object. A lot of global variables are initialized here.
     45     * @param initSequence Initialization sequence
     46     * @since 14139
     47     */
     48    public static void initialize(InitializationSequence initSequence) {
     49        // Initializes tasks that must be run before parallel tasks
     50        runInitializationTasks(initSequence.beforeInitializationTasks());
     51
     52        // Initializes tasks to be executed (in parallel) by a ExecutorService
     53        try {
     54            ExecutorService service = Executors.newFixedThreadPool(
     55                    Runtime.getRuntime().availableProcessors(), Utils.newThreadFactory("main-init-%d", Thread.NORM_PRIORITY));
     56            for (Future<Void> i : service.invokeAll(initSequence.parallelInitializationTasks())) {
     57                i.get();
     58            }
     59            // asynchronous initializations to be completed eventually
     60            initSequence.asynchronousRunnableTasks().forEach(service::submit);
     61            initSequence.asynchronousCallableTasks().forEach(service::submit);
     62            try {
     63                service.shutdown();
     64            } catch (SecurityException e) {
     65                Logging.log(Logging.LEVEL_ERROR, "Unable to shutdown executor service", e);
     66            }
     67        } catch (InterruptedException | ExecutionException ex) {
     68            throw new RuntimeException(ex);
     69        }
     70
     71        // Initializes tasks that must be run after parallel tasks
     72        runInitializationTasks(initSequence.afterInitializationTasks());
     73    }
     74
     75    private static void runInitializationTasks(List<InitializationTask> tasks) {
     76        for (InitializationTask task : tasks) {
     77            try {
     78                task.call();
     79            } catch (RuntimeException e) {
     80                // Can happen if the current projection needs NTV2 grid which is not available
     81                // In this case we want the user be able to change his projection
     82                BugReport.intercept(e).warn();
     83            }
     84        }
     85    }
    3386}
Note: See TracChangeset for help on using the changeset viewer.