/**
* 1.未使用线程池
*/
public class ThreadDemo {
public static void main(String[] args) {
List<Integer> list = new LinkedList<Integer>();
long startTime = System.currentTimeMillis();
for (int i = 0; i < 100000; i++) {
new Thread(()->{
list.add(new Random().nextInt());
}).start();
}
long endTime = System.currentTimeMillis();
System.out.println("耗时:"+(endTime-startTime));
}
}
/**
1. 2.使用线程池
*/
public class ThreadDemo {
public static void main(String[] args) {
ThreadPoolExecutor executor = new ThreadPoolExecutor(50, 1000,
0L, TimeUnit.SECONDS,
new LinkedBlockingQueue<Runnable>());
List<Integer> list = new LinkedList<Integer>();
long startTime = System.currentTimeMillis();
for (int i = 0; i < 100000; i++) {
executor.execute(new Thread(()->{
list.add(new Random().nextInt());
}));
}
//线程执行完成后,关闭线程池(此处仅为效果使用,开发用到不多)
executor.shutdown();
long endTime = System.currentTimeMillis();
System.out.println("耗时:"+(endTime-startTime));
}
}
Fork/Join框架介绍
4.ThreadPoolExecutor 核心参数
public ThreadPoolExecutor(int corePoolSize,
int maximumPoolSize,
long keepAliveTime,
TimeUnit unit,
BlockingQueue<Runnable> workQueue,
ThreadFactory threadFactory,
RejectedExecutionHandler handler)
public static ExecutorService newSingleThreadExecutor() {
return new FinalizableDelegatedExecutorService
(new ThreadPoolExecutor(1, 1,
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>()));
}
public static ExecutorService newFixedThreadPool(int nThreads) {
return new ThreadPoolExecutor(nThreads, nThreads,
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>());
}
当然,可以重写线程工厂方法来修改线程名称
ExecutorService pool = Executors.newFixedThreadPool(2, new ThreadFactory() {
private AtomicInteger t = new AtomicInteger(1);
public Thread newThread(Runnable r) {
return new Thread(r,"mypool_t"+t.getAndIncrement());
}
});
public static ExecutorService newCachedThreadPool() {
return new ThreadPoolExecutor(0, Integer.MAX_VALUE,
60L, TimeUnit.SECONDS,
new SynchronousQueue<Runnable>());
}
public static ExecutorService newWorkStealingPool() {
return new ForkJoinPool
(Runtime.getRuntime().availableProcessors(),
ForkJoinPool.defaultForkJoinWorkerThreadFactory,
null, true);
}
public static ScheduledExecutorService newScheduledThreadPool(int corePoolSize) {
return new ScheduledThreadPoolExecutor(corePoolSize);
}
public ScheduledThreadPoolExecutor(int corePoolSize) {
super(corePoolSize, Integer.MAX_VALUE, 0, NANOSECONDS,
new DelayedWorkQueue());
}
//1.以固定的频率
scheduledExecutor.scheduleAtFixedRate(() -> {
System.out.println("线程执行方法");
}, 0, 5, TimeUnit.SECONDS);
//2.以固定的延时
scheduledExecutor.scheduleWithFixedDelay(() -> {
System.out.println("线程执行方法");
}, 1, 5, TimeUnit.SECONDS);
public ScheduledFuture<?> scheduleAtFixedRate(Runnable command,
long initialDelay,
long period,
TimeUnit unit);
public ScheduledFuture<?> scheduleWithFixedDelay(Runnable command,
long initialDelay,
long delay,
TimeUnit unit);
public static ScheduledExecutorService newSingleThreadScheduledExecutor() {
return new DelegatedScheduledExecutorService
(new ScheduledThreadPoolExecutor(1));
}
//拒绝策略使用(通过 new ThreadPoolExecutor.xxx 的方式引入)
ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(1, 1,
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>(), new ThreadPoolExecutor.AbortPolicy());
欢迎关注公众号Java技术大本营,会不定期分享BAT面试资料等福利。