Java 线程池内容,全在这里了_并发编程

Java 线程池内容,全在这里了_线程池_02

/**
* 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));
}
}

Java 线程池内容,全在这里了_线程池_03

Java 线程池内容,全在这里了_并发编程_04

Java 线程池内容,全在这里了_多线程_05

Fork/Join框架介绍

4.ThreadPoolExecutor 核心参数

public ThreadPoolExecutor(int corePoolSize,
int maximumPoolSize,
long keepAliveTime,
TimeUnit unit,
BlockingQueue<Runnable> workQueue,
ThreadFactory threadFactory,
RejectedExecutionHandler handler)

Java 线程池内容,全在这里了_并发编程_06

Java 线程池内容,全在这里了_i++_07Java 线程池内容,全在这里了_线程池_08

Java 线程池内容,全在这里了_并发编程_09

public static ExecutorService newSingleThreadExecutor() {
return new FinalizableDelegatedExecutorService
(new ThreadPoolExecutor(1, 1,
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>()));
}

Java 线程池内容,全在这里了_ide_10

Java 线程池内容,全在这里了_ide_11

Java 线程池内容,全在这里了_线程池_12

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);
@Override
public Thread newThread(Runnable r) {
return new Thread(r,"mypool_t"+t.getAndIncrement());
}
});

Java 线程池内容,全在这里了_多线程_13

Java 线程池内容,全在这里了_并发编程_14

public static ExecutorService newCachedThreadPool() {
return new ThreadPoolExecutor(0, Integer.MAX_VALUE,
60L, TimeUnit.SECONDS,
new SynchronousQueue<Runnable>());
}

Java 线程池内容,全在这里了_线程池_15

Java 线程池内容,全在这里了_i++_16

public static ExecutorService newWorkStealingPool() {
return new ForkJoinPool
(Runtime.getRuntime().availableProcessors(),
ForkJoinPool.defaultForkJoinWorkerThreadFactory,
null, true);
}

Java 线程池内容,全在这里了_i++_17

​Fork/join框架介绍​

Java 线程池内容,全在这里了_线程池_18

public static ScheduledExecutorService newScheduledThreadPool(int corePoolSize) {
return new ScheduledThreadPoolExecutor(corePoolSize);
}

public ScheduledThreadPoolExecutor(int corePoolSize) {
super(corePoolSize, Integer.MAX_VALUE, 0, NANOSECONDS,
new DelayedWorkQueue());
}

Java 线程池内容,全在这里了_多线程_19

//1.以固定的频率
scheduledExecutor.scheduleAtFixedRate(() -> {
System.out.println("线程执行方法");
}, 0, 5, TimeUnit.SECONDS);

//2.以固定的延时
scheduledExecutor.scheduleWithFixedDelay(() -> {
System.out.println("线程执行方法");
}, 1, 5, TimeUnit.SECONDS);

Java 线程池内容,全在这里了_i++_20

public ScheduledFuture<?> scheduleAtFixedRate(Runnable command,
long initialDelay,
long period,
TimeUnit unit);

Java 线程池内容,全在这里了_多线程_21

public ScheduledFuture<?> scheduleWithFixedDelay(Runnable command,
long initialDelay,
long delay,
TimeUnit unit);

Java 线程池内容,全在这里了_多线程_22

public static ScheduledExecutorService newSingleThreadScheduledExecutor() {
return new DelegatedScheduledExecutorService
(new ScheduledThreadPoolExecutor(1));
}

Java 线程池内容,全在这里了_i++_23

Java 线程池内容,全在这里了_多线程_24

Java 线程池内容,全在这里了_线程池_25

//拒绝策略使用(通过 new ThreadPoolExecutor.xxx 的方式引入)
ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(1, 1,
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>(), new ThreadPoolExecutor.AbortPolicy());



欢迎关注公众号Java技术大本营,会不定期分享BAT面试资料等福利。

Java 线程池内容,全在这里了_并发编程_26