本文源码:Gitee·点这里
问题1
问:如下代码,两个方法谁先输出?
答:先输出 call,后 sendMsg
原因:被 synchronized 修饰的方法,锁的对象是方法调用者,所以说这里两个方法调用的对象是同一个,先调用的先执行!
public class Lock1 {
public static void main(String[] args) throws InterruptedException {
Phone phone = new Phone();
new Thread(phone::call, "A").start();
TimeUnit.SECONDS.sleep(1);
new Thread(phone::sendMsg, "B").start();
}
}
class Phone {
synchronized void call() {
System.out.println("call");
}
synchronized void sendMsg() {
System.out.println("sendMsg");
}
}
问题2
问:如下代码,两个方法谁先输出?
答:先输出 call,后 sendMsg
原因:被 synchronized 修饰的方法,锁的对象是方法调用者,所以说这里两个方法调用的对象是同一个,先调用的先执行!
public class Lock2 {
public static void main(String[] args) throws InterruptedException {
Phone phone = new Phone();
new Thread(phone::call, "A").start();
TimeUnit.SECONDS.sleep(1);
new Thread(phone::sendMsg, "B").start();
}
}
class Phone {
synchronized void call() {
try {
TimeUnit.SECONDS.sleep(2);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("call");
}
synchronized void sendMsg() {
System.out.println("sendMsg");
}
}
问题3
问:如下代码,两个方法谁先输出?
答:先输出 sendMsg,后 call
原因:没有加锁的方法不受锁的影响,所以按照休眠时间 sendMsg 会先输出
public class Lock3 {
public static void main(String[] args) throws InterruptedException {
Phone phone = new Phone();
new Thread(phone::call, "A").start();
TimeUnit.SECONDS.sleep(1);
new Thread(phone::sendMsg, "B").start();
}
}
class Phone {
synchronized void call() {
try {
TimeUnit.SECONDS.sleep(2);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("call");
}
void sendMsg() {
System.out.println("sendMsg");
}
}
问题4
问:如下代码,两个方法谁先输出?
答:先输出 sendMsg,后 call
原因:被 synchronized 修饰的方法,锁的对象是方法调用者。这里锁的是两个不同的调用者,所以互不影响,按照休眠时间 sendMsg 会先输出
public class Lock4 {
public static void main(String[] args) throws InterruptedException {
Phone phoneA = new Phone();
Phone phoneB = new Phone();
new Thread(phoneA::call, "A").start();
TimeUnit.SECONDS.sleep(1);
new Thread(phoneB::sendMsg, "B").start();
}
}
class Phone {
synchronized void call() {
try {
TimeUnit.SECONDS.sleep(2);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("call");
}
synchronized void sendMsg() {
System.out.println("sendMsg");
}
}
问题5
问:如下代码,两个方法谁先输出?
答:先输出 sendMsg,后 call
原因:被 synchronized 修饰的方法,锁的对象是方法调用者。这里锁的是两个不同的调用者,所以互不影响,按照休眠时间 sendMsg 会先输出
public class Lock5 {
public static void main(String[] args) throws InterruptedException {
Phone phoneA = new Phone();
Phone phoneB = new Phone();
new Thread(phoneA::call, "A").start();
TimeUnit.SECONDS.sleep(1);
new Thread(phoneB::sendMsg, "B").start();
}
}
class Phone {
synchronized void call() {
try {
TimeUnit.SECONDS.sleep(2);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("call");
}
synchronized void sendMsg() {
System.out.println("sendMsg");
}
}
问题6
问:如下代码,两个方法谁先输出?
答:先输出 call,后 sendMsg
原因:被 synchronized 修饰的方法,锁的对象是方法调用者。这里两个方法都是静态方法,锁的是同一对象(Phone.class),所以先调用的先执行
public class Lock6 {
public static void main(String[] args) throws InterruptedException {
Phone phoneA = new Phone();
Phone phoneB = new Phone();
new Thread(() -> {
phoneA.call();
}, "A").start();
TimeUnit.SECONDS.sleep(1);
new Thread(() -> {
phoneB.sendMsg();
}, "B").start();
}
}
class Phone {
static synchronized void call() {
try {
TimeUnit.SECONDS.sleep(2);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("call");
}
static synchronized void sendMsg() {
System.out.println("sendMsg");
}
}
问题7
问:如下代码,两个方法谁先输出?
答:先输出 sendMsg,后 call
原因:被 synchronized 修饰的方法,锁的对象是方法调用者。这里一个是静态方法,一个是非静态方法,锁的对象不同(A线程锁的对象是 Phone.class,B线程锁的对象是 phone 对象),按照休眠时间 sendMsg 会先输出
public class Lock7 {
public static void main(String[] args) throws InterruptedException {
Phone phone = new Phone();
new Thread(() -> {
phone.call();
}, "A").start();
TimeUnit.SECONDS.sleep(1);
new Thread(() -> {
phone.sendMsg();
}, "B").start();
}
}
class Phone {
static synchronized void call() {
try {
TimeUnit.SECONDS.sleep(2);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("call");
}
synchronized void sendMsg() {
System.out.println("sendMsg");
}
}
问题8
问:如下代码,两个方法谁先输出?
答:先输出 sendMsg,后 call
原因:被 synchronized 修饰的方法,锁的对象是方法调用者。这里一个是静态方法,一个是非静态方法,锁的对象不同(A线程锁的对象是 Phone.class,B线程锁的对象是 phoneB 对象),按照休眠时间 sendMsg 会先输出
public class Lock8 {
public static void main(String[] args) throws InterruptedException {
Phone phoneA = new Phone();
Phone phoneB = new Phone();
new Thread(() -> {
phoneA.call();
}, "A").start();
TimeUnit.SECONDS.sleep(1);
new Thread(() -> {
phoneB.sendMsg();
}, "B").start();
}
}
class Phone {
static synchronized void call() {
try {
TimeUnit.SECONDS.sleep(2);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("call");
}
synchronized void sendMsg() {
System.out.println("sendMsg");
}
}