标签:Thread void send 问题 Phone new 多线程 public
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
分析
标签:Thread,void,send,问题,Phone,new,多线程,public
来源: https://www.cnblogs.com/songjilong/p/12817071.html
本站声明:
1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。