为什么运行时前几次那么乱呢,一会先生一会女士。不规律
package com.xuanxuan;
public class Actor extends Thread {
public void run() {
System.out.println(getName()+"是一个演员");
int count=0;
boolean keepRuning=true;
while(keepRuning) {
System.out.println(getName()+"登台演出"+(++count));
if(count==100) {
keepRuning=false;
}
if(count%10==0) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
System.out.println(getName()+"演出结束了");
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Thread actor=new Actor();
actor.setName("先生");
Thread actressThread=new Thread(new Actress(),"xiaoxiaoxiao女士");
actor.start();
actressThread.start();
}
}
class Actress implements Runnable{
public void run() {
System.out.println(Thread.currentThread().getName()+"是一个演员");
int count=0;
boolean keepRuning=true;
while(keepRuning) {
System.out.println(Thread.currentThread().getName()+"登台演出"+(++count));
if(count==100) {
keepRuning=false;
}
if(count%10==0) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
System.out.println(Thread.currentThread().getName()+"演出结束了");
}
}