java中几种synchronized用法总结

synchronized方法,synchronized静态方法,synchronized(this),synchronized(类),synchronized(变量)之间的区别
先贴代码
/**
 * Created by sunshanpeng on 2016/9/27.
 */
public class ThreadTest {


    public static final String Lock = "lock";


    public final Object obj = new Object();


    synchronized void test1() throws InterruptedException {
        for (int i = 0; i < 5; i++) {
            Thread.sleep(1000);
            System.out.println("test1="+i);
        }
    }


    synchronized static void test2() throws InterruptedException {
        for (int i = 0; i < 5; i++) {
            Thread.sleep(1000);
            System.out.println("test2=" + i);
        }
    }


    void test3() throws InterruptedException {
       synchronized (this) {
           for (int i = 0; i < 5; i++) {
               Thread.sleep(1000);
               System.out.println("test3=" + i);
           }
       }
    }


    void test4() throws InterruptedException {
        synchronized (this.getClass()) {
            for (int i = 0; i < 5; i++) {
                Thread.sleep(1000);
                System.out.println("test4=" + i);
            }
        }
    }


    void test5() throws InterruptedException {
        synchronized (Lock) {
            for (int i = 0; i < 5; i++) {
                Thread.sleep(1000);
                System.out.println("test5=" + i);
            }
        }
    }


    void test6() throws InterruptedException {
        synchronized (obj) {
            for (int i = 0; i < 5; i++) {
                Thread.sleep(1000);
                System.out.println("test6=" + i);
            }
        }
    }
}


一、同一个对象的同一个同步方法是同步的
这个毋庸置疑,不多解释


二、同一个对象的synchronized方法和synchronized(this)块是同步的
方法test1和test3同步
这两个方法的锁都是加在当前对象上


三、类的static方法和synchronized(该类)块是同步的
方法test2和test4同步
这两个方法的锁都是加在ThreadTest类上面,方法test4的synchronized (this.getClass())块换成synchronized (ThreadTest.class),结果是一样的,但是如果换成比如synchronized (String.class)就不能同步了

因为锁是加在类上面,所以不仅相同类的这两种方法是同步的,同一个类的不同对象之间的这两种方法也是同步的


四、第二点的两种方法和第三点的两种方法不同步
test1和test2不同步,test1和test4不同步
test3和test2不同步,test3和test4不同步


五、synchronized(变量)跟前面四种都不同步
因为锁加的目标不一样,既不是当前类也不是当前对象。
synchronized (obj)和synchronized (this)的用法基本一致,对当前对象加锁。
而synchronized (Lock)除了当前对象以外,本类的其他新建对象也会同步加锁,甚至于其他类如果调用的synchronized (ThreadTest.Lock)也会同步。
比如:

class ThreadTest2{
    void test7() throws InterruptedException {
        synchronized (ThreadTest.Lock) {
            for (int i = 0; i < 5; i++) {
                Thread.sleep(1000);
                System.out.println("test7=" + i);
            }
        }
    }
}

ThreadTest2的test7方法跟ThreadTest的test5方法是同步的;
测试代码:

public static void main(String[] args) throws Exception{
        final ThreadTest t1 = new ThreadTest();
        final ThreadTest t2 = new ThreadTest();
        new Thread(){
            public void run() {
                try {
                    t1.test5();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }.start();
        new Thread(){
            public void run() {
                try {
                    new ThreadTest2().test7();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }.start();
    }


语言组织的不是很好,因为网上没有其他相关文献,所以写一篇来抛砖引玉。微笑

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值