Ignore:
Timestamp:
2015-09-08T15:20:34+02:00 (6 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/tools
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • 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.