ThreadPoolExecutor的使用问题
大家帮忙看一下这么写有没有问题?
测试中发现poo.getPoolSize()一直在增加,不理解。
package zzzzzz;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
public class TT implements Runnable {
private static boolean next = true;
private static ThreadPoolExecutor pool;
public static List<String> list = new ArrayList<String>();
private static List<String> ns = new ArrayList<String>();
static {
//初始化线程池
pool = new ThreadPoolExecutor(25, 50, 35,
TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(35),
new ThreadPoolExecutor.DiscardOldestPolicy());
ns.add("w1");
ns.add("w2");
ns.add("w3");
ns.add("w4");
ns.add("w5");
}
public void setNext(boolean next) {
this.next = next;
}
public static void stop() {
System.err.println("[TEST][TT] Service is stopping ......");
next = false;
if(pool != null) pool.shutdown();
}
public void run() {
System.err.println("[TEST][TT] Service is start ......");
// TODO Auto-generated method stub
Writer w = null;
while(next){
for(String s : ns) {
if(list.contains(s)){
System.err.println("[TEST][TT] thread "+s+" is running ,Leaving for now.");
} else {
w = new Writer(s);
pool.execute(w);
list.add(s);
}
}
try { Thread.sleep(5000);
} catch (InterruptedException e) { e.printStackTrace(); }
}
}
public static void listPool() {
System.err.println("pool.getActiveCount() = " + pool.getActiveCount());
System.err.println("pool.getPoolSize() = " + pool.getPoolSize());
System.err.println("pool.getTaskCount() = "+pool.getTaskCount());
System.err.println("pool.getCompletedTaskCount() = "+pool.getCompletedTaskCount());
System.err.println("pool.getMaximumPoolSize() = "+pool.getMaximumPoolSize());
System.err.println("pool.getLargestPoolSize() = "+pool.getLargestPoolSize());
System.err.println("pool.getCorePoolSize() = "+pool.getCorePoolSize());
ArrayBlockingQueue<Runnable> queue = (ArrayBlockingQueue<Runnable>)pool.getQueue();
System.out.println("queue.size() = " + queue.size());
}
public static void list() {
for(String s : list) {
System.out.println("[TT][list]------------->"+s);
}
}
}
class Writer implements Runnable{
private String name;
//private ThreadPoolExecutor pool;
public Writer(String name){
this.name = name;
//this.pool = pool;
}
public void run() {
// TODO Auto-generated method stub
//try {
//Thread.sleep(5000);
//} catch (InterruptedException e) { e.printStackTrace(); }
System.err.println("[TEST][Writer]" + name + " is run end !");
if(TT.list.contains(name)) TT.list.remove(name);
//pool.remove(this);
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
引用来自#8楼“KINSEY”的帖子
引用来自#6楼“heguangdong”的帖子
25是核心线程,涨到最大线程数50应该都是正常的,难道我的机器比你快,仅用25个就都能跑完?
引用来自#7楼“KINSEY”的帖子
我的问题是线程都运行完了,pool.getPoolSize()的值还是25
有没有那个方法能得到这个线程池里面活跃的线程数?
引用来自#6楼“heguangdong”的帖子
25是核心线程,涨到最大线程数50应该都是正常的,难道我的机器比你快,仅用25个就都能跑完?
我的问题是线程都运行完了,pool.getPoolSize()的值还是25
有没有那个方法能得到这个线程池里面活跃的线程数?
25是核心线程,涨到最大线程数50应该都是正常的,难道我的机器比你快,仅用25个就都能跑完?
引用来自#4楼“heguangdong”的帖子
刚才跑了一下你的程序,一直维持在25个核心线程,没发现你说的结果?
刚才跑了一下你的程序,一直维持在25个核心线程,没发现你说的现象?
引用来自#2楼“heguangdong”的帖子
构造方法第三个参数 keepAliveTime为0
构造方法第三个参数 keepAliveTime为0