RLPark 1.0.0
Reinforcement Learning Framework in Java

Scheduling.java

Go to the documentation of this file.
00001 package rlpark.plugin.rltoys.utils;
00002 
00003 import java.util.HashMap;
00004 import java.util.Map;
00005 import java.util.concurrent.ExecutorService;
00006 import java.util.concurrent.Executors;
00007 import java.util.concurrent.ThreadFactory;
00008 import java.util.concurrent.atomic.AtomicInteger;
00009 
00010 public class Scheduling {
00011   static private Map<String, Integer> poolPrefixNumber = new HashMap<String, Integer>();
00012 
00013   static synchronized int getPoolSuffix(String poolName) {
00014     Integer last = poolPrefixNumber.get(poolName);
00015     if (last == null)
00016       last = -1;
00017     int result = last + 1;
00018     poolPrefixNumber.put(poolName, result);
00019     return result;
00020   }
00021 
00022   static class NamedThreadFactory implements ThreadFactory {
00023     final ThreadGroup group;
00024     final AtomicInteger threadNumber = new AtomicInteger(1);
00025     final String namePrefix;
00026 
00027     NamedThreadFactory(String name) {
00028       SecurityManager s = System.getSecurityManager();
00029       group = (s != null) ? s.getThreadGroup() : Thread.currentThread().getThreadGroup();
00030       namePrefix = name + getPoolSuffix(name) + "-thread";
00031     }
00032 
00033     @Override
00034     public Thread newThread(Runnable r) {
00035       Thread t = new Thread(group, r, namePrefix + threadNumber.getAndIncrement(), 0);
00036       t.setDaemon(true);
00037       return t;
00038     }
00039   }
00040 
00041   public static int getDefaultNbThreads() {
00042     return Runtime.getRuntime().availableProcessors();
00043   }
00044 
00045   public static ExecutorService newFixedThreadPool(String name, int nThreads) {
00046     return Executors.newFixedThreadPool(nThreads, new NamedThreadFactory(name));
00047   }
00048 
00049 }
 All Classes Namespaces Files Functions Variables Enumerations
Zephyr
RLPark