public class Test {
public static void main(String[] args) {
Phone phone = new Phone();
System.out.println(Thread.currentThread().getName()+"===>111111");
new Thread(()->{
phone.sendSms();
},"A").start();
System.out.println(Thread.currentThread().getName()+"====>22222");
try {
TimeUnit.SECONDS.sleep(3);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+"====>333333");
try {
TimeUnit.SECONDS.sleep(4);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+"=====>444444444");
new Thread(()->{
phone.call();
},"B").start();
new Thread(()->{
phone.call();
},"C").start();
new Thread(()->{
phone.call();
},"D").start();
System.out.println(Thread.currentThread().getName()+"====>55555555");
}
}
class Phone{
public synchronized void sendSms(){
System.out.println(Thread.currentThread().getName()+"发短信");
}
public synchronized void call(){
System.out.println(Thread.currentThread().getName()+"打电话");
}
}
最后输出总是
main===>111111
main====>22222
A发短信
main====>333333
main=====>444444444
B打电话
C打电话
main====>55555555
D打电话
其他都好理解,主要是这个D线程为什么总是在输出“5”的主线程后面啊!
线程启动以后只是进入就绪太,并不一定马上执行,这里要等待前面的线程有没有执行完成,还有如果前面线程调用的call 同步方法没执行完也得等,所以5555555的打印在D 的前面
这种属于线程调度了,没有规律,所以不需要深究。
D线程在输出“5”的主线程后面是偶然现象,属于操作系统线程调度恰好
您好,我是有问必答小助手,你的问题已经有小伙伴为您解答了问题,您看下是否解决了您的问题,可以追评进行沟通哦~
如果有您比较满意的答案 / 帮您提供解决思路的答案,可以点击【采纳】按钮,给回答的小伙伴一些鼓励哦~~
ps:问答VIP仅需29元,即可享受5次/月 有问必答服务,了解详情>>>https://vip.csdn.net/askvip?utm_source=1146287632
博主,多做做这种题目,比较有益。考点
package com.example.demo.thread;
import java.util.concurrent.Semaphore;
public class Semaphore1 {
public static Semaphore sem1;
public static Semaphore sem2;
public static Semaphore sem3;
public static Semaphore sem4;
static class FirstThread extends Thread {
public void run() {
try {
while (true) {
sem1.acquire();
System.out.println("1");
sem2.release();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
static class SecondThread extends Thread {
public void run() {
try {
while (true) {
sem2.acquire();
System.out.println("2");
sem3.release();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
static class ThirdThread extends Thread {
public void run() {
try {
while (true) {
sem3.acquire();
System.out.println("3");
sem4.release();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
static class ForthThread extends Thread {
public void run() {
try {
while (true) {
sem4.acquire();
System.out.println("4");
sem1.release();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
sem1 = new Semaphore(1);
sem2 = new Semaphore(1);
sem3 = new Semaphore(1);
sem4 = new Semaphore(1);
try {
// 不要有sem1.acquire()
sem2.acquire();
sem3.acquire();
sem4.acquire();
} catch (InterruptedException e) {
e.printStackTrace();
}
new FirstThread().start();
new SecondThread().start();
new ThirdThread().start();
new ForthThread().start();
}
}
非常感谢您使用有问必答服务,为了后续更快速的帮您解决问题,现诚邀您参与有问必答体验反馈。您的建议将会运用到我们的产品优化中,希望能得到您的支持与协助!
速戳参与调研>>>https://t.csdnimg.cn/Kf0y
1-2-3-4