Java Code Examples for java.util.concurrent.ThreadPoolExecutor#AbortPolicy

The following examples show how to use java.util.concurrent.ThreadPoolExecutor#AbortPolicy . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ThreadPoolFactory.java    From rocketmq-spring-boot-starter with Apache License 2.0 6 votes vote down vote up
public static ThreadPoolExecutor createSendMessageThreadPoolExecutor(RocketProperties rocketProperties) {

		Integer threadNums = rocketProperties.getSendMessageThreadNums();

		ThreadFactory threadFactory = new ThreadFactoryBuilder()
				.setNameFormat("SendMessage").build();

		return new ThreadPoolExecutor(threadNums,
				threadNums,
				0,
				TimeUnit.SECONDS,
				new LinkedBlockingQueue<>(4096),
				threadFactory,
				new ThreadPoolExecutor.AbortPolicy()
		);
	}
 
Example 2
Source File: ThreadPoolExecutorDemo.java    From javacore with Creative Commons Attribution Share Alike 4.0 International 6 votes vote down vote up
public static void main(String[] args) {
    ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(5, 10, 500, TimeUnit.MILLISECONDS,
        new LinkedBlockingQueue<Runnable>(),
        Executors.defaultThreadFactory(),
        new ThreadPoolExecutor.AbortPolicy());

    for (int i = 0; i < 100; i++) {
        threadPoolExecutor.execute(new MyThread());
        String info = String.format("线程池中线程数目:%s,队列中等待执行的任务数目:%s,已执行玩别的任务数目:%s",
            threadPoolExecutor.getPoolSize(),
            threadPoolExecutor.getQueue().size(),
            threadPoolExecutor.getCompletedTaskCount());
        System.out.println(info);
    }
    threadPoolExecutor.shutdown();
}
 
Example 3
Source File: ThreadPoolManager.java    From CodeDefenders with GNU Lesser General Public License v3.0 6 votes vote down vote up
private ThreadPoolModel(final int core, final int max,
                        final long keepAliveTime, final TimeUnit keepAliveTimeUnit,
                        final BlockingQueue<Runnable> workQueue,
                        final ThreadFactory threadFactory,
                        final RejectedExecutionHandler handler,
                        final long shutdownTime, final TimeUnit shutdownTimeUnit) {
    this.core = core;
    this.max = max;
    this.keepAliveTime = keepAliveTime;
    this.keepAliveTimeUnit = keepAliveTimeUnit == null ? MILLISECONDS : keepAliveTimeUnit;
    this.shutdownTime = shutdownTime;
    this.shutdownTimeUnit = shutdownTimeUnit == null ? MILLISECONDS : shutdownTimeUnit;
    this.workQueue = workQueue == null ? new LinkedBlockingDeque<>() : workQueue;
    this.threadFactory = threadFactory == null ? Executors.defaultThreadFactory() : threadFactory;
    this.handler = handler == null ? new ThreadPoolExecutor.AbortPolicy() : handler;
}
 
Example 4
Source File: ThreadPoolUtils.java    From mcg-helper with Apache License 2.0 5 votes vote down vote up
public static ExecutorService createCacheExecutorService(int corePoolSize, int maximumPoolSize, String threadNamingPattern) {
	if(corePoolSize < 1) {
		corePoolSize = 5;
	}
	if(maximumPoolSize < 1) {
		maximumPoolSize = 100;
	}
	return new ThreadPoolExecutor(corePoolSize, maximumPoolSize, 100L, TimeUnit.MILLISECONDS, new SynchronousQueue<Runnable>(),
			new BasicThreadFactory.Builder().namingPattern(threadNamingPattern).daemon(true).build(), new ThreadPoolExecutor.AbortPolicy());
}
 
