Changeset 5134 in josm


Ignore:
Timestamp:
Mar 30, 2012 4:26:43 PM (14 months ago)
Author:
simon04
Message:

see #6964 - perform several initialization tasks in parallel in order to speedup startup

File:
1 edited

Legend:

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

    r5112 r5134  
    2222import java.util.Map; 
    2323import java.util.StringTokenizer; 
     24import java.util.concurrent.Callable; 
    2425import java.util.concurrent.ExecutorService; 
     26import java.util.concurrent.Executors; 
    2527import java.util.concurrent.Future; 
    26 import java.util.regex.Matcher; 
    27 import java.util.regex.Pattern; 
    2828 
    2929import javax.swing.Action; 
     
    148148     * The main menu bar at top of screen. 
    149149     */ 
    150     public final MainMenu menu; 
    151  
    152     public final OsmValidator validator; 
     150    public MainMenu menu; 
     151 
     152    public OsmValidator validator; 
    153153    /** 
    154154     * The MOTD Layer. 
     
    247247        platform.startupHook(); 
    248248 
    249         // We try to establish an API connection early, so that any API 
    250         // capabilities are already known to the editor instance. However 
    251         // if it goes wrong that's not critical at this stage. 
    252         if (initListener != null) { 
    253             initListener.updateStatus(tr("Initializing OSM API")); 
    254         } 
     249        // contains several initialization tasks to be executed (in parallel) by a ExecutorService 
     250        List<Callable<Void>> tasks = new ArrayList<Callable<Void>>(); 
     251 
     252        tasks.add(new Callable<Void>() { 
     253 
     254            @Override 
     255            public Void call() throws Exception { 
     256                // We try to establish an API connection early, so that any API 
     257                // capabilities are already known to the editor instance. However 
     258                // if it goes wrong that's not critical at this stage. 
     259                if (initListener != null) { 
     260                    initListener.updateStatus(tr("Initializing OSM API")); 
     261                } 
     262                try { 
     263                    OsmApi.getOsmApi().initialize(null, true); 
     264                } catch (Exception x) { 
     265                    // ignore any exception here. 
     266                } 
     267                return null; 
     268            } 
     269        }); 
     270 
     271        tasks.add(new Callable<Void>() { 
     272 
     273            @Override 
     274            public Void call() throws Exception { 
     275                if (initListener != null) { 
     276                    initListener.updateStatus(tr("Building main menu")); 
     277                } 
     278                contentPanePrivate.add(panel, BorderLayout.CENTER); 
     279                panel.add(gettingStarted, BorderLayout.CENTER); 
     280                menu = new MainMenu(); 
     281 
     282                undoRedo.addCommandQueueListener(redoUndoListener); 
     283 
     284                // creating toolbar 
     285                contentPanePrivate.add(toolbar.control, BorderLayout.NORTH); 
     286 
     287                registerActionShortcut(menu.help, Shortcut.registerShortcut("system:help", tr("Help"), 
     288                        KeyEvent.VK_F1, Shortcut.DIRECT)); 
     289 
     290                return null; 
     291            } 
     292        }); 
     293 
     294        tasks.add(new Callable<Void>() { 
     295 
     296            @Override 
     297            public Void call() throws Exception { 
     298                if (initListener != null) { 
     299                    initListener.updateStatus(tr("Initializing presets")); 
     300                } 
     301                TaggingPresetPreference.initialize(); 
     302                return null; 
     303            } 
     304        }); 
     305 
     306        tasks.add(new Callable<Void>() { 
     307 
     308            @Override 
     309            public Void call() throws Exception { 
     310                if (initListener != null) { 
     311                    initListener.updateStatus(tr("Initializing map styles")); 
     312                } 
     313                MapPaintPreference.initialize(); 
     314                return null; 
     315            } 
     316        }); 
     317 
     318        tasks.add(new Callable<Void>() { 
     319 
     320            @Override 
     321            public Void call() throws Exception { 
     322                if (initListener != null) { 
     323                    initListener.updateStatus(tr("Loading imagery preferences")); 
     324                } 
     325                ImageryPreference.initialize(); 
     326                return null; 
     327            } 
     328        }); 
     329 
     330        tasks.add(new Callable<Void>() { 
     331 
     332            @Override 
     333            public Void call() throws Exception { 
     334                if (initListener != null) { 
     335                    initListener.updateStatus(tr("Initializing validator")); 
     336                } 
     337                validator = new OsmValidator(); 
     338                MapView.addLayerChangeListener(validator); 
     339                return null; 
     340            } 
     341        }); 
     342 
    255343        try { 
    256             OsmApi.getOsmApi().initialize(null, true); 
    257         } catch (Exception x) { 
    258             // ignore any exception here. 
    259         } 
    260  
    261         if (initListener != null) { 
    262             initListener.updateStatus(tr("Building main menu")); 
    263         } 
    264         contentPanePrivate.add(panel, BorderLayout.CENTER); 
    265         panel.add(gettingStarted, BorderLayout.CENTER); 
    266         menu = new MainMenu(); 
    267  
    268         undoRedo.addCommandQueueListener(redoUndoListener); 
    269  
    270         // creating toolbar 
    271         contentPanePrivate.add(toolbar.control, BorderLayout.NORTH); 
    272  
    273         registerActionShortcut(menu.help, Shortcut.registerShortcut("system:help", tr("Help"), 
    274                 KeyEvent.VK_F1, Shortcut.DIRECT)); 
    275  
    276         if (initListener != null) { 
    277             initListener.updateStatus(tr("Initializing presets")); 
    278         } 
    279         TaggingPresetPreference.initialize(); 
    280  
    281         if (initListener != null) { 
    282             initListener.updateStatus(tr("Initializing map styles")); 
    283         } 
    284         MapPaintPreference.initialize(); 
    285  
    286         if (initListener != null) { 
    287             initListener.updateStatus(tr("Loading imagery preferences")); 
    288         } 
    289         ImageryPreference.initialize(); 
    290  
    291         if (initListener != null) { 
    292             initListener.updateStatus(tr("Initializing validator")); 
    293         } 
    294         validator = new OsmValidator(); 
    295         MapView.addLayerChangeListener(validator); 
     344            Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors()).invokeAll(tasks); 
     345        } catch (InterruptedException ex) { 
     346            throw new RuntimeException(ex); 
     347        } 
    296348 
    297349        // hooks for the jmapviewer component 
Note: See TracChangeset for help on using the changeset viewer.