public class runt {
public static void main(String[] args) {
MyThread1 thread1=new MyThread1("thread1");
thread1.start();
MyThread1 thread2=new MyThread1("thread2");
thread2.start();
}
static class MyThread1 extends Thread{
public MyThread1(String name){
super(name);
}
public void run(){
int i=0;
while(i++<5){
System.out.println(Thread.currentThread().getName()+"的run()方法在运行");
}
}
}
}
这个跟电脑性能有关系的。你把打印控制大一点,就能看出效果了。比如打印100以内
这个地方是随机的 你这个情况只是一种特殊情况而已。
帮你修改了一下,这样就正常了。
public class runt {
public static void main(String[] args) {
MyThread1 thread1=new MyThread1("thread1");
thread1.start();
MyThread1 thread2=new MyThread1("thread2");
thread2.start();
}
static class MyThread1 extends Thread{
public MyThread1(String name){
super(name);
}
public void run(){
int i=0;
while(i++<5){
try {
Thread.sleep(100);
}catch(Exception e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+"的run()方法在运行");
}
}
}
}
你可以将I++<5调整为i++<100再试试,现代CPU性能高,小于5的话CPU调度还未进行切换,线程1就已经走完了,所以呈现出这种情况。
懂了懂了
100个人跑50米,肯定会有人同时到的,不会一个接一个到的,你控制不了任何一个人
您好,我是有问必答小助手,您的问题已经有小伙伴解答了,您看下是否解决,可以追评进行沟通哦~
如果有您比较满意的答案 / 帮您提供解决思路的答案,可以点击【采纳】按钮,给回答的小伙伴一些鼓励哦~~
ps:问答VIP仅需29元,即可享受5次/月 有问必答服务,了解详情>>>https://vip.csdn.net/askvip?utm_source=1146287632