zoukankan      html  css  js  c++  java
  • java 锁Integer等对象失败

    在学习并发编程时,多个线程对同一个Integer对象进行操作时,我想当然的直接对这个Integer对象加锁。然而最终结果让我百思不得其解,加锁无效。

    public class TestJoin {
        static A testA = new A();
        public volatile static Integer t = 0;
    
        public static void main(String[] args) throws InterruptedException {
            Thread[] threads = new Thread[10];
            for (int j = 0; j < 10; j++) {
                threads[j] = new Thread(testA);
                threads[j].start();
            }
            for (int j = 0; j < 10; j++) {
                threads[j].join();
            }
            System.out.println(t);
        }
    
        static class A implements Runnable {
            @Override
            public void run() {
                for (int f = 0; f < 2000; f++) {
                    synchronized (t) {
                        t++;
                    }
                }
            }
        }
    
    }
    

      最终输出为:

    并不是所预期的20000.

    原因:java的自动封箱和解箱操作在作怪。这里的t++实际上是i = new Integer(t+1),所以执行完t++后,t已经不是原来的对象了,同步块自然就无效了

    解决方法:
    synchronized(t)变为synchronized(testA)即可
    
    
  • 相关阅读:
    OO第四次总结
    OO第三次总结
    C语言函数指针
    Java对象集合
    emacs下最牛逼的Markdown编辑方式
    OO第二次总结
    Git复习
    Java设计原则
    多线程学习笔记1
    OO第一次总结
  • 原文地址:https://www.cnblogs.com/DoJavaByHeart/p/8515020.html
Copyright © 2011-2022 走看看