通过源码可以看到,本质上,Executors.newSingleThreadExecutor();或者Executors.newFixedThreadPool(args);等等,都是在new ThreadPoolExecutor();只是给ThreadPoolExecutor传递参数不通罢了。
最近项目中有这样一个功能:需要短时间内调用外部接口发送大量短信。于是想到了线程池。发送短信有多个账号,并且每个账号也可以多个线程同步进行,由于线程池这块之前用的不多,所以先写了个例子试试:
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import utils.RandomUtils;
public class ThreadPool1 {
public static void main(String[] args) {
<span style="white-space:pre"> </span>// 创建固定大小的线程池
ExecutorService pool = Executors.newFixedThreadPool(3);
for (int i = 0; i < 100; i++) {
Thread t10 = new Thread(new MyTest());
pool.execute(t10);
}
pool.shutdown();
}
}
class MyTest implements Runnable {
// 模拟存储可用的短信发送账号
protected static ConcurrentHashMap<String, String> map = new ConcurrentHashMap<>();
static {
map.put("key1", "account1");
map.put("key2", "account2");
map.put("key3", "account3");
}
@Override
public void run() {
String key = null;
String value = null;
try {
synchronized (MyTest.class) { // 避免多线程并行key可能取到null
key = RandomUtils.getRandomKeyFromMap(map); // 从map随机获取一个key
value = map.get(key);
map.remove(key);
}
} catch (Exception e1) {
e1.printStackTrace();
System.out.println(key + " " + map.size());
}
System.out.println("use key:" + key + " value:" + value);
try {
Thread.sleep(1000L);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
map.put(key, value);
System.out.println("back " + key + " size:" + map.size());
}
}
}
模拟的3个账号,3个线程;前面说到一个账号可以多个线程同时发送,只需要把每个账号用不同key往map里多put几个即可。
测试运行无异常。