synchronized 静态方法和非静态方法

来源:互联网 发布:linux cp 文件夹 编辑:程序博客网 时间:2024/05/12 14:40

synchronized 的本质就是一个不可重入锁,大致分为以下两类
1. 有对象锁 synchronized(this),或者非静态方法上加synchronized修饰
2. 类锁 synchronized(XX.class),或者静态方法上加synchronized修饰

所以method1和method2的开始时间是同时的,因为method1的锁对应的是类锁,而method2对应的是当前对象的锁,所以method3和method2是有严格的顺序关系

测试synchronized添加在方法上

package com.wuhulala.javase.concurrent.synchronize;import java.util.concurrent.TimeUnit;/** * author: wuhulala * date: 2017/8/6 * version: 1.0 * description: 在普通方法添加不可重入锁 *   */public class CommonMethodSynchronized {    public synchronized static void method1(){        System.out.println(Thread.currentThread().getName() + ":::method1...start");        try {            TimeUnit.SECONDS.sleep(4);        } catch (InterruptedException e) {            e.printStackTrace();        }        System.out.println(Thread.currentThread().getName() + ":::method1...end");    }    public synchronized void method2(){        System.out.println(Thread.currentThread().getName() + ":::method2...start");        try {            TimeUnit.SECONDS.sleep(4);        } catch (InterruptedException e) {            e.printStackTrace();        }        System.out.println(Thread.currentThread().getName() + ":::method2...end");    }    public synchronized void method3(){        System.out.println(Thread.currentThread().getName() + ":::method3...start");        try {            TimeUnit.SECONDS.sleep(4);        } catch (InterruptedException e) {            e.printStackTrace();        }        System.out.println(Thread.currentThread().getName() + ":::method3...end");    }}
public class SynchronizedStaticTest{    public static void main(String[] args) {        CommonMethodSynchronized test = new CommonMethodSynchronized();        CommonMethodTest1 thread1 = new CommonMethodTest1(test);        CommonMethodTest2 thread2 = new CommonMethodTest2(test);        CommonMethodTest3 thread3 = new CommonMethodTest3(test);        thread1.start();        thread2.start();        thread3.start();    }}class CommonMethodTest1 extends Thread{    private CommonMethodSynchronized commonMethodSynchronized;    public CommonMethodTest1(CommonMethodSynchronized commonMethodSynchronized) {        this.commonMethodSynchronized = commonMethodSynchronized;    }    @Override    public void run() {        CommonMethodSynchronized.method1();    }}class CommonMethodTest2 extends Thread{    private CommonMethodSynchronized commonMethodSynchronized;    public CommonMethodTest2(CommonMethodSynchronized commonMethodSynchronized) {        this.commonMethodSynchronized = commonMethodSynchronized;    }    @Override    public void run() {        commonMethodSynchronized.method2();    }}class CommonMethodTest3 extends Thread{    private CommonMethodSynchronized commonMethodSynchronized;    public CommonMethodTest3(CommonMethodSynchronized commonMethodSynchronized) {        this.commonMethodSynchronized = commonMethodSynchronized;    }    @Override    public void run() {        commonMethodSynchronized.method3();    }

测试synchronized代码块

package com.wuhulala.javase.concurrent.synchronize;import java.util.concurrent.TimeUnit;/** * author: wuhulala * date: 2017/8/6 * version: 1.0 * description: 测试synchronized代码块 */public class ClassMethodSynchronized {    public static void main(String[] args) {        ClassMethodSynchronized test = new ClassMethodSynchronized();        ClassTest1 thread1 = new ClassTest1(test);        ClassTest2 thread2 = new ClassTest2(test);        ClassTest3 thread3 = new ClassTest3(test);        thread1.start();        thread2.start();        thread3.start();    }    public void method1(){        synchronized (this.getClass()){            System.out.println(Thread.currentThread().getName() + ":::method1...start");            try {                TimeUnit.SECONDS.sleep(4);            } catch (InterruptedException e) {                e.printStackTrace();            }            System.out.println(Thread.currentThread().getName() + ":::method1...end");        }    }    public void method2(){        synchronized (this.getClass()){            System.out.println(Thread.currentThread().getName() + ":::method2...start");            try {                TimeUnit.SECONDS.sleep(4);            } catch (InterruptedException e) {                e.printStackTrace();            }            System.out.println(Thread.currentThread().getName() + ":::method2...end");        }    }    public void method3(){        synchronized (this){            System.out.println(Thread.currentThread().getName() + ":::method3...start");            try {                TimeUnit.SECONDS.sleep(4);            } catch (InterruptedException e) {                e.printStackTrace();            }            System.out.println(Thread.currentThread().getName() + ":::method3...end");        }    }}class ClassTest1 extends Thread{    private ClassMethodSynchronized classMethodSynchronized;    public ClassTest1(ClassMethodSynchronized classMethodSynchronized) {        this.classMethodSynchronized = classMethodSynchronized;    }    @Override    public void run() {        classMethodSynchronized.method1();    }}class ClassTest2 extends Thread{    private ClassMethodSynchronized classMethodSynchronized;    public ClassTest2(ClassMethodSynchronized classMethodSynchronized) {        this.classMethodSynchronized = classMethodSynchronized;    }    @Override    public void run() {        classMethodSynchronized.method2();    }}class ClassTest3 extends Thread{    private ClassMethodSynchronized classMethodSynchronized;    public ClassTest3(ClassMethodSynchronized classMethodSynchronized) {        this.classMethodSynchronized = classMethodSynchronized;    }    @Override    public void run() {        classMethodSynchronized.method3();    }}
原创粉丝点击