码迷,mamicode.com
首页 > 编程语言 > 详细

单线程和多线程在pc上执行花费时间测试

时间:2015-07-12 00:12:19      阅读:146      评论:0      收藏:0      [点我收藏+]

标签:

  1 package Thread;
  2 
  3 import junit.framework.Assert;
  4 import org.junit.Test;
  5 
  6 import java.util.concurrent.CountDownLatch;
  7 import java.util.concurrent.Executor;
  8 import java.util.concurrent.Executors;
  9 
 10 /**
 11  * Created by csophys on 15/7/11.
 12  *
 13  * @author csophys
 14  */
 15 public class SingleThreadAndThreadPoolExecuteCompare {
 16 
 17     @Test
 18     public void TestSimpleTask() {
 19         long 简单任务 = 10000000l;
 20         //普通计算方式
 21         long start = System.currentTimeMillis();
 22         单线程计算方式(简单任务);
 23         long end = System.currentTimeMillis();
 24         long 单线程计算花费时长 = end - start;
 25 
 26         //多线程计算方式
 27         start = System.currentTimeMillis();
 28         int 多线程数目 = 10;
 29         多线程计算方式(简单任务, 多线程数目);
 30         end = System.currentTimeMillis();
 31         long 多线程计算花费时长 = end - start;
 32 
 33         System.out.println("多线程计算花费时长(ms):" + 多线程计算花费时长);
 34         System.out.println("单线程计算花费时长(ms):" + 单线程计算花费时长);
 35         Assert.assertTrue(多线程计算花费时长 > 单线程计算花费时长);
 36     }
 37 
 38     @Test
 39     public void TestComplexTask(){
 40         long 复杂任务 = 1000000000l;
 41         //普通计算方式
 42         long start = System.currentTimeMillis();
 43         单线程计算方式(复杂任务);
 44         long end = System.currentTimeMillis();
 45         long 单线程计算花费时长 = end - start;
 46 
 47         //多线程计算方式
 48         start = System.currentTimeMillis();
 49         int 多线程数目 = 10;
 50         多线程计算方式(复杂任务, 多线程数目);
 51         end = System.currentTimeMillis();
 52         long 多线程计算花费时长 = end - start;
 53 
 54         System.out.println("多线程计算花费时长(ms):" + 多线程计算花费时长);
 55         System.out.println("单线程计算花费时长(ms):" + 单线程计算花费时长);
 56         Assert.assertTrue(多线程计算花费时长 < 单线程计算花费时长);
 57 
 58     }
 59 
 60     //如果同时运行的线程比较多,那线程执行一个具体任务t所化的时间会比用单一线程执行任务t所化的时间多很多
 61 
 62     private void 多线程计算方式(long targetValue, int segmentNum) {
 63         Executor executor = Executors.newFixedThreadPool(segmentNum);
 64         CountDownLatch countDownLatch = new CountDownLatch(segmentNum);
 65         for (int i = 0; i < segmentNum; i++) {
 66             executor.execute(new Task(countDownLatch, targetValue / segmentNum));
 67         }
 68         try {
 69             countDownLatch.await();
 70         } catch (InterruptedException e) {
 71             e.printStackTrace();
 72         }
 73 
 74     }
 75 
 76     private void 单线程计算方式(long targetValue) {
 77         doComputer(targetValue);
 78     }
 79 
 80     public static void doComputer(long targetValue) {
 81 
 82         System.out.println(Thread.currentThread() + "开始计算,targetValue=" + targetValue);
 83         long start = System.currentTimeMillis();
 84         while (targetValue > 0) {
 85             targetValue--;
 86         }
 87         System.out.println(Thread.currentThread() + "计算完成,花费时长(ms)=" + (System.currentTimeMillis() - start));
 88 
 89     }
 90 
 91 
 92 
 93     private class Task implements Runnable {
 94         CountDownLatch countDownLatch;
 95         long targetValue;
 96 
 97         public Task(CountDownLatch countDownLatch, long targeValue) {
 98             this.countDownLatch = countDownLatch;
 99             this.targetValue = targeValue;
100         }
101 
102         public void run() {
103             doComputer(targetValue);
104             countDownLatch.countDown();
105         }
106     }
107 }

 

单线程和多线程在pc上执行花费时间测试

标签:

原文地址:http://www.cnblogs.com/csophys/p/4639639.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!