public class ConcurrentServiceQueue implements Serializable { /** */ private static final long serialVersionUID = 1L; private static final BlockingQueue<Runnable> execHandleQueue = new SynchronousQueue<Runnable>(); private static final BlockingQueue<Runnable> resultHandleQueue = new SynchronousQueue<Runnable>(); public static final ThreadPoolExecutor execHandleExecutor = new ThreadPoolExecutor( 100, 1000, 300, TimeUnit.SECONDS, execHandleQueue, Executors.defaultThreadFactory(), new ThreadPoolExecutor.AbortPolicy()); public static final ThreadPoolExecutor resultHandleExecutor = new ThreadPoolExecutor( 100, 1000, 300, TimeUnit.SECONDS, execHandleQueue, Executors.defaultThreadFactory(), new ThreadPoolExecutor.AbortPolicy()); public static final AtomicInteger currentTaskCount = new AtomicInteger(0); public static final int maxProcessCount = 600; }
public static void main(String[] args) { new ThreadPoolExecutor( 0, 10, 1000, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>(), Executors.defaultThreadFactory(), new RejectedExecutionHandler() { @Override public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) { // TODO Auto-generated method stub } }); new ThreadPoolExecutor( 0, 10, 1000, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>(10), Executors.defaultThreadFactory(), new ThreadPoolExecutor.CallerRunsPolicy()); int numOfProcessors = Runtime.getRuntime().availableProcessors(); System.out.println(numOfProcessors); MyLogger logger = new MyLogger(); Thread.currentThread().setUncaughtExceptionHandler(logger); int i = 10 / 0; System.out.println(i); }
public OrderedSafeExecutor build() { if (null == threadFactory) { threadFactory = Executors.defaultThreadFactory(); } return new OrderedSafeExecutor( name, numThreads, threadFactory, statsLogger, traceTaskExecution, warnTimeMicroSec); }
/** @author Shuyang Zhou */ public class SetRecordUncaughtExceptionThreadFactory implements ThreadFactory { public List<Thread> getCreatedThreads() { return _createdThreads; } public RecordUncaughtExceptionHandler getRecordUncaughtExceptionHandler() { return _recordUncaughtExceptionHandler; } @Override public Thread newThread(Runnable runnable) { Thread thread = _threadFactory.newThread(runnable); thread.setUncaughtExceptionHandler(_recordUncaughtExceptionHandler); _createdThreads.add(thread); return thread; } private final List<Thread> _createdThreads = new ArrayList<>(); private final RecordUncaughtExceptionHandler _recordUncaughtExceptionHandler = new RecordUncaughtExceptionHandler(); private final ThreadFactory _threadFactory = Executors.defaultThreadFactory(); }
/** * Based on the default thread factory * * @param name the name prefix of the new thread * @return the factory to use when creating new threads */ public static final ThreadFactory getThreadFactory(String name) { return r -> { Thread t = Executors.defaultThreadFactory().newThread(r); t.setName(name + "-" + t.getName()); // $NON-NLS-1$ return t; }; }
public MessagingServer(MessageReceiptCallback cb, int port) { this.messagingServerHandler = new MessagingServerHandler(cb); this.port = port; // Start server with Nb of active threads = 2*NB CPU + 1 as maximum. ChannelFactory factory = new NioServerSocketChannelFactory( Executors.newCachedThreadPool(), Executors.newCachedThreadPool(), Runtime.getRuntime().availableProcessors() * 2 + 1); bootstrap = new ServerBootstrap(factory); // Create the global ChannelGroup channelGroup = new DefaultChannelGroup(MessagingServer.class.getName()); // 200 threads max, Memory limitation: 1MB by channel, 1GB global, 100 ms of timeout OrderedMemoryAwareThreadPoolExecutor pipelineExecutor = new OrderedMemoryAwareThreadPoolExecutor( 200, 1048576, 1073741824, 100, TimeUnit.MILLISECONDS, Executors.defaultThreadFactory()); // We need to use a pipeline factory because we are using stateful handlers bootstrap.setPipelineFactory( new ChannelPipelineFactory() { @Override public ChannelPipeline getPipeline() throws Exception { return Channels.pipeline( new ObjectDecoder(getMaxObjectSize()), new ObjectEncoder(), messagingServerHandler); } }); bootstrap.setOption("child.tcpNoDelay", true); bootstrap.setOption("child.keepAlive", true); bootstrap.setOption("child.reuseAddress", true); bootstrap.setOption("child.connectTimeoutMillis", 100); bootstrap.setOption("readWriteFair", true); }
@Override public Thread newThread(@Nonnull Runnable runnable) { Thread thread = Executors.defaultThreadFactory().newThread(runnable); thread.setDaemon(true); if (name != null) thread.setName(name); return thread; }
public class NamedThreadFactory implements ThreadFactory { private final String nameFormat; private final AtomicLong count = new AtomicLong(0); private final ThreadFactory backingThreadFactory = Executors.defaultThreadFactory(); /** * Creates a new named {@link ThreadFactory} builder. * * @param nameFormat a {@link String#format(String, Object...)}-compatible format String, to which * a unique integer (0, 1, etc.) will be supplied as the single parameter. This integer will * be unique to the built instance of the ThreadFactory and will be assigned sequentially. */ public NamedThreadFactory(String nameFormat) { String.format(nameFormat, 0); // fail fast if the format is bad or null this.nameFormat = nameFormat; } @Override public Thread newThread(Runnable runnable) { Thread thread = backingThreadFactory.newThread(runnable); thread.setName(String.format(nameFormat, count.getAndIncrement())); return thread; } }
/** * A simple ThreadFactory which names the thread as follows:<br> * <br> * <i>applicationId</i>/KSB-pool-<i>m</i>-thread-<i>n</i><br> * <br> * Where <i>applicationId</i> is the id of the application running the thread pool, <i>m</i> is * the sequence number of the factory and <i>n</i> is the sequence number of the thread within the * factory. * * @author Kuali Rice Team ([email protected]) */ private static class KSBThreadFactory implements ThreadFactory { private static int factorySequence = 0; private static int threadSequence = 0; private ThreadFactory defaultThreadFactory = Executors.defaultThreadFactory(); private ClassLoader contextClassLoader; public KSBThreadFactory(ClassLoader contextClassLoader) { this.contextClassLoader = contextClassLoader; factorySequence++; } public Thread newThread(Runnable runnable) { threadSequence++; Thread thread = this.defaultThreadFactory.newThread(runnable); // if the thread ends up getting spawned by an action inside of a workflow plugin or something // along those lines, it will inherit the plugin's // classloader as it's ContextClassLoader. Let's make sure it's set to the same ClassLoader // that loaded the KSBConfigurer thread.setContextClassLoader(contextClassLoader); thread.setName( CoreConfigHelper.getApplicationId() + "/KSB-pool-" + factorySequence + "-thread-" + threadSequence); return thread; } }
@Override public Thread newThread(final Runnable r) { final Thread t = Executors.defaultThreadFactory().newThread(r); t.setDaemon(true); t.setName("Interactive instrumentation console"); return t; }
private static class MinPriorityThreadFactory implements ThreadFactory { private final ThreadFactory myDefaultThreadFactory = Executors.defaultThreadFactory(); public Thread newThread(Runnable r) { final Thread th = myDefaultThreadFactory.newThread(r); th.setPriority(Thread.MIN_PRIORITY); return th; } }
@Override public Thread newThread(Runnable runnable) { Thread thread = Executors.defaultThreadFactory().newThread(runnable); thread.setName(name + "-" + count.incrementAndGet()); if (exceptionHandler != null) { thread.setUncaughtExceptionHandler(exceptionHandler); } return thread; }
class TestingThreadFactory implements ThreadFactory { public final AtomicInteger numCreated = new AtomicInteger(); private final ThreadFactory factory = Executors.defaultThreadFactory(); public Thread newThread(Runnable r) { numCreated.incrementAndGet(); return factory.newThread(r); } }
private static class KEWThreadFactory implements ThreadFactory { private ThreadFactory defaultThreadFactory = Executors.defaultThreadFactory(); public Thread newThread(Runnable runnable) { Thread thread = defaultThreadFactory.newThread(runnable); thread.setName("ServerPluginRegistry-" + thread.getName()); return thread; } }
public FixThreadPoolExecutor( int corePoolSize, int maximumPoolSize, long keepAliveSecond, String poolName) { super( corePoolSize, maximumPoolSize, keepAliveSecond, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>(), Executors.defaultThreadFactory()); }
@Deprecated public OrderedSafeExecutor(int numThreads, String threadName) { this( threadName, numThreads, Executors.defaultThreadFactory(), NullStatsLogger.INSTANCE, false, WARN_TIME_MICRO_SEC_DEFAULT); }
public LoginToClientPipeLineFactory() { this.pipelineExecutor = new OrderedMemoryAwareThreadPoolExecutor( THREADS_MAX, MEMORY_PER_CHANNEL, TOTAL_MEMORY, TIMEOUT, TimeUnit.MILLISECONDS, Executors.defaultThreadFactory()); }
public static T_WorkoutExercisesControl getInstance(Context context) { if (instance == null) { context = context.getApplicationContext(); ThreadFactory logThreadFactory = Executors.defaultThreadFactory(); Executor logExecutor = Executors.newSingleThreadExecutor(logThreadFactory); SQLiteOpenHelper openHelper = DbOpenHelper.getInstance(context, DBControl.DB_NAME, DBControl.DB_VERSION, logExecutor); instance = new T_WorkoutExercisesControl(context, logExecutor, openHelper); } return instance; }
// create an executor with daemon threads private static ExecutorService createExecutor() { ExecutorService executor = Executors.newFixedThreadPool( 1, r -> { Thread t = Executors.defaultThreadFactory().newThread(r); t.setDaemon(true); return t; }); return executor; }
public SelfTuningExecutorService() { super( 0, Integer.MAX_VALUE, 60L, TimeUnit.SECONDS, new LinkedBlockingQueue(), Executors.defaultThreadFactory(), new AbortPolicy()); monitor = new Monitor(); monitor.start(); }
private static class MetricsPollerThreadFactory implements ThreadFactory { private static final String MetricsThreadName = "HystrixMetricPoller"; private final ThreadFactory defaultFactory = Executors.defaultThreadFactory(); public Thread newThread(Runnable r) { Thread thread = defaultFactory.newThread(r); thread.setName(MetricsThreadName); thread.setDaemon(true); return thread; } }
public ThreadPoolExecutor(int corePoolSize, int maxPoolSize) { this( corePoolSize, maxPoolSize, 60, TimeUnit.SECONDS, false, Integer.MAX_VALUE, new AbortPolicy(), Executors.defaultThreadFactory(), new ThreadPoolHandlerAdapter()); }
class DaemonThreadFactory implements ThreadFactory { static final ThreadFactory INSTANCE = new DaemonThreadFactory(); private static final ThreadFactory DEFAULT = Executors.defaultThreadFactory(); public Thread newThread(Runnable r) { Thread t = DEFAULT.newThread(r); t.setDaemon(true); return t; } }
public TimeScheduler2(int corePoolSize) { pool = new ThreadManagerThreadPoolExecutor( corePoolSize, corePoolSize * 2, 5000, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>(5000), Executors.defaultThreadFactory(), new ThreadPoolExecutor.CallerRunsPolicy()); init(); }
public Thread newThread(Runnable r) { Thread rv = Executors.defaultThreadFactory().newThread(r); rv.setName(_name + ' ' + (++_count) + '/' + _threads); // Uncomment this to test threadgrouping, but we should be all safe now that the constructor // preallocates! // String name = rv.getThreadGroup().getName(); // if(!name.equals("main")) { // (new Exception("OWCH! DAMN! Wrong ThreadGroup `" + name +"', `" + // rv.getName() + "'")).printStackTrace(); // } rv.setDaemon(true); return rv; }
/** * (Convenience constructor) Creates a new instance with a new {@link OrderedThreadPoolExecutor}, * no thread in the pool, but a maximum of threads in the pool is given. All the event will be * handled by this default executor. * * @param maximumPoolSize The maximum pool size */ public ExecutorFilter(int maximumPoolSize) { // Create a new default Executor Executor executor = createDefaultExecutor( BASE_THREAD_NUMBER, maximumPoolSize, DEFAULT_KEEPALIVE_TIME, TimeUnit.SECONDS, Executors.defaultThreadFactory(), null); // Initialize the filter init(executor, MANAGEABLE_EXECUTOR); }
/** * (Convenience constructor) Creates a new instance with a new {@link OrderedThreadPoolExecutor}. * * @param corePoolSize The initial pool size * @param maximumPoolSize The maximum pool size * @param keepAliveTime Default duration for a thread * @param unit Time unit used for the keepAlive value */ public ExecutorFilter(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit) { // Create a new default Executor Executor executor = createDefaultExecutor( corePoolSize, maximumPoolSize, keepAliveTime, unit, Executors.defaultThreadFactory(), null); // Initialize the filter init(executor, MANAGEABLE_EXECUTOR); }
/** * (Convenience constructor) Creates a new instance with a new {@link OrderedThreadPoolExecutor}. * * @param eventTypes The event for which the executor will be used */ public ExecutorFilter(IoEventType... eventTypes) { // Create a new default Executor Executor executor = createDefaultExecutor( BASE_THREAD_NUMBER, DEFAULT_MAX_POOL_SIZE, DEFAULT_KEEPALIVE_TIME, TimeUnit.SECONDS, Executors.defaultThreadFactory(), null); // Initialize the filter init(executor, MANAGEABLE_EXECUTOR, eventTypes); }
/** * Creates a new instance of {@code ThreadFactoryImpl} and configures it from the specified {@code * Builder} object. * * @param builder the {@code Builder} object */ private BasicThreadFactory(final Builder builder) { if (builder.wrappedFactory == null) { wrappedFactory = Executors.defaultThreadFactory(); } else { wrappedFactory = builder.wrappedFactory; } namingPattern = builder.namingPattern; priority = builder.priority; daemonFlag = builder.daemonFlag; uncaughtExceptionHandler = builder.exceptionHandler; threadCounter = new AtomicLong(); }
/** * (Convenience constructor) Creates a new instance with a new {@link OrderedThreadPoolExecutor}. * * @param corePoolSize The initial pool size * @param maximumPoolSize The maximum pool size * @param eventTypes The event for which the executor will be used */ public ExecutorFilter(int corePoolSize, int maximumPoolSize, IoEventType... eventTypes) { // Create a new default Executor Executor executor = createDefaultExecutor( corePoolSize, maximumPoolSize, DEFAULT_KEEPALIVE_TIME, TimeUnit.SECONDS, Executors.defaultThreadFactory(), null); // Initialize the filter init(executor, MANAGEABLE_EXECUTOR, eventTypes); }