7-2 试试多线程

编写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();可以删除。额。。。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值