Ticket #4626: 4626_alpha.patch

File 4626_alpha.patch, 6.7 KB (added by Don-vip, 11 years ago)

Some work in progress I had some time ago. Solution may be need to be redesigned to implement UI stuff

  • core/src/org/openstreetmap/josm/Main.java

     
    2727import java.util.StringTokenizer;
    2828import java.util.concurrent.Callable;
    2929import java.util.concurrent.ExecutorService;
    30 import java.util.concurrent.Executors;
    3130import java.util.concurrent.Future;
    3231
    3332import javax.swing.Action;
     
    374373            }
    375374        });
    376375
    377         try {
    378             for (Future<Void> i : Executors.newFixedThreadPool(
    379                     Runtime.getRuntime().availableProcessors()).invokeAll(tasks)) {
    380                 i.get();
    381             }
    382         } catch (Exception ex) {
    383             throw new RuntimeException(ex);
    384         }
     376        Utils.runMultiThread(tasks);
    385377
    386378        // hooks for the jmapviewer component
    387379        FeatureAdapter.registerBrowserAdapter(new FeatureAdapter.BrowserAdapter() {
  • core/src/org/openstreetmap/josm/actions/ValidateAction.java

     
    99import java.util.ArrayList;
    1010import java.util.Collection;
    1111import java.util.List;
     12import java.util.concurrent.Callable;
    1213
    1314import org.openstreetmap.josm.Main;
    1415import org.openstreetmap.josm.data.osm.OsmPrimitive;
     
    2122import org.openstreetmap.josm.gui.util.GuiHelper;
    2223import org.openstreetmap.josm.io.OsmTransferException;
    2324import org.openstreetmap.josm.tools.Shortcut;
     25import org.openstreetmap.josm.tools.Utils;
    2426import org.xml.sax.SAXException;
    2527
    2628/**
     
    160162            errors = new ArrayList<TestError>(200);
    161163            getProgressMonitor().setTicksCount(tests.size() * validatedPrimitives.size());
    162164            int testCounter = 0;
    163             for (Test test : tests) {
    164                 if (canceled)
    165                     return;
    166                 testCounter++;
    167                 getProgressMonitor().setCustomText(tr("Test {0}/{1}: Starting {2}", testCounter, tests.size(),test.getName()));
    168                 test.setPartialSelection(formerValidatedPrimitives != null);
    169                 test.startTest(getProgressMonitor().createSubTaskMonitor(validatedPrimitives.size(), false));
    170                 test.visit(validatedPrimitives);
    171                 test.endTest();
    172                 errors.addAll(test.getErrors());
     165            double start = System.currentTimeMillis();
     166            List<Callable<List<TestError>>> tasks = new ArrayList<Callable<List<TestError>>>();
     167            List<String> messages = new ArrayList<String>();
     168            for (final Test test : tests) {
     169                final String text = tr("Test {0}/{1}: Starting {2}", ++testCounter, tests.size(),test.getName());
     170                messages.add(text);
     171                tasks.add(new Callable<List<TestError>>() {
     172                    @Override
     173                    public List<TestError> call() throws Exception {
     174                        if (canceled)
     175                            return null;
     176                        //getProgressMonitor().setCustomText(text);
     177                        test.setPartialSelection(formerValidatedPrimitives != null);
     178                        test.startTest(getProgressMonitor().createSubTaskMonitor(validatedPrimitives.size(), false));
     179                        test.visit(validatedPrimitives);
     180                        test.endTest();
     181                        return test.getErrors();
     182                    }
     183                });
    173184            }
     185            for (List<TestError> result : Utils.runMultiThread(tasks/*, getProgressMonitor(), messages*/)) {
     186                errors.addAll(result);
     187            }
     188            double end = System.currentTimeMillis();
     189            System.out.println(end-start);
    174190            tests = null;
    175191            if (Main.pref.getBoolean(ValidatorPreference.PREF_USE_IGNORE, true)) {
    176192                getProgressMonitor().subTask(tr("Updating ignored errors ..."));
  • core/src/org/openstreetmap/josm/tools/Utils.java

     
    2626import java.util.Collection;
    2727import java.util.Iterator;
    2828import java.util.List;
     29import java.util.concurrent.Callable;
     30import java.util.concurrent.Executors;
     31import java.util.concurrent.Future;
    2932
    3033import org.openstreetmap.josm.data.Version;
     34import org.openstreetmap.josm.gui.progress.ProgressMonitor;
    3135
    3236/**
    3337 * Basic utils, that can be useful in different parts of the program.
     
    572576        }
    573577        return connection;
    574578    }
     579   
     580    /**
     581     * Runs a list of tasks in parallel (with the same number of threads as the number of available processors) and
     582     * waits for completion (success, cancellation or interruption).
     583     * @param tasks The list of tasks to run
     584     * @param monitor The progress monitor. May be null.
     585     * @param messages The list of messages used to update the progress monitor. Must have the same size as tasks list. Ignored if monitor is null.
     586     * @return The list of results
     587     * @throws RuntimeException If any exception happens
     588     * @since 5674
     589     */
     590    public static <T> List<T> runMultiThread(List<Callable<T>> tasks, ProgressMonitor monitor, List<String> messages) throws RuntimeException {
     591        try {
     592            List<T> results = new ArrayList<T>();
     593            int i = 0;
     594            for (Future<T> task : Executors.newFixedThreadPool(
     595                    Runtime.getRuntime().availableProcessors()).invokeAll(tasks)) {
     596                if (monitor != null && messages != null && messages.size() == tasks.size()) {
     597                    monitor.setCustomText(messages.get(i++));
     598                }
     599                results.add(task.get());
     600            }
     601            return results;
     602        } catch (Exception ex) {
     603            throw new RuntimeException(ex);
     604        }
     605    }
     606   
     607    /**
     608     * Runs a list of tasks in parallel (with the same number of threads as the number of available processors) and
     609     * waits for completion (success, cancellation or interruption).
     610     * @param tasks The list of tasks to run
     611     * @return The list of results
     612     * @throws RuntimeException If any exception happens
     613     * @since 5674
     614     */
     615    public static <T> List<T> runMultiThread(List<Callable<T>> tasks) throws RuntimeException {
     616        return runMultiThread(tasks, null, null);
     617    }
    575618}