Phone 有两个方法:发送邮件和发送短信,每个方法都打印一句话,现在通过不同的方式对方法进行操作,回答出打印的先后顺序(建议先自己看代码认真思考,然后再看答案,文章结尾会对每个问题进行分析)
问题
1、标准访问,两线程中间睡眠 2 毫秒,先打印邮件还是短信?
class Phone {
public synchronized void sendEmail() {
System.out.println("send email");
}
public synchronized void sendSms() {
System.out.println("send sms");
}
}
public class Lock01 {
public static void main(String[] args) throws InterruptedException {
Phone phone = new Phone();
new Thread(() -> phone.sendEmail(), "A").start();
Thread.sleep(200);
new Thread(() -> phone.sendSms(), "B").start();
}
}
查看答案
send emailsend sms
2、在 sendEmail() 方法中睡眠 4 秒,先打印邮件还是短信?
class Phone {
public synchronized void sendEmail() {
try {
Thread.sleep(4000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("send email");
}
public synchronized void sendSms() {
System.out.println("send sms");
}
}
public class Lock02 {
public static void main(String[] args) throws InterruptedException {
Phone phone = new Phone();
new Thread(() -> phone.sendEmail(), "A").start();
Thread.sleep(200);
new Thread(() -> phone.sendSms(), "B").start();
}
}
查看答案
send emailsend sms
3、添加普通的 hello() 方法,先打印邮件还是 hello?
class Phone {
public synchronized void sendEmail() {
try {
Thread.sleep(4000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("send email");
}
public void hello() {
System.out.println("hello");
}
}
public class Lock03 {
public static void main(String[] args) throws InterruptedException {
Phone phone = new Phone();
new Thread(() -> phone.sendEmail(), "A").start();
Thread.sleep(200);
new Thread(() -> phone.hello(), "B").start();
}
}
查看答案
hellosend email
4、2 个手机,先打印邮件还是短信?
class Phone {
public synchronized void sendEmail() {
try {
Thread.sleep(4000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("send email");
}
public synchronized void sendSms() {
System.out.println("send sms");
}
}
public class Lock04 {
public static void main(String[] args) throws InterruptedException {
Phone phone1 = new Phone();
Phone phone2 = new Phone();
new Thread(() -> phone1.sendEmail(), "A").start();
Thread.sleep(200);
new Thread(() -> phone2.sendSms(), "B").start();
}
}
查看答案
send smssend email
5、2个静态同步方法,1部手机,先打印邮件还是短信?
class Phone {
public static synchronized void sendEmail() {
try {
Thread.sleep(4000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("send email");
}
public static synchronized void sendSms() {
System.out.println("send sms");
}
}
public class Lock05 {
public static void main(String[] args) throws InterruptedException {
Phone phone = new Phone();
new Thread(() -> phone.sendEmail(), "A").start();
Thread.sleep(200);
new Thread(() -> phone.sendSms(), "B").start();
}
}
查看答案
send emailsend sms
class Phone {
public static synchronized void sendEmail() {
try {
Thread.sleep(4000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("send email");
}
public static synchronized void sendSms() {
System.out.println("send sms");
}
}
public class Lock06 {
public static void main(String[] args) throws InterruptedException {
Phone phone1 = new Phone();
Phone phone2 = new Phone();
new Thread(() -> phone1.sendEmail(), "A").start();
Thread.sleep(200);
new Thread(() -> phone2.sendSms(), "B").start();
}
}
查看答案
send emailsend sms
class Phone {
public static synchronized void sendEmail() {
try {
Thread.sleep(4000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("send email");
}
public synchronized void sendSms() {
System.out.println("send sms");
}
}
public class Lock07 {
public static void main(String[] args) throws InterruptedException {
Phone phone = new Phone();
new Thread(() -> phone.sendEmail(), "A").start();
Thread.sleep(200);
new Thread(() -> phone.sendSms(), "B").start();
}
}
查看答案
send smssend email
8、1个静态同步方法,1个普通同步方法,2部手机,先打印邮件还是短信?
class Phone {
public static synchronized void sendEmail() {
try {
Thread.sleep(4000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("send email");
}
public synchronized void sendSms() {
System.out.println("send sms");
}
}
public class Lock08 {
public static void main(String[] args) throws InterruptedException {
Phone phone1 = new Phone();
Phone phone2 = new Phone();
new Thread(() -> phone1.sendEmail(), "A").start();
Thread.sleep(200);
new Thread(() -> phone2.sendSms(), "B").start();
}
}
查看答案
send smssend email
分析