java多线程问题(代码示例)

前言:
实际开发中光听说多线程问题,多线程问题,到底啥是多线程问题?
解释:
多线程问题是指:多个线程(2个及以上)在长循环中同时操作同一个变量(可延伸为内存空间),会导致变量错乱问题(可能不是我们想要的结果)
mThread.join();等待线程执行完毕

final int[] num = new int[1];
        Thread mThread = new Thread() {
            @Override
            public void run() {
                for (int i = 0; i < 1000; i++) {
                    num[0]++;
                }
                System.out.println("+num[0]: " + num[0]);
            }
        };

        Thread mThread1 = new Thread() {
            @Override
            public void run() {
                for (int i = 0; i < 1000; i++) {
                    num[0]--;
                }
                System.out.println("-num[0]: " + num[0]);
            }
        };

        mThread.start();
        mThread1.start();
        try {
            mThread.join();
            mThread1.join();
        } catch (InterruptedException mE) {
            mE.printStackTrace();
        }
        System.out.println("final num[0]: " + num[0]);

现象:
循环次数是1000,最终结果是0;当是10000时,最终结果不再是0

2021-03-23 16:42:10.179 6240-6256/com.wintec.myapplication I/System.out: +num[0]: 1000
2021-03-23 16:42:10.179 6240-6257/com.wintec.myapplication I/System.out: -num[0]: 0
2021-03-23 16:42:10.179 6240-6240/com.wintec.myapplication I/System.out: final num[0]: 0

10000次日志

2021-03-23 17:02:15.589 6419-6435/com.wintec.myapplication I/System.out: +num[0]: -5112
2021-03-23 17:02:15.589 6419-6436/com.wintec.myapplication I/System.out: -num[0]: -5323
2021-03-23 17:02:15.589 6419-6419/com.wintec.myapplication I/System.out: final num[0]: -5323

此时,便是多线程问题
怎么解决?
使用syncronized关键字给容易出现线程切换的地方加锁[修改共享变量的线程代码块]
num[0]++;实际jvm是进行了3次操作,ILOAD IADD ISTORE,容易出现线程切换

  final int[] num = new int[1];
        Thread mThread = new Thread() {
            @Override
            public void run() {
                for (int i = 0; i < 10000; i++) {
                    synchronized (UserLoginActivity.this){
                        num[0]++;
                    }

                }
                System.out.println("+num[0]: " + num[0]);
            }
        };

        Thread mThread1 = new Thread() {
            @Override
            public void run() {
                for (int i = 0; i < 10000; i++) {
                    synchronized (UserLoginActivity.this){
                        num[0]--;
                    }

                }
                System.out.println("-num[0]: " + num[0]);
            }
        };

        mThread.start();
        mThread1.start();
        try {
            mThread.join();
            mThread1.join();
        } catch (InterruptedException mE) {
            mE.printStackTrace();
        }
        System.out.println("final num[0]: " + num[0]);
2021-03-23 17:17:35.654 7008-7027/com.wintec.myapplication I/System.out: -num[0]: -11
2021-03-23 17:17:35.655 7008-7026/com.wintec.myapplication I/System.out: +num[0]: 0
2021-03-23 17:17:35.657 7008-7008/com.wintec.myapplication I/System.out: final num[0]: 0
  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值