【发布时间】:2017-04-28 05:51:00
【问题描述】:
我实现了在没有池的情况下使用两个线程计算阶乘。我有两个阶乘类,分别命名为 Factorial1、Factorial2 并扩展了 Thread 类。让我们考虑一下我想计算!160000 的值。在 Factorial1 的 run() 方法中,我在 for 循环中从 i=2 到 i=80000 以及在 Factorial2 中从 i=80001 到 160000 进行乘法。之后,我返回两个值并在 main 方法中将它们相乘。当我比较执行时间时(即 5000 毫秒)比非线程计算的时间(15000 毫秒)要好得多,即使有两个线程也是如此。
现在我想写干净更好的代码,因为我在阶乘计算中看到了线程的效率,但是当我使用线程池计算阶乘值时,并行计算总是比非线程计算花费更多时间(几乎16000)。我的代码片段如下所示:
for(int i=2; i<= Calculate; i++)
{
myPool.execute(new Multiplication(result, i));
}
在乘法类中的run()方法:
public void run()
{
s1.Mltply(s2); // s1 and s2 are instances of my Number class
// their fields holds BigInteger values
}
Number 类中的 Mltply() 方法:
public void Multiply(int number)
{
area.lock(); // result is going wrong without lock
Number temp = new Number(number);
value = value.multiply(temp.value); // value is a BigInteger
area.unlock();
}
在我看来,这个锁可能会扼杀线程使用的所有优势,因为看起来线程所做的只是乘法,而没有别的。但没有它,我什至无法计算出真正的结果。假设我要计算 !10,所以 thread1 计算 10*9*8*7*6,thread2 计算 5*4*3*2*1。这就是我要找的方式吗?甚至可以使用线程池吗?当然执行时间必须小于正常计算...
感谢您的所有帮助和建议。
编辑:-我自己解决问题的方法-
public class MyMultiplication implements Runnable
{
public static BigInteger subResult1;
public static BigInteger subResult2;
int thread1StopsAt;
int thread2StopsAt;
long threadId;
static boolean idIsSet=false;
public MyMultiplication(BigInteger n1, int n2) // First Thread
{
MyMultiplication.subResult1 = n1;
this.thread1StopsAt = n2/2;
thread2StopsAt = n2;
}
public MyMultiplication(int n2,BigInteger n1) // Second Thread
{
MyMultiplication.subResult2 = n1;
this.thread2StopsAt = n2;
thread1StopsAt = n2/2;
}
@Override
public void run()
{
if(idIsSet==false)
{
threadId = Thread.currentThread().getId();
idIsSet=true;
}
if(Thread.currentThread().getId() == threadId)
{
for(int i=2; i<=thread1StopsAt; i++)
{
subResult1 = subResult1.multiply(BigInteger.valueOf(i));
}
}
else
{
for(int i=thread1StopsAt+1; i<= thread2StopsAt; i++)
{
subResult2 = subResult2.multiply(BigInteger.valueOf(i));
}
}
}
}
public class JavaApplication3
{
public static void main(String[] args) throws InterruptedException
{
int calculate=160000;
long start = System.nanoTime();
BigInteger num = BigInteger.valueOf(1);
for (int i = 2; i <= calculate; i++)
{
num = num.multiply(BigInteger.valueOf(i));
}
long end = System.nanoTime();
double time = (end-start)/1000000.0;
System.out.println("Without threads: \t" +
String.format("%.2f",time) + " miliseconds");
System.out.println("without threads Result: " + num);
BigInteger num1 = BigInteger.valueOf(1);
BigInteger num2 = BigInteger.valueOf(1);
ExecutorService myPool = Executors.newFixedThreadPool(2);
start = System.nanoTime();
myPool.execute(new MyMultiplication(num1,calculate));
Thread.sleep(100);
myPool.execute(new MyMultiplication(calculate,num2));
myPool.shutdown();
while(!myPool.isTerminated()) {} // waiting threads to end
end = System.nanoTime();
time = (end-start)/1000000.0;
System.out.println("With threads: \t" +String.format("%.2f",time)
+ " miliseconds");
BigInteger result =
MyMultiplication.subResult1.
multiply(MyMultiplication.subResult2);
System.out.println("With threads Result: " + result);
System.out.println(MyMultiplication.subResult1);
System.out.println(MyMultiplication.subResult2);
}
}
输入:!160000 无线程执行时间:15000 毫秒 2个线程的执行时间:4500毫秒
感谢您的想法和建议。
【问题讨论】:
-
你遗漏了太多细节:minimal reproducible example
-
这条线是做什么的:
Number temp = new Number(number);我看不到 temp 正在使用。 -
除非您计算一个非常大的数字的阶乘,例如 10,000,否则您不会看到多线程带来的一致改进。线程管理的开销将压倒拆分计算的任何优势,并且由于 JIT 预热时间,您无法对 10,000 次迭代计数进行微基准测试。简而言之,对于这样一个小问题,您不需要也不想要多线程。
-
@Jhonny007 我在 multiply() 函数中使用 temp
-
@JimGarrison 我在上面提到过,我在没有线程(15000 毫秒)和两个线程(5000 毫秒)的情况下计算了 !160000。所以我认为 160000 比 10000 大得多,对于这么小的过程来说,10 秒并不是一个“小”的时间。我想知道如何使用线程池实现相同的目标,无论如何,谢谢。
标签: java multithreading threadpool factorial