Changeset 8734 in josm for trunk/src/org


Ignore:
Timestamp:
2015-09-08T15:20:34+02:00 (9 years ago)
Author:
simon04
Message:

see #11843 - Give all started threads sensible names

Utils#newThreadFactory creates a ThreadFactory to be used when
obtaining a new Executor via Executors.new….

Location:
trunk/src/org/openstreetmap/josm
Files:
17 edited

Legend:

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

    r8513 r8734  
    168168     * and sequential.
    169169     */
    170     public static final ExecutorService worker = new ProgressMonitorExecutor();
     170    public static final ExecutorService worker = new ProgressMonitorExecutor("main-worker-%d", Thread.NORM_PRIORITY);
    171171
    172172    /**
     
    625625
    626626        try {
    627             for (Future<Void> i : Executors.newFixedThreadPool(
    628                     Runtime.getRuntime().availableProcessors()).invokeAll(tasks)) {
     627            final ExecutorService service = Executors.newFixedThreadPool(
     628                    Runtime.getRuntime().availableProcessors(), Utils.newThreadFactory("main-init-%d", Thread.NORM_PRIORITY));
     629            for (Future<Void> i : service.invokeAll(tasks)) {
    629630                i.get();
    630631            }
  • trunk/src/org/openstreetmap/josm/data/cache/JCSCachedTileLoaderJob.java

    r8673 r8734  
    7777            // make queue of LIFO type - so recently requested tiles will be loaded first (assuming that these are which user is waiting to see)
    7878            new LinkedBlockingDeque<Runnable>(),
    79             Utils.getNamedThreadFactory("JCS downloader")
     79            Utils.newThreadFactory("JCS-downloader-%d", Thread.NORM_PRIORITY)
    8080            );
    8181
  • trunk/src/org/openstreetmap/josm/data/imagery/TMSCachedTileLoader.java

    r8624 r8734  
    5050     * and for TMS imagery
    5151     */
    52     private static ThreadPoolExecutor DEFAULT_DOWNLOAD_JOB_DISPATCHER = getNewThreadPoolExecutor("TMS downloader");
     52    private static ThreadPoolExecutor DEFAULT_DOWNLOAD_JOB_DISPATCHER = getNewThreadPoolExecutor("TMS-downloader-%d");
    5353
    5454
     
    7474
    7575    /**
    76      * @param name name of the threads
     76     * @param nameFormat see {@link Utils#newThreadFactory(String, int)}
    7777     * @param workers number of worker thread to keep
    7878     * @return new ThreadPoolExecutor that will use a @see HostLimitQueue based queue
    7979     */
    80     public static ThreadPoolExecutor getNewThreadPoolExecutor(String name, int workers) {
     80    public static ThreadPoolExecutor getNewThreadPoolExecutor(String nameFormat, int workers) {
    8181        return new ThreadPoolExecutor(
    8282                workers, // keep the thread number constant
     
    8585                TimeUnit.SECONDS,
    8686                new HostLimitQueue(HOST_LIMIT.get().intValue()),
    87                 Utils.getNamedThreadFactory(name)
     87                Utils.newThreadFactory(nameFormat, Thread.NORM_PRIORITY)
    8888                );
    8989    }
  • trunk/src/org/openstreetmap/josm/data/imagery/WMSCachedTileLoader.java

    r8624 r8734  
    3434
    3535        super(listener, cache, connectTimeout, readTimeout, headers);
    36         setDownloadExecutor(TMSCachedTileLoader.getNewThreadPoolExecutor("WMS downloader", THREAD_LIMIT.get()));
     36        setDownloadExecutor(TMSCachedTileLoader.getNewThreadPoolExecutor("WMS-downloader-%d", THREAD_LIMIT.get()));
    3737    }
    3838
  • trunk/src/org/openstreetmap/josm/data/osm/MultipolygonBuilder.java

    r8510 r8734  
    3232
    3333    private static final Pair<Integer, ExecutorService> THREAD_POOL =
    34             Utils.newThreadPool("multipolygon_creation.numberOfThreads");
     34            Utils.newThreadPool("multipolygon_creation.numberOfThreads", "multipolygon-builder-%d", Thread.NORM_PRIORITY);
    3535
    3636    /**
  • trunk/src/org/openstreetmap/josm/data/osm/visitor/paint/StyledMapRenderer.java

    r8632 r8734  
    8787
    8888    private static final Pair<Integer, ExecutorService> THREAD_POOL =
    89             Utils.newThreadPool("mappaint.StyledMapRenderer.style_creation.numberOfThreads");
     89            Utils.newThreadPool("mappaint.StyledMapRenderer.style_creation.numberOfThreads", "styled-map-renderer-%d", Thread.NORM_PRIORITY);
    9090
    9191    /**
  • trunk/src/org/openstreetmap/josm/gui/io/SaveLayerTask.java

    r8510 r8734  
    1717 *     ExecutorService executorService = ...
    1818 *     SaveLayerTask task = new SaveLayerTask(layer, monitor);
    19  *     Future&lt;?&gt; taskFuture = executorServce.submit(task)
     19 *     Future&lt;?&gt; taskFuture = executorService.submit(task)
    2020 *     try {
    2121 *        // wait for the task to complete
    2222 *        taskFuture.get();
    2323 *     } catch (Exception e) {
    24  *        e.printStackTracek();
     24 *        e.printStackTrace();
    2525 *     }
    2626 * </pre>
  • trunk/src/org/openstreetmap/josm/gui/io/SaveLayersDialog.java

    r8510 r8734  
    5151import org.openstreetmap.josm.gui.util.GuiHelper;
    5252import org.openstreetmap.josm.tools.ImageProvider;
     53import org.openstreetmap.josm.tools.Utils;
    5354import org.openstreetmap.josm.tools.WindowGeometry;
    5455
     
    445446            this.model = model;
    446447            this.monitor = monitor;
    447             this.worker = Executors.newSingleThreadExecutor();
     448            this.worker = Executors.newSingleThreadExecutor(Utils.newThreadFactory(getClass() + "-%d", Thread.NORM_PRIORITY));
    448449        }
    449450
  • trunk/src/org/openstreetmap/josm/gui/io/UploadLayerTask.java

    r8510 r8734  
    3131 *     ExecutorService executorService = ...
    3232 *     UploadLayerTask task = new UploadLayerTask(layer, monitor);
    33  *     Future&lt;?&gt; taskFuture = executorServce.submit(task)
     33 *     Future&lt;?&gt; taskFuture = executorService.submit(task)
    3434 *     try {
    3535 *        // wait for the task to complete
    3636 *        taskFuture.get();
    3737 *     } catch (Exception e) {
    38  *        e.printStackTracek();
     38 *        e.printStackTrace();
    3939 *     }
    4040 * </pre>
  • trunk/src/org/openstreetmap/josm/gui/layer/geoimage/GeoImageLayer.java

    r8660 r8734  
    3737import java.util.concurrent.ExecutorService;
    3838import java.util.concurrent.Executors;
    39 import java.util.concurrent.ThreadFactory;
    4039
    4140import javax.swing.Action;
     
    9594
    9695    boolean useThumbs = false;
    97     private ExecutorService thumbsLoaderExecutor = Executors.newSingleThreadExecutor(new ThreadFactory() {
    98         @Override
    99         public Thread newThread(Runnable r) {
    100             Thread t = new Thread(r);
    101             t.setPriority(Thread.MIN_PRIORITY);
    102             return t;
    103         }
    104     });
     96    private ExecutorService thumbsLoaderExecutor =
     97            Executors.newSingleThreadExecutor(Utils.newThreadFactory("thumbnail-loader-%d", Thread.MIN_PRIORITY));
    10598    private ThumbsLoader thumbsloader;
    10699    private boolean thumbsLoaderRunning = false;
  • trunk/src/org/openstreetmap/josm/gui/preferences/imagery/CacheContentsPanel.java

    r8624 r8734  
    4848import org.openstreetmap.josm.tools.GBC;
    4949import org.openstreetmap.josm.tools.Pair;
     50import org.openstreetmap.josm.tools.Utils;
    5051
    5152/**
     
    133134    }
    134135
    135     private final transient ExecutorService executor = Executors.newSingleThreadExecutor();
     136    private final transient ExecutorService executor =
     137            Executors.newSingleThreadExecutor(Utils.newThreadFactory(getClass() + "-%d", Thread.NORM_PRIORITY));
    136138
    137139    /**
  • trunk/src/org/openstreetmap/josm/gui/progress/ProgressMonitorExecutor.java

    r7937 r8734  
    77
    88import org.openstreetmap.josm.Main;
     9import org.openstreetmap.josm.tools.Utils;
    910
    1011/**
     
    1617public class ProgressMonitorExecutor extends ThreadPoolExecutor {
    1718
    18     public ProgressMonitorExecutor() {
     19    /**
     20     * Creates a new {@code ProgressMonitorExecutor}
     21     * @param nameFormat see {@link Utils#newThreadFactory(String, int)}
     22     * @param threadPriority see {@link Utils#newThreadFactory(String, int)}
     23     */
     24    public ProgressMonitorExecutor(final String nameFormat, final int threadPriority) {
    1925        super(1, 1, 0L, TimeUnit.MILLISECONDS,
    20                 new LinkedBlockingQueue<Runnable>());
     26                new LinkedBlockingQueue<Runnable>(),
     27                Utils.newThreadFactory(nameFormat, threadPriority));
    2128    }
    2229
  • trunk/src/org/openstreetmap/josm/io/MessageNotifier.java

    r8510 r8734  
    3030import org.openstreetmap.josm.io.auth.JosmPreferencesCredentialAgent;
    3131import org.openstreetmap.josm.tools.GBC;
     32import org.openstreetmap.josm.tools.Utils;
    3233
    3334/**
     
    4647    public static final IntegerProperty PROP_INTERVAL = new IntegerProperty("message.notifier.interval", 5);
    4748
    48     private static final ScheduledExecutorService EXECUTOR = Executors.newSingleThreadScheduledExecutor();
     49    private static final ScheduledExecutorService EXECUTOR =
     50            Executors.newSingleThreadScheduledExecutor(Utils.newThreadFactory("message-notifier-%d", Thread.NORM_PRIORITY));
    4951
    5052    private static final Runnable WORKER = new Worker();
  • trunk/src/org/openstreetmap/josm/io/MultiFetchServerObjectReader.java

    r8540 r8734  
    3838import org.openstreetmap.josm.gui.progress.ProgressMonitor;
    3939import org.openstreetmap.josm.tools.CheckParameterUtil;
     40import org.openstreetmap.josm.tools.Utils;
    4041
    4142/**
     
    336337        progressMonitor.setTicksCount(ids.size());
    337338        progressMonitor.setTicks(0);
    338         // The complete set containg all primitives to fetch
     339        // The complete set containing all primitives to fetch
    339340        Set<Long> toFetch = new HashSet<>(ids);
    340341        // Build a list of fetchers that will  download smaller sets containing only MAX_IDS_PER_REQUEST (200) primitives each.
     
    342343        int threadsNumber = Main.pref.getInteger("osm.download.threads", OsmApi.MAX_DOWNLOAD_THREADS);
    343344        threadsNumber = Math.min(Math.max(threadsNumber, 1), OsmApi.MAX_DOWNLOAD_THREADS);
    344         Executor exec = Executors.newFixedThreadPool(threadsNumber);
     345        Executor exec = Executors.newFixedThreadPool(threadsNumber, Utils.newThreadFactory(getClass() + "-%d", Thread.NORM_PRIORITY));
    345346        CompletionService<FetchResult> ecs = new ExecutorCompletionService<>(exec);
    346347        List<Future<FetchResult>> jobs = new ArrayList<>();
  • trunk/src/org/openstreetmap/josm/plugins/PluginHandler.java

    r8540 r8734  
    813813        try {
    814814            ReadLocalPluginInformationTask task = new ReadLocalPluginInformationTask(monitor);
    815             ExecutorService service = Executors.newSingleThreadExecutor();
     815            ExecutorService service = Executors.newSingleThreadExecutor(Utils.newThreadFactory("plugin-loader-%d", Thread.NORM_PRIORITY));
    816816            Future<?> future = service.submit(task);
    817817            try {
     
    970970        try {
    971971            monitor.beginTask("");
    972             ExecutorService service = Executors.newSingleThreadExecutor();
     972            ExecutorService service = Executors.newSingleThreadExecutor(Utils.newThreadFactory("plugin-updater-%d", Thread.NORM_PRIORITY));
    973973
    974974            // try to download the plugin lists
  • trunk/src/org/openstreetmap/josm/tools/ImageProvider.java

    r8540 r8734  
    198198    private static final Map<Image, Map<Long, ImageResource>> ROTATE_CACHE = new HashMap<>();
    199199
    200     private static final ExecutorService IMAGE_FETCHER = Executors.newSingleThreadExecutor();
     200    private static final ExecutorService IMAGE_FETCHER =
     201            Executors.newSingleThreadExecutor(Utils.newThreadFactory("image-fetcher-%d", Thread.NORM_PRIORITY));
    201202
    202203    /**
  • trunk/src/org/openstreetmap/josm/tools/Utils.java

    r8650 r8734  
    4747import java.util.concurrent.Executors;
    4848import java.util.concurrent.ThreadFactory;
     49import java.util.concurrent.atomic.AtomicLong;
    4950import java.util.regex.Matcher;
    5051import java.util.regex.Pattern;
     
    12681269
    12691270    /**
     1271     * Creates a new {@link ThreadFactory} which creates threads with names according to {@code nameFormat}.
     1272     * @param nameFormat a {@link String#format(String, Object...)} compatible name format; its first argument is a unique thread index
     1273     * @param threadPriority the priority of the created threads, see {@link Thread#setPriority(int)}
     1274     * @return a new {@link ThreadFactory}
     1275     */
     1276    public static ThreadFactory newThreadFactory(final String nameFormat, final int threadPriority) {
     1277        final String ignore = String.format(Locale.ENGLISH, nameFormat, 0);// fail fast
     1278        return new ThreadFactory() {
     1279            final AtomicLong count = new AtomicLong(0);
     1280            @Override
     1281            public Thread newThread(final Runnable runnable) {
     1282                final Thread thread = new Thread(runnable, String.format(Locale.ENGLISH, nameFormat, count.getAndIncrement()));
     1283                thread.setPriority(threadPriority);
     1284                return thread;
     1285            }
     1286        };
     1287    }
     1288
     1289    /**
    12701290     * Returns a pair containing the number of threads (n), and a thread pool (if n > 1) to perform
    12711291     * multi-thread computation in the context of the given preference key.
    12721292     * @param pref The preference key
     1293     * @param nameFormat see {@link #newThreadFactory(String, int)}
     1294     * @param threadPriority see {@link #newThreadFactory(String, int)}
    12731295     * @return a pair containing the number of threads (n), and a thread pool (if n > 1, null otherwise)
    12741296     * @since 7423
    12751297     */
    1276     public static Pair<Integer, ExecutorService> newThreadPool(String pref) {
     1298    public static Pair<Integer, ExecutorService> newThreadPool(String pref, String nameFormat, int threadPriority) {
    12771299        int noThreads = Main.pref.getInteger(pref, Runtime.getRuntime().availableProcessors());
    1278         ExecutorService pool = noThreads <= 1 ? null : Executors.newFixedThreadPool(noThreads);
     1300        ExecutorService pool = noThreads <= 1 ? null : Executors.newFixedThreadPool(noThreads, newThreadFactory(nameFormat, threadPriority));
    12791301        return new Pair<>(noThreads, pool);
    12801302    }
     
    14271449        return hashMapInitialCapacity(nEntries, 0.75f);
    14281450    }
    1429 
    1430     /**
    1431      * @param name to be set for the threads
    1432      * @return Thread Factory returning named threads
    1433      */
    1434     public static ThreadFactory getNamedThreadFactory(final String name) {
    1435         return new ThreadFactory() {
    1436             @Override
    1437             public Thread newThread(Runnable r) {
    1438                 Thread t = Executors.defaultThreadFactory().newThread(r);
    1439                 t.setName(name);
    1440                 return t;
    1441             }
    1442         };
    1443     }
    14441451}
Note: See TracChangeset for help on using the changeset viewer.