概述
1标准访问,请问先打印邮件还是短信? 邮件
class Phone{
public synchronized void sendEmail(){
System.out.println("==========sendEmail");
}
public synchronized void sendMessage(){
System.out.println("======sendMessage");
}
}
public class LockDemo {
public static void main(String[] args) {
Phone phone = new Phone();
new Thread(()->{
phone.sendEmail();
}, "t1").start();
try { TimeUnit.SECONDS.sleep(1); } catch (InterruptedException e) {e.printStackTrace(); }
new Thread(()->{
phone.sendMessage();
}, "t2").start();
}
}
2邮件方法暂停4秒钟,请问先打印邮件还是短信? 邮件
class Phone{
public synchronized void sendEmail(){
try { TimeUnit.SECONDS.sleep(4); } catch (InterruptedException e) {e.printStackTrace(); }
System.out.println("==========sendEmail");
}
public
synchronized void sendMessage(){
System.out.println("======sendMessage");
}
}
public class LockDemo {
public static void main(String[] args) {
Phone phone = new Phone();
new Thread(()->{
phone.sendEmail();
}, "t1").start();
try { TimeUnit.SECONDS.sleep(1); } catch (InterruptedException e) {e.printStackTrace(); }
new Thread(()->{
phone.sendMessage();
}, "t2").start();
try { TimeUnit.SECONDS.sleep(1); } catch (InterruptedException e) {e.printStackTrace(); }
}
}
总结:
对象锁
一个对象里面如果有多个synchronized方法,某一个时刻内,只要一个线程去调用其中的一个synchronized方法了,
其他的线程都只能等待,换句话说,某一个时刻内,只能有唯一一个线程去访问这些synchronized方法,
锁的是当前对象this,被锁定后,其他的线程都不能进入到当前对象的其他的synchronized方法
3新增一个普通方法hello(),请问先打印邮件还是hello? hello
class Phone{
public synchronized void sendEmail(){
try { TimeUnit.SECONDS.sleep(4); } catch (InterruptedException e) {e.printStackTrace(); }
System.out.println("==========sendEmail");
}
public
synchronized void sendMessage(){
System.out.println("======sendMessage");
}
public void hello(){
System.out.println("say hello");
}
}
public class LockDemo {
public static void main(String[] args) {
Phone phone = new Phone();
new Thread(()->{
phone.sendEmail();
}, "t1").start();
try { TimeUnit.SECONDS.sleep(1); } catch (InterruptedException e) {e.printStackTrace(); }
new Thread(()->{
phone.sendMessage();
}, "t2").start();
try { TimeUnit.SECONDS.sleep(1); } catch (InterruptedException e) {e.printStackTrace(); }
new Thread(()->{
phone.hello();
}, "t3").start();
}
}
hello没有synchronized不会受到当前对象锁的控制 不涉及到锁对象的控制和限制 直接运行
加个普通方法后发现和同步锁无关
4两部手机,请问先打印邮件还是短信? 短信
class Phone{
public synchronized void sendEmail(){
try { TimeUnit.SECONDS.sleep(4); } catch (InterruptedException e) {e.printStackTrace(); }
System.out.println("==========sendEmail");
}
public
synchronized void sendMessage(){
System.out.println("======sendMessage");
}
public void hello(){
System.out.println("say hello");
}
}
public class LockDemo {
public static void main(String[] args) {
Phone phone = new Phone();
Phone phone2 = new Phone();
new Thread(()->{
phone.sendEmail();
}, "t1").start();
try { TimeUnit.SECONDS.sleep(1); } catch (InterruptedException e) {e.printStackTrace(); }
new Thread(()->{
phone2.sendMessage();
}, "t2").start();
try { TimeUnit.SECONDS.sleep(1); } catch (InterruptedException e) {e.printStackTrace(); }
}
}
两个线程不是同一把对象锁 各走各的
5加关键字static 两个静态同步方法,同一部手机,请问先打印邮件还是短信?邮件
class Phone{
public static synchronized void sendEmail(){
try { TimeUnit.SECONDS.sleep(4); } catch (InterruptedException e) {e.printStackTrace(); }
System.out.println("==========sendEmail");
}
public static
synchronized void sendMessage(){
System.out.println("======sendMessage");
}
/*
public void hello(){
System.out.println("say hello");
}*/
}
public class LockDemo {
public static void main(String[] args) {
Phone phone = new Phone();
new Thread(()->{
phone.sendEmail();
}, "t1").start();
try { TimeUnit.SECONDS.sleep(1); } catch (InterruptedException e) {e.printStackTrace(); }
new Thread(()->{
phone.sendMessage();
}, "t2").start();
try { TimeUnit.SECONDS.sleep(1); } catch (InterruptedException e) {e.printStackTrace(); }
}
}
同一部手机,同一个锁对象,锁对象都是 类名.class文件 Phone.class
6两个静态同步方法,2部手机,请问先打印邮件还是短信?邮件
class Phone{
public static synchronized void sendEmail(){
try { TimeUnit.SECONDS.sleep(4); } catch (InterruptedException e) {e.printStackTrace(); }
System.out.println("==========sendEmail");
}
public static
synchronized void sendMessage(){
System.out.println("======sendMessage");
}
/*
public void hello(){
System.out.println("say hello");
}*/
}
public class LockDemo {
public static void main(String[] args) {
Phone phone = new Phone();
Phone phone2 = new Phone();
new Thread(()->{
phone.sendEmail();
}, "t1").start();
try { TimeUnit.SECONDS.sleep(1); } catch (InterruptedException e) {e.printStackTrace(); }
new Thread(()->{
phone2.sendMessage();
}, "t2").start();
try { TimeUnit.SECONDS.sleep(1); } catch (InterruptedException e) {e.printStackTrace(); }
}
}
两个对象同一把锁 Phone.class
7.1个普通同步方法,1个静态同步方法,1部手机,请问先打印邮件还是短信? 短信
class Phone{
public
synchronized void sendEmail(){
try { TimeUnit.SECONDS.sleep(4); } catch (InterruptedException e) {e.printStackTrace(); }
System.out.println("==========sendEmail");
}
public static
synchronized void sendMessage(){
System.out.println("======sendMessage");
}
}
public class LockDemo {
public static void main(String[] args) {
Phone phone = new Phone();
new Thread(()->{
phone.sendEmail();
}, "t1").start();
try { TimeUnit.SECONDS.sleep(1); } catch (InterruptedException e) {e.printStackTrace(); }
new Thread(()->{
phone.sendMessage();
}, "t2").start();
try { TimeUnit.SECONDS.sleep(1); } catch (InterruptedException e) {e.printStackTrace(); }
}
}
t1 phone对象锁this锁 t2类锁 Phone.class 不是同一把锁
81个普通同步方法,1个静态同步方法,2部手机,请问先打印邮件还有短信? 短信
class Phone{
public
synchronized void sendEmail(){
try { TimeUnit.SECONDS.sleep(4); } catch (InterruptedException e) {e.printStackTrace(); }
System.out.println("==========sendEmail");
}
public static
synchronized void sendMessage(){
System.out.println("======sendMessage");
}
/*
public void hello(){
System.out.println("say hello");
}*/
}
public class LockDemo {
public static void main(String[] args) {
Phone phone = new Phone();
Phone phone2 = new Phone();
new Thread(()->{
phone.sendEmail();
}, "t1").start();
try { TimeUnit.SECONDS.sleep(1); } catch (InterruptedException e) {e.printStackTrace(); }
new Thread(()->{
phone2.sendMessage();
}, "t2").start();
try { TimeUnit.SECONDS.sleep(1); } catch (InterruptedException e) {e.printStackTrace(); }
}
}
不是同一把锁
最后
以上就是刻苦香水为你收集整理的synchronized的总结的全部内容,希望文章能够帮你解决synchronized的总结所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复