Java线程池创建的四种⽅式
闲话少叙...
package com.adao.thread;
import java.util.Calendar;
import java.util.Date;
import urrent.ExecutorService;
import urrent.Executors;
import urrent.ScheduledExecutorService;
import urrent.TimeUnit;
public class ThreadPool {
/**
* 1.创建⼀个可缓存线程池,如果线程池长度超过处理需要,可灵活回收空闲线程,若⽆可回收,则新建线程
*/
public static void cachedThreadPool() {
ExecutorService cachedThreadPool = wCachedThreadPool();
for (int j = 0; j < 10; j++) {
for (int i = 0; i < 10; i++) {
final int index = i;
// try {
// Thread.sleep(1000);
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
@Override
public void run() {
System.out.println(index + "--" + Thread.currentThread().getId() + " name:"
+ Thread.currentThread().getName());
}
});
}
}
}
/
**
* 2.创建⼀个定长线程池,可控制线程最⼤并发数,超出的线程会在队列中等待
*/
public static void newFixedThreadPool() {
ExecutorService fixedThreadPool = wFixedThreadPool(5);
for (int j = 0; j < 10; j++) {
for (int i = 0; i < 10; i++) {
final int index = i;
@Override
public void run() {
try {
System.out.println(index + "----" + Thread.currentThread().getId() + " name:"
+ Thread.currentThread().getName());
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
}
}
/
/ 两种关闭⽅式
// fixedThreadPool.shutdown(); //不会⽴即终⽌线程池,⽽是要等所有任务缓存队列中的任务都执⾏完后才终⽌,但再也不会接受新的任务// fixedThreadPool.shutdownNow(); //⽴即终⽌线程池,并尝试打断正在执⾏的任务,并且清空任务缓存队列,返回尚未执⾏的任务
}
/**
* 3.创建⼀个定长线程池,⽀持定时及周期性任务执⾏
*/
public static void newScheduledThreadPool() {
ScheduledExecutorService scheduledThreadPool = wScheduledThreadPool(50);
Calendar calendar = Instance();
calendar.set(Calendar.HOUR_OF_DAY, 10); // 时
calendar.set(Calendar.MINUTE, 53);// 分
calendar.set(Calendar.SECOND, 00); // 秒
// 计算现在时间和计划任务执⾏时间差多久,单位毫秒
Long date = Time().getTime() - System.currentTimeMillis();
////延迟3秒执⾏
scheduledThreadPool.schedule(new Runnable() {
@Override
public void run() {
System.out.println("delay 3 seconds--" + new Date().toLocaleString() + " name:"
+ Thread.currentThread().getName());
}
}, 3, TimeUnit.SECONDS);
// 延迟5秒执⾏,然后每隔2秒执⾏⼀次
scheduledThreadPool.scheduleAtFixedRate(new Runnable() {
@Override
public void run() {
System.out.println("延迟5秒执⾏,然后每隔2秒执⾏⼀次--" + new Date().toLocaleString() + " name:"
+ Thread.currentThread().getName());
}
}, 5, 2, TimeUnit.SECONDS);
// 定时在某⼀时刻执⾏任务,然后间隔执⾏,如果时间过了会⽴马执⾏
scheduledThreadPool.scheduleAtFixedRate(new Runnable() {
@Override
public void run() {
System.out.println("定时在某⼀时刻执⾏任务,然后间隔执⾏--" + new Date().toLocaleString() + " name:"
+ Thread.currentThread().getName());
}
}, date / 1000, 2, TimeUnit.SECONDS);
}
/**
* 4. 创建⼀个单线程化的线程池,它只会⽤唯⼀的⼯作线程来执⾏任务,保证所有任务按照指定顺序(FIFO, LIFO, 优先级)执⾏ */
public static void newSingleThreadExecutor() {
ExecutorService singleThreadExecutor = wSingleThreadExecutor();
for (int i = 0; i < 10; i++) {
final int index = i;
@Override
java线程池创建的四种public void run() {
try {
System.out.println("单线程执⾏任务。。。" + Thread.currentThread().getName());
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
}
}
public static void main(String[] args) {
// cachedThreadPool();
// newFixedThreadPool();
// newScheduledThreadPool();
newSingleThreadExecutor();
}
}
执⾏结果:
newSingleThreadExecutor();
newFixedThreadPool();
细⼼的⼈可以观察到:
Thread.currentThread().getId() 和 Thread.currentThread().getName()的关系。
perfect
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论