Ticket #4626: 4626_alpha.patch
File 4626_alpha.patch, 6.7 KB (added by , 11 years ago) |
---|
-
core/src/org/openstreetmap/josm/Main.java
27 27 import java.util.StringTokenizer; 28 28 import java.util.concurrent.Callable; 29 29 import java.util.concurrent.ExecutorService; 30 import java.util.concurrent.Executors;31 30 import java.util.concurrent.Future; 32 31 33 32 import javax.swing.Action; … … 374 373 } 375 374 }); 376 375 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); 385 377 386 378 // hooks for the jmapviewer component 387 379 FeatureAdapter.registerBrowserAdapter(new FeatureAdapter.BrowserAdapter() { -
core/src/org/openstreetmap/josm/actions/ValidateAction.java
9 9 import java.util.ArrayList; 10 10 import java.util.Collection; 11 11 import java.util.List; 12 import java.util.concurrent.Callable; 12 13 13 14 import org.openstreetmap.josm.Main; 14 15 import org.openstreetmap.josm.data.osm.OsmPrimitive; … … 21 22 import org.openstreetmap.josm.gui.util.GuiHelper; 22 23 import org.openstreetmap.josm.io.OsmTransferException; 23 24 import org.openstreetmap.josm.tools.Shortcut; 25 import org.openstreetmap.josm.tools.Utils; 24 26 import org.xml.sax.SAXException; 25 27 26 28 /** … … 160 162 errors = new ArrayList<TestError>(200); 161 163 getProgressMonitor().setTicksCount(tests.size() * validatedPrimitives.size()); 162 164 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 }); 173 184 } 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); 174 190 tests = null; 175 191 if (Main.pref.getBoolean(ValidatorPreference.PREF_USE_IGNORE, true)) { 176 192 getProgressMonitor().subTask(tr("Updating ignored errors ...")); -
core/src/org/openstreetmap/josm/tools/Utils.java
26 26 import java.util.Collection; 27 27 import java.util.Iterator; 28 28 import java.util.List; 29 import java.util.concurrent.Callable; 30 import java.util.concurrent.Executors; 31 import java.util.concurrent.Future; 29 32 30 33 import org.openstreetmap.josm.data.Version; 34 import org.openstreetmap.josm.gui.progress.ProgressMonitor; 31 35 32 36 /** 33 37 * Basic utils, that can be useful in different parts of the program. … … 572 576 } 573 577 return connection; 574 578 } 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 } 575 618 }