java中几种synchronized用法总结

来源:互联网 发布:网络机房整改方案 编辑:程序博客网 时间:2024/06/05 00:33

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();    }


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

0 0
原创粉丝点击