java在for循环中使用concurrent包进行多线程编程
2009-12-20 02:14
513 查看
最近在做接口的时候总是遇到一个for语句中 每次循环会涉及很多资源,包括 ftp io db,总想用现场来控制太.找到一篇文章 http://daoger.javaeye.com/blog/142485 写的不错.自己写了2个demo
1. 主线程不等待
public class CopyOfTestThreadPool {
public static void main(String args[]) throws InterruptedException {
// only two threads
ExecutorService exec = Executors.newFixedThreadPool(20);
List<Long> list = new ArrayList<Long>();
for(int index = 0; index < 1000000; index++){
list.add(System.nanoTime());
}
long start = System.currentTimeMillis();
for (Long long1 : list) {
final Long l = long1;
exec.execute(new Runnable(){
public void run() {
System.out.println(l);
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}});
}
// must shutdown
exec.shutdown();
long end = System.currentTimeMillis();
System.out.print("共计用时 ");
System.out.println(end - start);
}
}
2 主线程会等待
public class TestCountDownLatch {
public static void main(String[] args) throws InterruptedException {
long start = System.currentTimeMillis();
// 开始的倒数锁
final CountDownLatch begin = new CountDownLatch(1);
// 结束的倒数锁
final CountDownLatch end = new CountDownLatch(10000);
// 十名选手
final ExecutorService exec = Executors.newFixedThreadPool(10);
for (int index = 0; index < 10000; index++) {
final int NO = index + 1;
Runnable run = new Runnable() {
public void run() {
try {
begin.await();
Thread.sleep((long) (Math.random() * 10));
} catch (InterruptedException e) {
} finally {
end.countDown();
}
}
};
exec.submit(run);
}
System.out.println("Game Start");
begin.countDown();
end.await();
System.out.println("Game Over");
exec.shutdown();
System.out.print("共计用时 ");
System.out.println(System.currentTimeMillis() - start);
}
}
详细看上面那位老兄的blog
1. 主线程不等待
public class CopyOfTestThreadPool {
public static void main(String args[]) throws InterruptedException {
// only two threads
ExecutorService exec = Executors.newFixedThreadPool(20);
List<Long> list = new ArrayList<Long>();
for(int index = 0; index < 1000000; index++){
list.add(System.nanoTime());
}
long start = System.currentTimeMillis();
for (Long long1 : list) {
final Long l = long1;
exec.execute(new Runnable(){
public void run() {
System.out.println(l);
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}});
}
// must shutdown
exec.shutdown();
long end = System.currentTimeMillis();
System.out.print("共计用时 ");
System.out.println(end - start);
}
}
2 主线程会等待
public class TestCountDownLatch {
public static void main(String[] args) throws InterruptedException {
long start = System.currentTimeMillis();
// 开始的倒数锁
final CountDownLatch begin = new CountDownLatch(1);
// 结束的倒数锁
final CountDownLatch end = new CountDownLatch(10000);
// 十名选手
final ExecutorService exec = Executors.newFixedThreadPool(10);
for (int index = 0; index < 10000; index++) {
final int NO = index + 1;
Runnable run = new Runnable() {
public void run() {
try {
begin.await();
Thread.sleep((long) (Math.random() * 10));
} catch (InterruptedException e) {
} finally {
end.countDown();
}
}
};
exec.submit(run);
}
System.out.println("Game Start");
begin.countDown();
end.await();
System.out.println("Game Over");
exec.shutdown();
System.out.print("共计用时 ");
System.out.println(System.currentTimeMillis() - start);
}
}
详细看上面那位老兄的blog
相关文章推荐
- java在for循环中使用concurrent包进行多线程编程
- java 使用线程池进行多线程编程
- 使用AES算法对文件进行加密/解密的操作(JAVA)
- JAVA使用EPoll来进行NIO处理的方法(转)
- java中使用MD5进行加密
- 使用JS对中文字符串进行utf-8的Base64编码,使其与Java编码相同的办法
- 使用 Java 进行图像处理 - 图像翻转
- 深入分析Java使用+和StringBuilder进行字符串拼接的差异
- 使用 Java 配置进行 Spring bean 管理
- 【学习笔记】系列十三:使用JMeter进行性能测试之Java请求
- java中使用MD5加密算法进行加密
- 使用ICTCLAS JAVA版(ictclas4j)进行中文分词
- 使用Java自带API进行MD5加密
- Java基础之泛型——使用二叉树进行排序(TryBinaryTree)
- Java中使用DES进行加密解密
- Java之使用json-lib进行对象与json字符串之间的转换
- 如何使用java synchronized进行线程同步
- Java使用DateUtils对日期进行数学运算经典应用示例【附DateUtils相关包文件下载】
- Redis-Java中使用Jedis进行操作
- java 通过使用wait和notify进行线程之间通信(代码)