java 使用 5 个线程添加数字
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29619374/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Add numbers using 5 threads
提问by UserJS
Question : I have to create 5 threads where each thread has to perform the addition operation.
问题:我必须创建 5 个线程,其中每个线程都必须执行加法操作。
- Thread1 - Add 1 to 10
- Thread2 - Add 1 to 50
- Thread3 - Add 5 to 15
- Thread4 - Add 10 to 20
- Thread5 - Add 15 to 20
- 线程 1 - 将 1 添加到 10
- 线程 2 - 将 1 添加到 50
- 线程 3 - 将 5 添加到 15
- 线程 4 - 将 10 添加到 20
- Thread5 - 将 15 添加到 20
What is the best way to accomplish this? Also, I need 1 sec time delay between each addition operation. I have written this code: My output is wrong and changing every time. I know problem is with synchronized but not able solve.
实现这一目标的最佳方法是什么?另外,每个加法操作之间我需要 1 秒的时间延迟。我写了这段代码:我的输出是错误的并且每次都在变化。我知道问题是同步的,但无法解决。
class adding implements Runnable{
int a,b;
public adding(int a, int b){
this.a = a;
this.b = b;
}
public void run() {
add(a,b);
}
public void add(int a, int b){
int sum=0;
synchronized (this) {
for(int i=a;i<=b;i++){
sum = sum+ a;
}
System.out.println("Sum of "+a+" to "+ b+" numbers = "+sum);
}
}
}
public class addnumbersusing5threads {
public static void main(String[] args) {
Thread t1 = new Thread(new adding(1,10));
Thread t2 = new Thread(new adding(1,50));
Thread t3 = new Thread(new adding(5,15));
Thread t4 = new Thread(new adding(10,20));
Thread t5 = new Thread(new adding(15,20));
t1.start();
t2.start();
t3.start();
t4.start();
t5.start();
}
}
Output:
输出:
Sum of 1 to 10 numbers = 10
Sum of 1 to 50 numbers = 50
Sum of 5 to 15 numbers = 55
Sum of 10 to 20 numbers = 110
Sum of 15 to 20 numbers = 90
回答by I?ya Bursov
here is the problem:
这是问题所在:
sum = sum + a;
it should be sum += i;
它应该是 sum += i;
BTW, you don't need any synchronization here
顺便说一句,您在这里不需要任何同步
if you want delay between additions - use Thread.sleep(1000L);
如果您想在添加之间延迟 - 使用 Thread.sleep(1000L);
回答by StuartLC
As per Lashane, there is no need to synchronize here - there is no contention on sum
as each thread will have its own variable. (And if you do need to synchronize, don't synchronize on this
as the object reference has scope outside of the class and could e.g. be subject to deadlock - instead, synchronize on a private field object, e.g. private final Object lock = new Object();
)
根据 Lashane,这里不需要同步——没有争用,sum
因为每个线程都有自己的变量。(并且如果您确实需要同步,请不要同步,this
因为对象引用具有类之外的范围并且可能例如受到死锁的影响 - 相反,在私有字段对象上同步,例如private final Object lock = new Object();
)
public void add(int a, int b){
int sum=0;
for(int i=a;i<=b;i++){
sum = sum + i;
Thread.sleep(1000);
}
System.out.println("Sum of "+a+" to "+ b+" numbers = "+sum);
}
Also, after starting threads, you will need to join them back into the main thread.
此外,启动线程后,您需要将它们重新加入主线程。
...
t4.start();
t5.start();
// Join
t1.join();
t2.join();
...
回答by sriman reddy
Add Thread.sleep(1000L)
to your add method
添加Thread.sleep(1000L)
到您的添加方法
public void add(int a, int b) throws InterruptedException {
int sum=0;
synchronized (this) {
for(int i=a;i<=b;i++){
sum += i;
Thread.sleep(1000L); // Add this line for one second delay on each addition
}
System.out.println("Sum of "+a+" to "+ b+" numbers = "+sum);
}
}