您的位置:首页 > 编程语言 > Java开发

Java自带线程池

2015-11-17 14:24 567 查看
package thread;

import org.junit.Test;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
* Created by guofeipeng on 15/11/17.
* 线程池测试
*/
public class TestThreadPool {

@Test
public void testThradPool(){
ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(30, 50, 60L, TimeUnit.SECONDS,
new SynchronousQueue<Runnable>());
for(int i = 0 ; i < 10 ; i++){
Thread t = new Thread(new Task());
t.setName("thread" + i);
threadPoolExecutor.execute(t);
}
threadPoolExecutor.shutdown();
while (!threadPoolExecutor.isTerminated()) {

}
}

class Task implements Runnable{
@Override
public void run() {
System.out.println("task " + Thread.currentThread().getName());
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: