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());
}
}
}
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());
}
}
}
相关文章推荐
- SpringMVC学习系列(3) 之 URL请求到Action的映射规则
- JAVA导出Excel
- Java dbf文件写入远程服务器
- SpringMVC学习系列(2) 之 经典的HelloWorld实现
- Java开发中的23种设计模式详解(转)
- Spring如何下载
- UNICODE转码成中文
- java实现括号匹配
- 使用Eclipse for J2EE 开发Web程序
- java调用R 画词云
- 深入理解Java:SimpleDateFormat安全的时间格式化
- Java JDBC连接数据库 Access连接数据库
- SpringMVC学习系列(1) 之 初识SpringMVC
- JAVA 实现 IMEI校验码算法
- java中字符流的总结
- eclipse的svn连接不上 request failed
- java数字图像处理基础使用imageio写图像文件示例(转载)
- 尝试编写的rabbitmq+spring 框架
- Java发送邮件(带附件)
- myeclipse 编辑java中的变量时,高亮显示