Java 中的多线程查找素数需要更多时间?
Multithreading in Java findin prime number takes more time?
我试图找出这个问题的解决方案,但无法在 Whosebug 上找到它?
我只是想知道为什么我的多线程工作如此缓慢事实上它应该相反。
public class Prime {
static BufferedWriter writer;
static DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
public static void main(String[] args) throws IOException {
System.out.println("Without Thread" + findPrime() + " ms");
System.out.println("With thread : " + findPrimeWithThreads() + " ms");
}
public static long findPrimeWithThreads() {
Instant start = Instant.now();
int primaryNumber = 3;
while (primaryNumber <= 100000) {
int finalPrimaryNumber = primaryNumber;
new Thread(() -> {
multiplicationHelper(finalPrimaryNumber);
}).start();
new Thread(() -> {
multiplicationHelper(finalPrimaryNumber+1);
}).start();
primaryNumber+=2;
}
return Duration.between(start, Instant.now()).toMillis();
}
public static long findPrime() throws IOException {
Instant instant = Instant.now();
int primaryNumber = 3;
while (primaryNumber <= 100000) {
multiplicationHelper(primaryNumber);
primaryNumber++;
}
return Duration.between(instant, Instant.now()).toMillis();
}
public static void multiplicationHelper(int primaryNumber){
int j = 2;
boolean isPrime = true;
while (j <= primaryNumber/2) {
if (primaryNumber % j == 0) {
isPrime = false;
break;
}
j++;
}
if (isPrime) {
// System.out.println("PRIME :: " + primaryNumber);
}
}
}
这是代码,代码的输出是:
Without Thread497 ms
With thread : 22592 ms
请您详细说明为什么会这样以及如何提高多线程的性能?
我是多线程编程的新手,我是不是做错了什么?
"Finding prime numbers" 是一个 compute-bound 操作。它自然会使用 100% CPU 利用率,因为它永远不需要执行 I/O.
"multithreading" 的两个目的是:(a) 利用多个 CPU 核心,以及 (b) 与 I/O 重叠计算。 (并更轻松地发出并行 I/O 操作。)
多线程可以在正确的情况下节省时间,或在错误的情况下会花费更多的时间。
您的设计似乎启动了 20,000 个线程!
将您的功能更改为以下
public static long findPrimeWithThreads() {
Instant start = Instant.now();
int primaryNumber = 3;
ExecutorService pool = Executors.newFixedThreadPool(4); // considering you've 4 CPU
while (primaryNumber <= 100000) {
int finalPrimaryNumber = primaryNumber;
pool.submit(()->multiplicationHelper(finalPrimaryNumber));
primaryNumber ++;
}
pool.shutdown(); // stop your threads
return Duration.between(start, Instant.now()).toMillis();
}
我试图找出这个问题的解决方案,但无法在 Whosebug 上找到它?
我只是想知道为什么我的多线程工作如此缓慢事实上它应该相反。
public class Prime {
static BufferedWriter writer;
static DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
public static void main(String[] args) throws IOException {
System.out.println("Without Thread" + findPrime() + " ms");
System.out.println("With thread : " + findPrimeWithThreads() + " ms");
}
public static long findPrimeWithThreads() {
Instant start = Instant.now();
int primaryNumber = 3;
while (primaryNumber <= 100000) {
int finalPrimaryNumber = primaryNumber;
new Thread(() -> {
multiplicationHelper(finalPrimaryNumber);
}).start();
new Thread(() -> {
multiplicationHelper(finalPrimaryNumber+1);
}).start();
primaryNumber+=2;
}
return Duration.between(start, Instant.now()).toMillis();
}
public static long findPrime() throws IOException {
Instant instant = Instant.now();
int primaryNumber = 3;
while (primaryNumber <= 100000) {
multiplicationHelper(primaryNumber);
primaryNumber++;
}
return Duration.between(instant, Instant.now()).toMillis();
}
public static void multiplicationHelper(int primaryNumber){
int j = 2;
boolean isPrime = true;
while (j <= primaryNumber/2) {
if (primaryNumber % j == 0) {
isPrime = false;
break;
}
j++;
}
if (isPrime) {
// System.out.println("PRIME :: " + primaryNumber);
}
}
}
这是代码,代码的输出是:
Without Thread497 ms
With thread : 22592 ms
请您详细说明为什么会这样以及如何提高多线程的性能? 我是多线程编程的新手,我是不是做错了什么?
"Finding prime numbers" 是一个 compute-bound 操作。它自然会使用 100% CPU 利用率,因为它永远不需要执行 I/O.
"multithreading" 的两个目的是:(a) 利用多个 CPU 核心,以及 (b) 与 I/O 重叠计算。 (并更轻松地发出并行 I/O 操作。)
多线程可以在正确的情况下节省时间,或在错误的情况下会花费更多的时间。
您的设计似乎启动了 20,000 个线程!
将您的功能更改为以下
public static long findPrimeWithThreads() {
Instant start = Instant.now();
int primaryNumber = 3;
ExecutorService pool = Executors.newFixedThreadPool(4); // considering you've 4 CPU
while (primaryNumber <= 100000) {
int finalPrimaryNumber = primaryNumber;
pool.submit(()->multiplicationHelper(finalPrimaryNumber));
primaryNumber ++;
}
pool.shutdown(); // stop your threads
return Duration.between(start, Instant.now()).toMillis();
}