8锁现象

1.synchronized锁的对象是方法的调用者,两个方法用的是同一个锁,谁先拿到谁先执行

2.这先后是锁的问题,那个线程先拿到

public class Test1 {
	
	public static void main(String[] args) {
		Phone phone = new Phone();
		
		new Thread(()->{
			phone.sendSms();
		}).start();
		
		try {
			TimeUnit.SECONDS.sleep(1);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		
		new Thread(()->{
			phone.call();
		}).start();
	}
}
class Phone{
	
	public synchronized void sendSms() {
		System.out.println("sendSms");
	}
	
	public synchronized void call() {
		System.out.println("call");
	}
}

执行结果为
sendSms
call

public class Test1 {
	
	public static void main(String[] args) {
		Phone phone = new Phone();
		
		new Thread(()->{
			phone.sendSms();
		}).start();
		
		try {
			TimeUnit.SECONDS.sleep(1);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		
		new Thread(()->{
			phone.call();
		}).start();
	}
}
class Phone{
	
	public synchronized void sendSms() {
		try {
			TimeUnit.SECONDS.sleep(4);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		System.out.println("sendSms");
	}
	
	public synchronized void call() {
		System.out.println("call");
	}
}

执行结果为
sendSms
call

3.当增加了一个普通方法,不受锁的影响,先执行

public class Test2 {
	
	public static void main(String[] args) {
		Phone2 phone = new Phone2();
		
		new Thread(()->{
			phone.sendSms();
		}).start();
		
		try {
			TimeUnit.SECONDS.sleep(1);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		
		new Thread(()->{
			phone.hello();
		}).start();
	}
}

class Phone2{
	
	public synchronized void sendSms() {
		try {
			TimeUnit.SECONDS.sleep(4);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		System.out.println("sendSms");
	}
	
	public synchronized void call() {
		System.out.println("call");
	}
	
	//这里没有锁 不是同步方法,不受锁的影响
	public void hello() {
		System.out.println("hello");
	}
}

4.多个对象跑不同线程,他们的线程不存在抢夺资源,同时进行(2把锁)

public class Test2 {
	public static void main(String[] args) {
		Phone2 phone = new Phone2();
		Phone2 phone2 = new Phone2();
		
		new Thread(()->{
			phone.sendSms();
		}).start();
		
		try {
			TimeUnit.SECONDS.sleep(1);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		
		new Thread(()->{
			phone2.call();
		}).start();
	}
}

class Phone2{
	public synchronized void sendSms() {
		try {
			TimeUnit.SECONDS.sleep(1);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		System.out.println("sendSms");
	}
	
	public synchronized void call() {
		System.out.println("call");
	}
	
	//这里没有锁 不是同步方法,不受锁的影响
	public void hello() {
		System.out.println("hello");
	}
}

5.增加个静态同步方法 static 静态方法(锁的是Class,全局唯一)

public class Test3 {
	
	public static void main(String[] args) {
		Phone3 phone = new Phone3();
		
		new Thread(()->{
			phone.sendSms();
		}).start();
		
		try {
			TimeUnit.SECONDS.sleep(1);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		
		new Thread(()->{
			phone.call();
		}).start();
	}
}

class Phone3{
	
	public static synchronized void sendSms() {
		try {
			TimeUnit.SECONDS.sleep(1);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		System.out.println("sendSms");
	}
	
	public static synchronized void call() {
		System.out.println("call");
	}
	
}

6.两个对象! 两个静态同步方法,两个对象的Class模板只有一个,static锁的是Class

public class Test3 {
	
	public static void main(String[] args) {
		Phone3 phone = new Phone3();
		Phone3 phone2 = new Phone3();
		
		new Thread(()->{
			phone.sendSms();
		}).start();
		
		try {
			TimeUnit.SECONDS.sleep(1);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		
		new Thread(()->{
			phone2.call();
		}).start();
	}
}

class Phone3{
	
	public static synchronized void sendSms() {
		try {
			TimeUnit.SECONDS.sleep(1);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		System.out.println("sendSms");
	}
	
	public static synchronized void call() {
		System.out.println("call");
	}
	
}

7. 一个静态同步方法(锁的是Class模板),一个普通同步方法(锁的是Phone对象),一个对象。所以同步执行

package JUC;

import java.util.concurrent.TimeUnit;

/**   
 * Copyright © 2021 eSunny Info. Tech Ltd. All rights reserved.
 * 
 * 功能描述:
 * @Package: JUC 
 * @author: 79283   
 * @date: 2021年1月21日 下午1:58:47 
 */
public class Test4 {
	
	public static void main(String[] args) {
		Phone4 phone = new Phone4();
		
		new Thread(()->{
			phone.sendSms();
		}).start();
		
		try {
			TimeUnit.SECONDS.sleep(1);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		
		new Thread(()->{
			phone.call();
		}).start();
	}
}

class Phone4{
	
	public static synchronized void sendSms() {
		try {
			TimeUnit.SECONDS.sleep(3);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		System.out.println("sendSms");
	}
	
	public synchronized void call() {
		System.out.println("call");
	}
	
}

8.一个静态同步方法(锁的是Class模板),一个普通同步方法(锁的是Phone对象),二个对象。

package JUC;

import java.util.concurrent.TimeUnit;

/**   
 * Copyright © 2021 eSunny Info. Tech Ltd. All rights reserved.
 * 
 * 功能描述:
 * @Package: JUC 
 * @author: 79283   
 * @date: 2021年1月21日 下午1:58:47 
 */
public class Test4 {
	
	public static void main(String[] args) {
		Phone4 phone = new Phone4();
		Phone4 phone2 = new Phone4();
		new Thread(()->{
			phone.sendSms();
		}).start();
		
		try {
			TimeUnit.SECONDS.sleep(1);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		
		new Thread(()->{
			phone2.call();
		}).start();
	}
}

class Phone4{
	
	public static synchronized void sendSms() {
		try {
			TimeUnit.SECONDS.sleep(1);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		System.out.println("sendSms");
	}
	
	public synchronized void call() {
		System.out.println("call");
	}
	
}

总结一下

new (this具体的一个对象)
static (Class唯一的类模板)

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一只小小狗

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值