Example 5
Source File: ThreadPoolFactory.java    From rocketmq-spring-boot-starter with Apache License 2.0 5 votes vote down vote up
public static ThreadPoolExecutor createConsumeThreadPoolExecutor(RocketProperties rocketProperties) {
	Integer threadNums = rocketProperties.getCreateConsumeThreadNums();

	ThreadFactory threadFactory = new ThreadFactoryBuilder()
			.setNameFormat("InitializeConsumerListener").build();

	return new ThreadPoolExecutor(threadNums,
			threadNums,
			0,
			TimeUnit.SECONDS,
			new LinkedBlockingQueue<>(1024),
			threadFactory,
			new ThreadPoolExecutor.AbortPolicy()
	);
}
 
Example 6
Source File: DownloadThreadPool.java    From okhttp-OkGo with Apache License 2.0 5 votes vote down vote up
public XExecutor getExecutor() {
    if (executor == null) {
        synchronized (DownloadThreadPool.class) {
            if (executor == null) {
                executor = new XExecutor(corePoolSize, MAX_POOL_SIZE, KEEP_ALIVE_TIME, UNIT, //
                                         new PriorityBlockingQueue<Runnable>(),   //无限容量的缓冲队列
                                         Executors.defaultThreadFactory(),        //线程创建工厂
                                         new ThreadPoolExecutor.AbortPolicy());   //继续超出上限的策略,阻止
            }
        }
    }
    return executor;
}
 
Example 7
Source File: AsynchronousGetRecordsRetrievalStrategy.java    From amazon-kinesis-client with Apache License 2.0 5 votes vote down vote up
private static ExecutorService buildExector(int maxGetRecordsThreadPool, String shardId) {
    String threadNameFormat = "get-records-worker-" + shardId + "-%d";
    return new ThreadPoolExecutor(CORE_THREAD_POOL_COUNT, maxGetRecordsThreadPool, TIME_TO_KEEP_ALIVE,
            TimeUnit.SECONDS, new LinkedBlockingQueue<>(1),
            new ThreadFactoryBuilder().setDaemon(true).setNameFormat(threadNameFormat).build(),
            new ThreadPoolExecutor.AbortPolicy());
}
 
Example 8
Source File: JobPoller.java    From scipio-erp with Apache License 2.0 5 votes vote down vote up
private ThreadPoolExecutor createThreadPoolExecutor() {
    try {
        ThreadPool threadPool = getThreadPoolConfig();
        return new ThreadPoolExecutor(threadPool.getMinThreads(), threadPool.getMaxThreads(), threadPool.getTtl(),
                TimeUnit.MILLISECONDS, new PriorityBlockingQueue<>(threadPool.getJobs(), createPriorityComparator()), new JobInvokerThreadFactory(), new ThreadPoolExecutor.AbortPolicy());
    } catch (GenericConfigException e) {
        Debug.logError(e, "Exception thrown while getting <thread-pool> model, using default <thread-pool> values: ", module);
        return new ThreadPoolExecutor(ThreadPool.MIN_THREADS, ThreadPool.MAX_THREADS, ThreadPool.THREAD_TTL,
                TimeUnit.MILLISECONDS, new PriorityBlockingQueue<>(ThreadPool.QUEUE_SIZE, createPriorityComparator()), new JobInvokerThreadFactory(), new ThreadPoolExecutor.AbortPolicy());
    }
}
 
Example 9
Source File: Master.java    From drftpd with GNU General Public License v2.0 5 votes vote down vote up
public void createThreadPool() {
    int maxUserConnected = GlobalContext.getConfig().getMaxUsersTotal();
    int maxAliveThreads = maxUserConnected + GlobalContext.getConfig().getMaxUsersExempt();
    int minAliveThreads = (int) Math.round(maxAliveThreads * 0.25);

    _pool = new ThreadPoolExecutor(minAliveThreads, maxAliveThreads, 3 * 60, TimeUnit.SECONDS,
            new SynchronousQueue<>(), new ConnectionThreadFactory(), new ThreadPoolExecutor.AbortPolicy());
    _pool.allowCoreThreadTimeOut(false);
    _pool.prestartAllCoreThreads();
}
 
