这个线程程序显示我每次不同的答案 [英] This thread program shows me different answers every time
问题描述
这是一个Java程序,可从1-500000查找具有最大除数的数字。
public class Medium2 {
static int count1 = 1;
static int count2 = 1;
static int big_count = 0;
static int big = 0;
主方法
code> public static void main(String [] args){
Runnable runnable1 = new Runnable(){
public void run(){
/ pre>
这里的实现
for(int num = 1; num <= 500000; num ++){
for(int i = 2; i <= num; i ++){
if(num%i == 0){// Actual Logic
count1 ++;
}
}
if(count1> big_count){
big_count = count1; //除数:
big = num; // Largest Number
}
count1 = 1;
}
}
};
和线程执行
Thread thread1 = new Thread(runnable1); // Threads
Thread thread2 = new Thread(runnable1);
thread1.start();
thread2.start();
try {
thread1.join();
thread2.join();
} catch(InterruptedException ie){
;
}
System.out.println(Biggest:+ big +\\\
Number of Divisors for+ big +=+ big_count);
}
}
但每次都给出不同的答案。实际的答案是:498960和200个divisors
解决方案关于你的目标,你的实现可能会有问题。由于
big_count
和big
对于两个线程都是常见的,并且当线程尝试修改这些线程时没有任何保护,您的程序应该创建错误。
除此之外,你也没有使用2个线程,因为这两个线程都是从1到500000的计算。
因为你的计算逻辑似乎确定,你应该得到你想要的输出,当你尝试使用单线程。
如果你想它做两个线程,你可以容易尝试这个。
-
您应该拥有
big_count1
,big1
和big_count2
,big2
。因此,以'1'结尾的变量只由thread1使用,以'2'结尾的变量只由thread2使用。 -
-
join()
之后,检查从1到250000和thread2是否从250001到500000 ,只需比较big_count1
和big_count2
,就可以推导出最终答案。 :))
This is a Java Program to Find The Number with Largest Divisors from 1-500000.
public class Medium2 {
static int count1 = 1;
static int count2 = 1;
static int big_count = 0;
static int big = 0;
Main method
public static void main(String[] args) {
Runnable runnable1 = new Runnable() {
public void run() {
The implementation goes here
for (int num = 1; num <= 500000; num++) {
for (int i = 2; i <= num; i++) {
if (num % i == 0) { //Actual Logic
count1++;
}
}
if (count1 > big_count) {
big_count = count1; //Number of Divisors
big = num; //Largest Number
}
count1 = 1;
}
}
};
And the thread execution
Thread thread1 = new Thread(runnable1); //Threads
Thread thread2 = new Thread(runnable1);
thread1.start();
thread2.start();
try {
thread1.join();
thread2.join();
} catch (InterruptedException ie) {
;
}
System.out.println("Biggest: " + big + "\nNumber of Divisors for " + big + " = " + big_count);
}
}
But it gives different answers every time. The actual answer is : 498960 and 200 Divisors
Concerning your goal, your implementation should probably have problems. Since big_count
and big
is common for both threads and don't have any protection when threads are trying to modify those, your program should create errors.
Other than that, you are also not utilizing 2 threads, since both threads are doing calculation from 1 to 500000.
Since your calculation logic seems ok, you should get your desired output when you try with single thread.
If you want it to do by two threads, you can easily try this. (just to verify, not the nicest way)
You should have
big_count1
,big1
andbig_count2
,big2
. So that variables whose names end with '1' is only using by thread1 and variables whose names end with '2' is only using by thread2.Assign thread1 to check from 1 to 250000 and thread2 to from 250001 to 500000.
After
join()
s, just comparebig_count1
andbig_count2
, then you can deduce the final answer. :))
这篇关于这个线程程序显示我每次不同的答案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!