JAVA多线程性能测试_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > JAVA多线程性能测试

JAVA多线程性能测试

 2011/11/25 9:18:41  kong0itey  http://kongcodecenter.iteye.com  我要评论(0)
  • 摘要:importjava.util.concurrent.CountDownLatch;publicclassTest{publicstaticvoidmain(String[]args){intnum=100000;test1(num);test2(num);}privatestaticvoidtest1(intmax){longt1=System.currentTimeMillis();intn=method1(max);longt2=System.currentTimeMillis()
  • 标签:多线程 测试 Java 性能测试 线程
import java.util.concurrent.CountDownLatch;

public class Test {

	public static void main(String[] args) {
		int num = 100000;
		test1(num);
		test2(num);
	}

	private static void test1(int max) {
		long t1 = System.currentTimeMillis();
		int n = method1(max);
		long t2 = System.currentTimeMillis();
		System.out.println("method1: value=" + n + ",time=" + (t2 - t1)
				/ 1000.0);
	}

	private static int method1(int max) {
		int num = 0;
		for (int i = 1; i <= max; i++) {
			boolean flag = true;
			for (int j = 2; j < i - 1; j++) {
				if (i % j == 0) {
					flag = false;
					break;
				}
			}
			if (flag && i > num)
				num = i;
		}
		return num;
	}

	private static int method2(int max) {
		int num = 0;
		for (int i = 1; i <= max; i++) {
			boolean flag = true;
			for (int j = 2; j < i - 1; j++) {
				if (i % j == 0) {
					flag = false;
					break;
				}
			}
			if (flag && i > num)
				num = i;
		}
		return num;
	}

	private static void test2(int max) {
		long t1 = System.currentTimeMillis();
		int threadNumber = 20;//线程数,在我的机器上20个线程效果最佳
		final CountDownLatch countDownLatch = new CountDownLatch(threadNumber);
		int step = max / threadNumber;
		for (int i = 0; i <= max; i += step) {
			if (i - step >= 0) {
				Calc calc = new Calc(i - step + 1, i, countDownLatch);
				Thread thread = new Thread(calc);
				thread.start();
			}
		}
		try {
			countDownLatch.await();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		long t2 = System.currentTimeMillis();
		System.out.println("method2: value=" + Calc.getVal() + ",time="
				+ (t2 - t1) / 1000.0);
	}
}

class Calc implements Runnable {

	private static Integer val = 0;

	private int min;
	private int max;
	private CountDownLatch cdl;

	public Calc(int min, int max, CountDownLatch cdl) {
		this.min = min;
		this.max = max;
		this.cdl = cdl;
	}

	public static int getVal() {
		return val;
	}

	public void run() {
		int num = 0;
		for (int i = min; i <= max; i++) {
			boolean flag = true;
			for (int j = 2; j < i - 1; j++) {
				if (i % j == 0) {
					flag = false;
					break;
				}
			}
			if (flag && i > num)
				num = i;
		}
		synchronized (val) {
			if (num > val)
				val = num;
		}
		cdl.countDown();
	}

}


在我的机器上测试效果如下:

50线程
method1: value=99991,time=7.797
method2: value=99991,time=3.672

30线程
method1: value=99991,time=7.813
method2: value=99989,time=3.797

20线程
method1: value=99991,time=7.782
method2: value=99991,time=3.797

10线程
method1: value=99991,time=7.719
method2: value=99991,time=4.109

不难看出,当计算量较大时候,多线程程序可以比普通程序快80%
发表评论
用户名: 匿名