java面试之线程八锁

情况1:12 或 21

锁的是this对象

@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();

情况2:1s后12,或 2 1s后 1

情况3:3 1s 12 或 23 1s 1 或 32 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();
 	new Thread(()->{ n1.a(); }).start();
	new Thread(()->{ n1.b(); }).start();
}

情况3:3 1s 12 或 23 1s 1 或 32 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 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();
}

假如时间片分给1,1需要等待1秒,然后3没有锁,3就执行了,所以1不会在3的前面

情况4:2 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();

线程1锁的是n1对象,线程2锁的是n2对象,不互斥

情况5:2 1s 后 1

@Slf4j(topic = "c.Number")
class Number{
	public static synchronized void a() { // 锁的是Number类的类对象
		sleep(1);
		log.debug("1");
 	}
	public synchronized void b() { // 锁的是this对象
 		log.debug("2");
 	}
 }
public static void main(String[] args) {
	Number n1 = new Number();
	new Thread(()->{ n1.a(); }).start();
 	new Thread(()->{ n1.b(); }).start();
}

情况6:1s 后12, 或 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();
}

都是对类对象加锁,类对象整个内存里只有一份,所以是互斥的

情况7: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();
}

线程1锁的是类对象,线程2锁的是this对象,不是互斥的

情况8:1s 后12, 或 2 1s后 1

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();
}

锁的是类对象,是互斥的

  • 5
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值