知识前提:
synchronized是可重入锁
一个类有两种锁,Class锁和对象锁,static方法使用的是Class锁,并且是所有static方法唯一的一把锁。对象锁是每new出来一个对象就会产生一个锁,并且这个对象的所有非静态方法共用这一个锁,不同方法的非静态方法使用的锁不是同一把锁。
验证代码:
package com.gui.thread;
import java.util.concurrent.TimeUnit;
/**
* ParentAndSonSynchronized
*
* @author guigu
* @QQ 1170258867
* @date 2020/5/11
* @description 验证父类和子类是否为同一把锁
* 难点,Synchronized是可重入锁
*/
public class ParentAndSonSynchronized {
public static void main(String[] args) {
Son son = new Son();
new Thread(()->{
try {
son.test1();
} catch (InterruptedException e) {
e.printStackTrace();
}
}).start();
new Thread(()->{
try {
son.test2();
} catch (InterruptedException e) {
e.printStackTrace();
}
}).start();
}
}
class Parent{
public synchronized void test1() throws InterruptedException {
System.out.println("这个是父类方法,抱着锁睡");
TimeUnit.SECONDS.sleep(5);
System.out.println("父类睡醒了");
}
public synchronized void test2() throws InterruptedException {
System.out.println("用于子类继承");
}
}
class Son extends Parent{
@Override
public synchronized void test2() throws InterruptedException {
super.test2();
TimeUnit.SECONDS.sleep(2);
System.out.println("子类方法运行了");
}
}
还没完:
对于静态方法,谁的锁就是谁的锁,就是Class锁不共用
package com.gui.thread;
import java.util.concurrent.TimeUnit;
/**
* ParentAndSonSynchronized
*
* @author guigu
* @QQ 1170258867
* @date 2020/5/11
* @description 验证父类和子类是否为同一把锁
* 难点,Synchronized是可重入锁
*/
public class ParentAndSonSynchronizedUseStatic {
public static void main(String[] args) {
new Thread(()->{
try {
Son1.test1();
} catch (InterruptedException e) {
e.printStackTrace();
}
}).start();
new Thread(()->{
try {
Son1.test2();
} catch (InterruptedException e) {
e.printStackTrace();
}
}).start();
}
}
class Parent1{
public static synchronized void test1() throws InterruptedException {
System.out.println("这个是父类方法,抱着锁睡");
TimeUnit.SECONDS.sleep(5);
System.out.println("父类睡醒了");
}
public static synchronized void test2() throws InterruptedException {
System.out.println("用于子类继承");
}
}
class Son1 extends Parent1{
// public static synchronized void test1() throws InterruptedException {
// TimeUnit.SECONDS.sleep(2);
// System.out.println("子类方法运行了");
// }
public static synchronized void test2() throws InterruptedException {
TimeUnit.SECONDS.sleep(2);
System.out.println("子类方法运行了");
}
}
package com.gui.thread;
import java.util.concurrent.TimeUnit;
/**
* ParentAndSonSynchronized
*
* @author guigu
* @QQ 1170258867
* @date 2020/5/11
* @description 验证父类和子类是否为同一把锁
* 难点,Synchronized是可重入锁
*/
public class ParentAndSonSynchronizedUseStatic {
public static void main(String[] args) {
new Thread(()->{
try {
Parent1.test1();
} catch (InterruptedException e) {
e.printStackTrace();
}
}).start();
new Thread(()->{
try {
Son1.test1();
} catch (InterruptedException e) {
e.printStackTrace();
}
}).start();
}
}
class Parent1{
public static synchronized void test1() throws InterruptedException {
System.out.println("这个是父类方法,抱着锁睡");
TimeUnit.SECONDS.sleep(5);
System.out.println("父类睡醒了");
}
public static synchronized void test2() throws InterruptedException {
System.out.println("用于子类继承");
}
}
class Son1 extends Parent1{
public static synchronized void test1() throws InterruptedException {
TimeUnit.SECONDS.sleep(2);
System.out.println("子类方法运行了");
}
public static synchronized void test2() throws InterruptedException {
TimeUnit.SECONDS.sleep(2);
System.out.println("子类方法运行了");
}
}