多线程处理for循环
来源:互联网 发布:最准确的生男生女算法 编辑:程序博客网 时间:2024/04/29 08:29
package Thread;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;import java.util.concurrent.atomic.AtomicInteger;public class TestThreadPool {private static final int loopNum = 1*10000;private static final int sleepTime = 20000;private static final AtomicInteger connectionIds = new AtomicInteger(0);public static void main(String args[]) throws InterruptedException {// only two threadsTestThreadPool TestThreadPool = new TestThreadPool();long bt = System.currentTimeMillis();TestThreadPool.m1();long et2 = System.currentTimeMillis();System.out.println("耗时:"+(et2 - bt)+ "ms");Thread thread = new Thread();thread.sleep(sleepTime);if(connectionIds.get()==loopNum){long et = System.currentTimeMillis();System.out.println("耗时:"+(et - bt-sleepTime)+ "ms");}}public void m1() {ExecutorService exec = Executors.newFixedThreadPool(2);for (int index = 0; index < loopNum; index++) {Runnable run = new Runnable() {public void run() {try {new Thread().sleep(1);connectionIds.getAndIncrement();} catch (Exception e) {}}};exec.execute(run);}// must shutdownexec.shutdown();}public void m2() {for (int index = 0; index < loopNum; index++) {try {new Thread().sleep(1);connectionIds.getAndIncrement();} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}}}}
0 0
- 多线程处理for循环
- CoffeeScript for循环处理
- 如何在for循环中使用多线程
- for循环中多线程的无序性
- 使用多线程优化双重for循环校验
- for循环程序块的处理顺序
- for循环中删除精灵的处理
- 图像处理for循环初始化问题
- for循环、list和map处理
- Oracle PL/SQL中的循环处理(sql for循环)
- Oracle PL/SQL中的循环处理(sql for循环)
- Oracle PL/SQL中的循环处理(sql for循环)
- js循环处理的表达式 for、for in、while
- linux多线程编程--对三层for循环的优化
- 普通for循环和增强for循环处理数组元素的一个差别
- python 列表处理,for循环语句,if条件判断
- for循环里面进行数据库查询的处理
- iOS for循环创建UIButton及点击处理
- Struts2 拦截器处理普通Http请求和Ajax请求时拦截配置
- storm部署安装deploy
- 内置函数
- JSP页面跳转的五种方法
- NSMutableAttributedString
- 多线程处理for循环
- 限制在同一台电脑上只允许有一个用户登录系统
- Android activity 之间的数据传递 bundle
- java 子类重写父类的方法应注意的问题
- ssh项目中启动tomcat报错:session not found
- C#创建windows服务
- IOS推送详解
- POI读取Excel(兼容Excel2003、Excel2007)
- Eclipse——工作台