考察内容:synchronized 锁住的是哪个对象
本质上考察 synchronized 与 static synchronized 的区别:
- synchronized 是实例锁,对类的当前实例进行加锁
- static synchronized 是类锁,对类对象进行加锁
情况1
public class Test1 {
public static void main(String[] args) {
Number n1 = new Number();
new Thread(() -> n1.a()).start();
new Thread(() -> n1.b()).start();
}
}
class Number {
public synchronized void a() {
log.debug("1");
}
public synchronized void b() {
log.debug("2");
}
}
两种可能
- 12
- 21
情况2
public class Test2 {
public static void main(String[] args) {
Number n1 = new Number();
new Thread(() -> n1.a()).start();
new Thread(() -> n1.b()).start();
}
}
class Number{
public synchronized void a(){
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
log.debug("1");
}
public synchronized void b() {
log.debug("2");
}
}
两种可能
- 1s 后打印 12
- 先打印2, 1s 后打印 1
情况3
public class Test3 {
public static void main(String[] args) {
Number n1 = new Number();
new Thread(() -> n1.a()).start();
new Thread(() -> n1.b()).start();
new Thread(() -> n1.c()).start();
}
}
class Number{
public synchronized void a() {
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
log.debug("1");
}
public synchronized void b() {
log.debug("2");
}
public void c() {
log.debug("3");
}
}
三种可能
- 先打印 3,1s 后打印 12
- 先打印 23,1s 后打印 1
- 先打印 32,1s 后打印 1
情况4
public class Test4 {
public static void main(String[] args) {
Number n1 = new Number();
Number n2 = new Number();
new Thread(() -> n1.a()).start();
new Thread(() -> n2.b()).start();
}
}
class Number{
public synchronized void a() {
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
log.debug("1");
}
public synchronized void b() {
log.debug("2");
}
}
一种可能,先打印 2,1s 后打印 1
情况5
public class Test5 {
public static void main(String[] args) {
Number n1 = new Number();
new Thread(() -> n1.a()).start();
new Thread(() -> n1.b()).start();
}
}
class Number {
public static synchronized void a() {
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
log.debug("1");
}
public synchronized void b() {
log.debug("2");
}
}
一种可能,先打印 2,1s 后打印 1
情况6
public class Test6 {
public static void main(String[] args) {
Number n1 = new Number();
new Thread(() -> n1.a()).start();
new Thread(() -> n1.b()).start();
}
}
class Number{
public static synchronized void a() {
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
log.debug("1");
}
public static synchronized void b() {
log.debug("2");
}
}
两种可能
- 1s 后打印 12
- 先打印 2,1s 后打印 1
情况7
public class Test7 {
public static void main(String[] args) {
Number n1 = new Number();
Number n2 = new Number();
new Thread(() -> n1.a()).start();
new Thread(() -> n2.b()).start();
}
}
class Number{
public static synchronized void a() {
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
log.debug("1");
}
public synchronized void b() {
log.debug("2");
}
}
一种可能,先打印 2,1s 后打印 1
情况8
public class Test8 {
public static void main(String[] args) {
Number n1 = new Number();
Number n2 = new Number();
new Thread(() -> n1.a()).start();
new Thread(() -> n2.b()).start();
}
}
class Number{
public static synchronized void a() {
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
log.debug("1");
}
public static synchronized void b() {
log.debug("2");
}
}
两种可能
- 1s 后打印 12
- 先打印 2,1s 后打印 1