一、线程池——单线程
public static void runSingleThreadPool() {
ExecutorService threadPool0 = Executors.newSingleThreadExecutor();
threadPool0.execute(new FastTask("task0"));
threadPool0.execute(new FastTask("task1"));
threadPool0.execute(new FastTask("task2"));
}
public static void runSingleThreadPoolWithFactory() {
ExecutorService threadPool0 = Executors.newSingleThreadExecutor(new MyThreadFactory());
threadPool0.execute(new FastTask("task0"));
threadPool0.execute(new FastTask("task1"));
threadPool0.execute(new FastTask("task2"));
}
private static class MyThreadFactory implements ThreadFactory {
private static int count = 0;
@Override
public Thread newThread(Runnable r) {
Log.i(TAG, "第 " + (count++) + " 个线程创建");
return new Thread(r);
}
}
二、线程池——固定线程数
public static final void runFixedThreadPool() {
ExecutorService threadPool = Executors.newFixedThreadPool(2);
for (int i = 0; i < 10; i++) {
threadPool.execute(new FastTask("task" + i));
}
}
public static void runFixedThreadPoolWithFactory() {
ExecutorService threadPool = Executors.newFixedThreadPool(2, new MyThreadFactory());
for (int i = 0; i < 10; i++) {
threadPool.execute(new FastTask("task" + i));
}
}
三、线程池——动态线程数
public static void runCachedThreadPool() {
ExecutorService threadPool = Executors.newCachedThreadPool();
for (int i = 0; i < 10; i++) {
threadPool.execute(new FastTask("task" + i));
}
}
public static void runCachedThreadPoolWithFactory() {
ExecutorService threadPool = Executors.newCachedThreadPool(new MyThreadFactory());
for (int i = 0; i < 10; i++) {
threadPool.execute(new FastTask("task" + i));
}
}
四、自定义线程池
public static void runCustomThreadPoolWithFactory() {
// 创建一个线程执行
ExecutorService threadPool = new ThreadPoolExecutor(0, 5,
60L, TimeUnit.SECONDS,
new LinkedBlockingDeque<Runnable>(20),
new MyThreadFactory());
}
五、线程池关闭
public static void runShutdown() {
ExecutorService threadPool = new ThreadPoolExecutor(2, 10,
60L, TimeUnit.SECONDS,
new LinkedBlockingDeque<Runnable>(10),
new MyThreadFactory(),
new RejectedExecutionHandler() {
@Override
public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
Log.i(TAG, "被拒任务名称是: " + r.toString());
}
});
Log.i(TAG, "threadPool isShutdown: " + threadPool.isShutdown());
// threadPool.shutdown();
threadPool.shutdownNow();
Log.i(TAG, "thread shutdown!!!");
Log.i(TAG, "threadPool isShutdown: " + threadPool.isShutdown());
threadPool.execute(new SlowTask("Task lastest"));
}
六、线程池—移除任务
ThreadPoolExecutor threadPool = new ThreadPoolExecutor(2, 10,
60L, TimeUnit.SECONDS,
new LinkedBlockingDeque<Runnable>(100));
for (int i = 0; i < 20; i++) {
try {
threadPool.execute(new SlowTask("Task" + i));
} catch (Exception e) {
Log.e(TAG, e.toString());
}
}
Runnable lastTask = new SlowTask("Task should be removed");
threadPool.execute(lastTask);
threadPool.getQueue().remove(lastTask);
七、线程池——获取任务执行结果
//获取任务执行结果
public static void runSubmit() {
ExecutorService threadPool = Executors.newFixedThreadPool(2);
Future<String> future0 = threadPool.submit(new Callable<String>() {
@Override
public String call() throws Exception {
return "Callable result";
}
});
Future<?> future1 = threadPool.submit(new SlowTask("SlowTask1"));
Future<?> future2 = threadPool.submit(new SlowTask("SlowTask2"),
"Runnable result");
try {
Log.i(TAG, "future0: " + future0.get());
Log.i(TAG, "future1: " + future1.get());
Log.i(TAG, "future2: " + future2.get());
} catch (Exception e) {
Log.e(TAG, e.toString());
}
}