Example 10
Source File: JolieThreadPoolExecutor.java    From jolie with GNU Lesser General Public License v2.1 5 votes vote down vote up
public JolieThreadPoolExecutor( final ThreadFactory factory ) {
	super(
		0,
		Integer.MAX_VALUE - 8,
		60L,
		TimeUnit.SECONDS,
		new SynchronousQueue<>(),
		factory,
		new ThreadPoolExecutor.AbortPolicy() );
}
 
Example 11
Source File: UploadThreadPool.java    From okhttp-OkGo with Apache License 2.0 5 votes vote down vote up
public XExecutor getExecutor() {
    if (executor == null) {
        synchronized (UploadThreadPool.class) {
            if (executor == null) {
                executor = new XExecutor(corePoolSize, MAX_IMUM_POOL_SIZE, KEEP_ALIVE_TIME, UNIT, //
                                         new PriorityBlockingQueue<Runnable>(),   //无限容量的缓冲队列
                                         Executors.defaultThreadFactory(),        //线程创建工厂
                                         new ThreadPoolExecutor.AbortPolicy());   //继续超出上限的策略,阻止
            }
        }
    }
    return executor;
}
 
Example 12
Source File: TransactionStoreManager.java    From joyqueue with Apache License 2.0 5 votes vote down vote up
public TransactionStoreManager(File base, PositioningStore.Config config, PreloadBufferPool bufferPool) {
    this.base = base;
    this.config = config;
    this.bufferPool = bufferPool;
    idSequence = new AtomicInteger(0);
    loadFiles();

    writeExecutor = new ThreadPoolExecutor(1, 1, 10L, TimeUnit.SECONDS,
            new ArrayBlockingQueue<>(1024), new ThreadPoolExecutor.AbortPolicy());
    storeMap = new HashMap<>();

}
 
Example 13
Source File: ExporterServer.java    From sofa-lookout with Apache License 2.0 5 votes vote down vote up
public ExporterServer(int port) {
    final ExecutorService singleThreadPool = new ThreadPoolExecutor(1, 1, 0L,
        TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>(100),
        CommonUtil.getNamedThreadFactory("prometheus-exporter-pool"),
        new ThreadPoolExecutor.AbortPolicy());
    try {
        httpServer = HttpServer.create(new InetSocketAddress(port), 2);
        httpServer.setExecutor(singleThreadPool);
    } catch (IOException e) {
        throw new RuntimeException("prometheus exporter server create err! And port = " + port,
            e);
    }
}
 
Example 14
Source File: ExecutorServiceBuilder.java    From che with Eclipse Public License 2.0 5 votes vote down vote up
public ExecutorServiceBuilder() {
  this.corePoolSize = 0;
  this.maxPoolSize = 1;
  this.allowCoreThreadTimeOut = false;
  this.keepAliveTime = Duration.ofSeconds(60);
  this.workQueue = new LinkedBlockingQueue<>();
  this.threadFactory = Executors.defaultThreadFactory();
  this.handler = new ThreadPoolExecutor.AbortPolicy();
}
 
Example 15
Source File: ExecutorConfiguration.java    From porcupine with Apache License 2.0 4 votes vote down vote up
public Builder abortPolicy() {
    configuration.rejectedExecutionHandler = new ThreadPoolExecutor.AbortPolicy();
    return this;
}
 
Example 16
Source File: ReadPresenter.java    From CrawlerForReader with Apache License 2.0 4 votes vote down vote up
public ReadPresenter(ReadContract.View view) {
    this.view = view;
    mPool = new ThreadPoolExecutor(mCorePoolSize, mMaximumPoolSize, mKeepAliveTime, TimeUnit.MILLISECONDS,
            new LinkedBlockingQueue<Runnable>(), Executors.defaultThreadFactory(), new ThreadPoolExecutor.AbortPolicy());
}
 
