多线程 1加到100 开10个线程 每个实现十个相加

package com.myh.kb13.t804.service;

/**
 * @Description:
 * @author:myh
 * @date: 2021/8/4  17:11
 */
public class MyAddMute implements Runnable{
    private static int numbers=100;
    @Override
    public void run() {
        int tn=Integer.parseInt(Thread.currentThread().getName());
        if(tn%2==0){
            add();
        }else {
            mute();
        }
    }
    //同一时间只有一个线程干活 的代码

    public synchronized void add(){

        numbers++;
        System.out.println("当前线程:"+Thread.currentThread().getName()+",加完结果是:"+numbers);

    }
    public synchronized void mute(){

        numbers--;
        System.out.println("当前线程:"+Thread.currentThread().getName()+",减完结果是:"+numbers);

    }

    public static void main(String[] args) {
        MyAddMute mau = new MyAddMute();
        for (int i = 0; i < 4; i++) {
            Thread th=new Thread(mau);
            th.setName(i+"");
            th.start();
        }
    }
}
package com.myh.kb13.t804.service;

/**
 * @Description:
 * @author:myh
 * @date: 2021/8/4  15:56
 */
public class MyTest {
    public static void main(String[] args) {
//        OneToHun oth=new OneToHun();

        OneToHun2 oth=new OneToHun2();
        for (int i = 1; i <= 100; i += 10) {
            Thread thread = new Thread(oth);
            thread.setName(i + "");
            thread.start();
        }
    }
}
package com.myh.kb13.t804.service;

/**
 * @Description:
 * @author:myh
 * @date: 2021/8/4  15:50
 */
public class Nums {
    int number;

    public int getNumber() {
        return number;
    }

    public void setNumber(int number) {
        this.number = number;
    }

    public void add(int n){
        number+=n;
    }
}
package com.myh.kb13.t804.service;

/**
 * @Description:
 * @author:myh
 * @date: 2021/8/4  15:52
 */
public class OneToHun implements Runnable{
    Nums num=new Nums();

    @Override
    public void run() {
        int threadName= Integer.valueOf(Thread.currentThread().getName());
        for (int i = threadName; i < threadName + 10; i++) {
            synchronized (num){
                num.add(i);
                System.out.println("当前线程为:"+threadName+",加入"+i+"的值后为:"+num.getNumber());
            }
        }
    }
}
package com.myh.kb13.t804.service;

/**
 * @Description:
 * @author:myh
 * @date: 2021/8/4  16:18
 */
public class OneToHun2 implements Runnable{

    private static int numbers;
    @Override
    public void run() {
        int num=0;
        int thn= Integer.parseInt(Thread.currentThread().getName());//线程名 1 11 21 31
        for (int i = thn; i < thn + 10; i++) {
            num+=i;
        }

        add(num);

    }
    public synchronized void add(int num){
        numbers+=num;
        System.out.println("当前线程是:"+Thread.currentThread().getName()+"循环10此后相加结果为:"+numbers);
    }
}

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值