方法上的锁判断之线程八锁

线程八锁

@Slf4j(topic = "c.Number")
class Number{
 public synchronized void a() {
 log.debug("1");
 }
 public synchronized void b() {
 log.debug("2");
 }
}
public static void main(String[] args) {
 Number n1 = new Number();
 new Thread(()->{ n1.a(); }).start();
 new Thread(()->{ n1.b(); }).start();
}

结果

上面 Number 的两个方法,synchronized,都在一般方法上,所以锁住的是 对象,
也就是锁住的是n1 这个对象,所以当无论时锁住 a 方法还是 b方法,另一个线程都是阻塞状态
结果为 12 或 21
@Slf4j(topic = "c.Number")
class Number{
 public synchronized void a() {
 	sleep(1);
 	log.debug("1");
 }
 public synchronized void b() {
 	log.debug("2");
 }
}
public static void main(String[] args) {
 Number n1 = new Number();
 new Thread(()->{ n1.a(); }).start();
 new Thread(()->{ n1.b(); }).start();
}
情况和上一个相同 但是存在一个sleep
结果为 1s 12 或者 2 1s 1
class Number{
 public synchronized void a() {
 sleep(1);
 log.debug("1");
 }
 public synchronized void b() {
 log.debug("2");
 }
 public void c() {
 log.debug("3");
 }
}
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();
}
因为 方法c 没有加锁,所以 方法c 和其他两个方法是并行关系
如果 a 先获得锁,那么先等一秒,此时 c方法会执行 所以结果为 3 1s 12
如果 b 先获得锁,那么 有两种情况 就是32 1s 1 或者 23 1s 1
@Slf4j(topic = "c.Number")
class Number{
 public synchronized void a() {
 sleep(1);
 log.debug("1");
 }
 public synchronized void b() {
 log.debug("2");
 }
}
public static void main(String[] args) {
 Number n1 = new Number();
 Number n2 = new Number();
 new Thread(()->{ n1.a(); }).start();
 new Thread(()->{ n2.b(); }).start();
}
因为这里是 一般方法上的锁锁的是 对象,所以 a的锁锁的是 n1, b 的锁锁的是 n2
两个线程的运行不受对方的锁的控制
又因为 方法 a,睡眠一秒
所以,结果为2 1s后 1
class Number{
 public static synchronized void a() {
     sleep(1);
     log.debug("1");
 }
 public synchronized void b() {
     log.debug("2");
     }
 }
public static void main(String[] args) {
 Number n1 = new Number();
 new Thread(()->{ n1.a(); }).start();
 new Thread(()->{ n1.b(); }).start();
}
上面方法 a 是静态方法,所以锁住的是类,
方法b 是一般方法,锁住的是 对象
所以两个方法的锁并不能干扰到对方
结果为 2 1s 后 1
@Slf4j(topic = "c.Number")
class Number{
 public static synchronized void a() {
 sleep(1);
 log.debug("1");
 }
 public static synchronized void b() {
 log.debug("2");
 }
}
public static void main(String[] args) {
 Number n1 = new Number();
 new Thread(()->{ n1.a(); }).start();
 new Thread(()->{ n1.b(); }).start();
}
因为两个方法锁住的都是类对象,锁住的是同一个对象
结果为
1s 后12, 或 2 1s后 1
@Slf4j(topic = "c.Number")
class Number{
 public static synchronized void a() {
 sleep(1);
 log.debug("1");
 }
 public synchronized void b() {
 log.debug("2");
 }
}
public static void main(String[] args) {
 Number n1 = new Number();
 Number n2 = new Number();
 new Thread(()->{ n1.a(); }).start();
 new Thread(()->{ n2.b(); }).start();
}
上面a 方法锁住的是类对象,b方法锁住的是一般对象,两个方法锁住的对象不同,所以两个方法线程并行
    结果为 :2 1s 后 1,因为sleep a后打印
class Number{
 public static synchronized void a() {
 sleep(1);
 log.debug("1");
 }
 public static synchronized void b() {
 log.debug("2");
 }
}
public static void main(String[] args) {
 Number n1 = new Number();
 Number n2 = new Number();
 new Thread(()->{ n1.a(); }).start();
 new Thread(()->{ n2.b(); }).start();
}
虽然对象不同但是两个方法锁住的是类对象,也就是说所著的是同一个,
那么根据获得锁的先后 结果不同
1s 后12, 或 2 1s后 1
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值