synchronized 静态方法和非静态方法

xiaoxiao2021-02-28  98

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(); } }
转载请注明原文地址: https://www.6miu.com/read-62381.html

最新回复(0)