一、 ExecutorService 自身方法
- void shutdown;會拒絕繼續提交任務到隊列,但會把正在執行的任務和隊列中的任務執行完。
- boolean isShutdown;判斷是否開始了關閉工作,爲ture不代表線程池已經關閉,只代表可以執行關閉任務了。
- boolean isTerminated;代表線程已經真正終結了。
- boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedException; 等待一點時間,如果線程池終結返回true,沒有終結返回false,如果線程被打斷拋異常。
- List shutdownNow;立刻發送中斷信號,終止所有線程,並返回一個停止線程的隊列,要求線程能夠正確響應中斷。
- 使用 threadPoll.shutdown()和 threadPoll.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS);配合
package com.example.thread.threadlocal.scene1;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
public class ThreadLocalDemo08 {
public static ExecutorService threadPoll = new ThreadPoolExecutor(16, 16,
0L, TimeUnit.MILLISECONDS,
new ArrayBlockingQueue<>(1000));
public SimpleDateFormat simpleDateFormat = new SimpleDateFormat("mm:ss");
public static void main(String[] args) throws InterruptedException {
long st = System.currentTimeMillis();
for (int i = 0; i < 1000; i++) {
int finalI = i;
threadPoll.execute(() -> {
String date = date(finalI);
System.out.println(date);
});
}
threadPoll.shutdown();
threadPoll.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS);
if (threadPoll.isTerminated()) {
long et = System.currentTimeMillis();
System.out.println("耗時:" + (et - st) + "秒");
}
}
public static String date(int seconds) {
Date date = new Date(1000 * seconds);
SimpleDateFormat simpleDateFormat = ThreadSafeFormat.dataFormatterThreadLocal.get();
System.out.println(System.identityHashCode(simpleDateFormat));
return simpleDateFormat.format(date);
}
static class ThreadSafeFormatter06 {
public static ThreadLocal<SimpleDateFormat> simpleDateFormatThreadLocal = new ThreadLocal<SimpleDateFormat>() {
@Override
protected SimpleDateFormat initialValue() {
return new ThreadLocalDemo08().simpleDateFormat;
}
};
}
}
二、CountDownLatch類
- 使用CountDownLatch
package com.example.thread.threadlocal.scene1;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
public class ThreadLocalDemo06 {
public static ExecutorService threadPoll = new ThreadPoolExecutor(16, 16,
0L, TimeUnit.MILLISECONDS,
new ArrayBlockingQueue<>(1000));
public SimpleDateFormat simpleDateFormat = new SimpleDateFormat("mm:ss");
public static void main(String[] args) throws InterruptedException {
long st = System.currentTimeMillis();
CountDownLatch countDownLatch = new CountDownLatch(1000);
for (int i = 0; i < 1000; i++) {
int finalI = i;
threadPoll.execute(() -> {
String date = date(finalI);
System.out.println(date);
countDownLatch.countDown();
});
}
countDownLatch.await();
long et = System.currentTimeMillis();
System.out.println("耗時:"+(et-st)+"秒");
}
public static String date(int seconds) {
Date date = new Date(1000 * seconds);
SimpleDateFormat simpleDateFormat = ThreadSafeFormat.dataFormatterThreadLocal.get();
System.out.println(System.identityHashCode(simpleDateFormat));
return simpleDateFormat.format(date);
}
static class ThreadSafeFormatter06 {
public static ThreadLocal<SimpleDateFormat> simpleDateFormatThreadLocal = new ThreadLocal<SimpleDateFormat>() {
@Override
protected SimpleDateFormat initialValue() {
return new ThreadLocalDemo06().simpleDateFormat;
}
};
}
}