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%