编写4个线程,第一个线程从1加到25,第二个线程从26加到50,第三个线程从51加到75,第四个线程从76加到100,最后再把四个线程计算的结果相加。
输入格式:
无
输出格式:
最终结果
输入样例:
输出样例:
5050
代码长度限制
16 KB
时间限制
1000 ms
内存限制
代码实现如下:
public class Main{
public static void main(String[] args) throws InterruptedException {
xianchen o1=new xianchen(1,25);
xianchen o2=new xianchen(26,50);
xianchen o3=new xianchen(51,75);
xianchen o4=new xianchen(76,100);
Thread thread1=new Thread(o1);
Thread thread2=new Thread(o2);
Thread thread3=new Thread(o3);
Thread thread4=new Thread(o4);
thread1.start();
thread2.start();
thread3.start();
thread4.start();
System.out.println(xianchen.sum);
}
}
class xianchen implements Runnable{
static int sum=0;
@Override
public void run() {
sum();
}
public xianchen(int a,int b){
for(;a<=b;a++)
{
sum+=a;
}
}
public int sum(){
return sum;
}
}
有问题.........
thread1.start(); thread2.start(); thread3.start(); thread4.start();可以删除。额。。。
435

被折叠的 条评论
为什么被折叠?