Example 17
Source File: KSBThreadPoolImpl.java    From rice with Educational Community License v2.0 4 votes vote down vote up
public KSBThreadPoolImpl() {
	super(DEFAULT_POOL_SIZE, DEFAULT_POOL_SIZE, 60, TimeUnit.SECONDS, new PriorityBlockingQueue(1, new PriorityBlockingQueuePersistedMessageComparator()),  new KSBThreadFactory(ClassLoaderUtils.getDefaultClassLoader()), new ThreadPoolExecutor.AbortPolicy());
}
 
Example 18
Source File: HttpClient4Utils.java    From ZTuoExchange_framework with MIT License 4 votes vote down vote up
/**
	 * 增加定时任务, 每隔一段时间清理连接
	 *
	 * @param cm
	 */
	private static void startMonitorThread(final PoolingHttpClientConnectionManager cm) {
		// 参数是线程的数量
		ThreadFactory namedThreadFactory = new ThreadFactoryBuilder().setNameFormat("start-monitor-pool-%d").build();
		ScheduledExecutorService singleThreadPool = (ScheduledExecutorService) new ThreadPoolExecutor(1, 1, 0L,
				TimeUnit.MILLISECONDS, new SynchronousQueue<Runnable>(),namedThreadFactory,new ThreadPoolExecutor.AbortPolicy());
		/**
		 * 第二个参数,是首次执行该线程的延迟时间,之后失效 第三个参数是,首次执行完之后,再过该段时间再次执行该线程,具有周期性
		 */
		singleThreadPool.scheduleAtFixedRate(new Runnable() {
			@Override
			public void run() {
				while (true) {
					try {
						cm.closeExpiredConnections();
						cm.closeIdleConnections(30, TimeUnit.SECONDS);

                       // log.info("closing expired & idle connections, stat={}", cm.getTotalStats());
                       TimeUnit.SECONDS.sleep(10);
                   } catch (Exception e) {
                       // ignore exceptoin
                   }
               }
           }
        }, 10, 3, TimeUnit.SECONDS);


//        Thread t = new Thread(new Runnable() {
//            @Override
//            public void run() {
//                while (true) {
//                    try {
//                        cm.closeExpiredConnections();
//                        cm.closeIdleConnections(30, TimeUnit.SECONDS);
//
//                        // log.info("closing expired & idle connections, stat={}", cm.getTotalStats());
//                        TimeUnit.SECONDS.sleep(10);
//                    } catch (Exception e) {
//                        // ignore exceptoin
//                    }
//                }
//            }
////        });
//        t.setDaemon(true);
//        t.start();
    }
 
Example 19
Source File: ExecutorConfigurationSupport.java    From spring-analysis-note with MIT License 2 votes vote down vote up
/**
 * Set the RejectedExecutionHandler to use for the ExecutorService.
 * Default is the ExecutorService's default abort policy.
 * @see java.util.concurrent.ThreadPoolExecutor.AbortPolicy
 */
public void setRejectedExecutionHandler(@Nullable RejectedExecutionHandler rejectedExecutionHandler) {
	this.rejectedExecutionHandler =
			(rejectedExecutionHandler != null ? rejectedExecutionHandler : new ThreadPoolExecutor.AbortPolicy());
}
 
Example 20
Source File: ExecutorConfigurationSupport.java    From lams with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Set the RejectedExecutionHandler to use for the ExecutorService.
 * Default is the ExecutorService's default abort policy.
 * @see java.util.concurrent.ThreadPoolExecutor.AbortPolicy
 */
public void setRejectedExecutionHandler(RejectedExecutionHandler rejectedExecutionHandler) {
	this.rejectedExecutionHandler =
			(rejectedExecutionHandler != null ? rejectedExecutionHandler : new ThreadPoolExecutor.AbortPolicy());
}