Java线程同步操作

synchronized

作用于对象实例:对给定对象加锁,进入同步代码前要获得给定对象的锁。

作用于实例方法:相当于对当前实例加锁,进入同步代码前要获得当前实例的锁。

作用于静态方法:相当于对当前类加锁,进入同步代码前要获得当前类的锁。

使用

给实例对象加锁

public class AccountingSync implements Runnable {
    static AccountingSync instance = new AccountingSync();
    static int i = 0;

    @Override
    public void run() {
        for (int k = 0; k < 10000; k++) {
            synchronized (instance) {
                i++;
            }
        }
    }

    @Test
    public void testInteger() throws InterruptedException {
        int count = 10;
        Thread[] ts = new Thread[count];

        for (int k = 0; k < count; k++) {
            ts[k] = new Thread(instance);
        }

        // start
        for (int k = 0; k < count; k++) {
            ts[k].start();
        }

        // join
        for (int k = 0; k < count; k++) {
            ts[k].join();
        }

        System.out.println(i);
    }
}

给类方法加锁

public class AccountingSync2 implements Runnable {
    static AccountingSync2 instance = new AccountingSync2();
    static int i = 0;

    public synchronized void increase() {
        i++;
    }

    @Override
    public void run() {
        for (int k = 0; k < 10000; k++) {
            increase();
        }
    }

    @Test
    public void testInteger() throws InterruptedException {
        int count = 10;
        Thread[] ts = new Thread[count];

        for (int k = 0; k < count; k++) {
            ts[k] = new Thread(instance);
        }

        // start
        for (int k = 0; k < count; k++) {
            ts[k].start();
        }

        // join
        for (int k = 0; k < count; k++) {
            ts[k].join();
        }

        System.out.println(i);
    }
}

给类方法加锁的错误演示

public class AccountingSyncBad implements Runnable {
    static int i = 0;

    public synchronized void increase() {
        i++;
    }

    @Override
    public void run() {
        for (int k = 0; k < 10000; k++) {
            increase();
        }
    }

    @Test
    public void testInteger() throws InterruptedException {
        int count = 10;
        Thread[] ts = new Thread[count];

        for (int k = 0; k < count; k++) {
            ts[k] = new Thread(new AccountingSyncBad());
        }

        // start
        for (int k = 0; k < count; k++) {
            ts[k].start();
        }

        // join
        for (int k = 0; k < count; k++) {
            ts[k].join();
        }

        System.out.println(i);
    }
}

假设把给类实例加锁中的每个实例比作一个门,上面的测试方法中每个门都有锁但是10个门10把锁,每个线程进一个门。还是不能保证临界区资源i同时只一个线程访问

fix

@Test
public void testIntegerFix() throws InterruptedException {
  int count = 10;
  AccountingSyncBad instance = new AccountingSyncBad();
  Thread[] ts = new Thread[count];

  for (int k = 0; k < count; k++) {
    ts[k] = new Thread(instance);
  }

  // start
  for (int k = 0; k < count; k++) {
    ts[k].start();
  }

  // join
  for (int k = 0; k < count; k++) {
    ts[k].join();
  }

  System.out.println(i);
}

给静态类方法加锁

public class AccountingSyncClass implements Runnable {
    static int i = 0;

    public static synchronized void increase() {
        i++;
    }

    @Override
    public void run() {
        for (int k = 0; k < 10000; k++) {
            increase();
        }
    }

    @Test
    public void testInteger() throws InterruptedException {
        int count = 10;
        Thread[] ts = new Thread[count];

        for (int k = 0; k < count; k++) {
            ts[k] = new Thread(new AccountingSyncClass());
        }

        // start
        for (int k = 0; k < count; k++) {
            ts[k].start();
        }

        // join
        for (int k = 0; k < count; k++) {
            ts[k].join();
        }

        System.out.println(i);
    }
    
    @Test
    public void testIntegerFix() throws InterruptedException {
        int count = 10;
        AccountingSyncClass instance = new AccountingSyncClass();
        Thread[] ts = new Thread[count];

        for (int k = 0; k < count; k++) {
            ts[k] = new Thread(instance);
        }

        // start
        for (int k = 0; k < count; k++) {
            ts[k].start();
        }

        // join
        for (int k = 0; k < count; k++) {
            ts[k].join();
        }

        System.out.println(i);
    }
}

上面测试的testInteger方法和testIntegerFix方法都能得到正确的结果,原因是给静态类方法加锁相当于10个门用的同一把锁,保证了同一时间只有一个线程能访问临界区资源i。

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 从三月份找实习到现在,面了一些公司,挂了不少,但最终还是拿到小米、百度、阿里、京东、新浪、CVTE、乐视家的研发岗...
    时芥蓝阅读 42,498评论 11 349
  • 本文出自 Eddy Wiki ,转载请注明出处:http://eddy.wiki/interview-java.h...
    eddy_wiki阅读 6,655评论 0 14
  • Java8张图 11、字符串不变性 12、equals()方法、hashCode()方法的区别 13、...
    Miley_MOJIE阅读 9,178评论 0 11
  • 这是一篇听课总结。 脑神经最新研究发现,想象的时候大脑经历着相同的过程。梵高想象画布上画的样子,爱因斯坦想象引力波...
    海星_love阅读 1,634评论 0 0
  • 文:蓝玫汐 自古学生分等级,学神者,学霸者,学渣者,学沫者。 眼下,又多了个词,叫“学婊”,专指那些看起来不努力,...
    蓝玫汐阅读 6,734评论 2 1

友情链接更多精彩内容