多线程处理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
原创粉丝点击