本文共 1069 字,大约阅读时间需要 3 分钟。
Java codepackage test;
import java.io.IOException;
import java.io.InputStream;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.Properties;
public class TestThreadPool {
private static int produceTaskSleepTime ;
private static int produceTaskMaxNumber ;
public static void main(String[] args) {
InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("Config.properties");
Properties p = new Properties();
try {
p.load(inputStream);
} catch (IOException e1) {
e1.printStackTrace();
}
// 构造一个线程池
ThreadPoolExecutor threadPool = new ThreadPoolExecutor(2, 4, 3,
TimeUnit.SECONDS, new ArrayBlockingQueue(3),
new ThreadPoolExecutor.DiscardOldestPolicy());
for (int i = 1; i <= p.getProperty(produceTaskMaxNumber); i++) {
try {
String task = "task@ " + i;
System.out.println("创建任务并提交到线程池中:" + task);
threadPool.execute(new ThreadPoolTask(task));
Thread.sleep(p.getProperty.(produceTaskSleepTime));
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
转载地址:https://blog.csdn.net/weixin_33272631/article/details/116044569 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!