Last active
October 30, 2017 10:27
-
-
Save dadarom/3939d8fa97ab96323319f7c22edc67a7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/************************************************** | |
* timer | |
***************************************************/ | |
//org.apache.commons.lang3.concurrent.BasicThreadFactory | |
ScheduledExecutorService executorService = new ScheduledThreadPoolExecutor(1, | |
new BasicThreadFactory.Builder().namingPattern("example-schedule-pool-%d").daemon(true).build()); | |
executorService.scheduleAtFixedRate(new Runnable() { | |
@Override | |
public void run() { | |
//do something | |
} | |
},initialDelay,period, TimeUnit.HOURS); | |
/************************************************** | |
* thread pool | |
***************************************************/ | |
// Positive example 1: | |
//org.apache.commons.lang3.concurrent.BasicThreadFactory | |
ScheduledExecutorService executorService = new ScheduledThreadPoolExecutor(1, | |
new BasicThreadFactory.Builder().namingPattern("example-schedule-pool-%d").daemon(true).build()); | |
// Positive example 2: | |
ThreadFactory namedThreadFactory = new ThreadFactoryBuilder() | |
.setNameFormat("demo-pool-%d").build(); | |
//Common Thread Pool | |
ExecutorService pool = new ThreadPoolExecutor(5, 200, | |
0L, TimeUnit.MILLISECONDS, | |
new LinkedBlockingQueue<Runnable>(1024), namedThreadFactory, new ThreadPoolExecutor.AbortPolicy()); | |
pool.execute(()-> System.out.println(Thread.currentThread().getName())); | |
pool.shutdown();//gracefully shutdown | |
// Positive example 3: | |
<bean id="userThreadPool" | |
class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor"> | |
<property name="corePoolSize" value="10" /> | |
<property name="maxPoolSize" value="100" /> | |
<property name="queueCapacity" value="2000" /> | |
<property name="threadFactory" value= threadFactory /> | |
<property name="rejectedExecutionHandler"> | |
<ref local="rejectedExecutionHandler" /> | |
</property> | |
</bean> | |
//in code | |
userThreadPool.execute(thread); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment