前言
同一个类内部,静态方法或成员方法上加Sychronized,同时使用是否对调用资源产生互斥。
针对synchronized锁对象的八种案例,进行可能结果的分析。
使用了Slf4j进行日志打印,可用System.out.println();替代
new Thread(n1::a).start();使用了方法引用,可以认为是lambda表达式的语法糖
案例1
@Slf4j(topic = "c.ThreadEightLock")
public class ThreadEightLock {
public static void main(String[] args) {
Number n1 = new Number();
new Thread(n1::a).start();
new Thread(n1::b).start();
}
}
@Slf4j(topic = "c.Number")
class Number{
public synchronized void a() {
log.debug("1");
}
public synchronized void b() {
log.debug("2");
}
}
分析:成员方法上加sychronized,等价于在this上加锁。
n1调用两个方法,锁住的是同一个对象。两个线程调用顺序可能不同,所以结果为12或21。
案例2
@Slf4j(topic = "c.ThreadEightLock")
public class ThreadEightLock {
public static void main(String[] args) {
Number n1 = new Number();
new Thread(n1::a).start();
new Thread(n1::b).start();
}
}
@Slf4j(topic = "c.Number")
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");
}
}
分析: a和b互斥,若先调用a,睡眠过程中b对象不能获得锁,无法执行。
可能结果:睡眠1s 1 2或2 睡眠1s 1。
案例3
@Slf4j(topic = "c.ThreadEightLock")
public class ThreadEightLock {
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();
}
}
@Slf4j(topic = "c.Number")
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");
}
}
分析:
c和(a,b)不是互斥关系
①(a,b)先调用a,sleep过程中,c打印。3 睡眠1s 12
② 调用b,c先执行,b打印,调度a 。3 2 睡眠1s 1
③ 先调用b,b打印,调度a时,c执行。2 3 睡眠1s 1
案例4
@Slf4j(topic = "c.ThreadEightLock")
public class ThreadEightLock {
public static void main(String[] args) {
Number n1 = new Number();
Number n2 = new Number();
new Thread(n1::a).start();
new Thread(n2::b).start();
}
}
@Slf4j(topic = "c.Number")
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
@Slf4j(topic = "c.ThreadEightLock")
class ThreadEightLock{
public static void main(String[] args) {
Number n1 = new Number();
new Thread(() -> n1.a()).start();
new Thread(n1::b).start();
}
}
@Slf4j(topic = "c.Number")
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");
}
}
分析:在static上加锁,相当于在Number.class上加锁,调用this对象同样不构成互斥。
结果为2 睡眠1s 1。
案例6
@Slf4j(topic = "c.ThreadEightLock")
class ThreadEightLock{
public static void main(String[] args) {
Number n1 = new Number();
new Thread(() -> n1.a()).start();
new Thread(() -> n1.b()).start();
}
}
@Slf4j(topic = "c.Number")
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");
}
}
分析:同时对Number.class加锁,产生互斥。结果为:睡眠1s 1 2或2 睡眠1s 1。
案例7
@Slf4j(topic = "c.ThreadEightLock")
class ThreadEightLock{
public static void main(String[] args) {
Number n1 = new Number();
Number n2 = new Number();
new Thread(() -> n1.a()).start();
new Thread(() -> n2.b()).start();
}
}
@Slf4j(topic = "c.Number")
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");
}
}
分析:两个对象,一个对象上锁,一个class上锁,不互斥,结果为2 睡眠1s 1。
案例8
@Slf4j(topic = "c.ThreadEightLock")
class ThreadEightLock{
public static void main(String[] args) {
Number n1 = new Number();
Number n2 = new Number();
new Thread(() -> n1.a()).start();
new Thread(() -> n2.b()).start();
}
}
@Slf4j(topic = "c.Number")
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");
}
}
分析:两个方法均为static上锁,即对class上锁,两个对象属于class类,所以产生互斥。
结果:睡眠1s 1 2或2 睡眠1s 1。