public class Actor extends Thread {
public void run(){
System.out.println(getName()+"是一个演员!");
int count=0;
while(true){
System.out.println(getName()+"登台演出:"+(++count));
if(count==50){
break;
}
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) {
Thread actor=new Actor();
actor.setName("Mr.thread ");
//actor.run();
actor.start();
Thread actressThread=new Thread(new Actress(),"MS.Runnable");
actressThread.start();
}
}
class Actress implements Runnable{
public void run() {
// TODO Auto-generated method stub
System.out.println(Thread.currentThread().getName()+"是一个演员!");
int count=0;
while(true){
System.out.println(Thread.currentThread().getName()+"登台演出:"+(++count));
if(count==50){
break;
}
if(count%10==0){
try {
Thread.sleep(1000);//延长一秒
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
System.out.println(Thread.currentThread().getName()+"的演出结束了");
}
}
Thread actressThread=new Thread(new Actress(),"MS.Runnable");
Runnable 接口只有一个run方法 , 实现Runnable接口创建的线程最终还是要通过将自身实例作为参数传递给Thread然后执行
Thread.currentThread() 得到的就是当前thread对象
运行结果:一部分
Mr.thread 是一个演员!
Mr.thread 登台演出:1
Mr.thread 登台演出:2
Mr.thread 登台演出:3
Mr.thread 登台演出:4
Mr.thread 登台演出:5
Mr.thread 登台演出:6
Mr.thread 登台演出:7
Mr.thread 登台演出:8
MS.Runnable是一个演员!
Mr.thread 登台演出:9
MS.Runnable登台演出:1
Mr.thread 登台演出:10
MS.Runnable登台演出:2
MS.Runnable登台演出:3
MS.Runnable登台演出:4
MS.Runnable登台演出:5
MS.Runnable登台演出:6
MS.Runnable登台演出:7
MS.Runnable登台演出:8
MS.Runnable登台演出:9
MS.Runnable登台演出:10
Mr.thread 登台演出:11
Mr.thread 登台演出:12
Mr.thread 登台演出:13
Mr.thread 登台演出:14
Mr.thread 登台演出:15
Mr.thread 登台演出:16
Mr.thread 登台演出:17
Mr.thread 登台演出:18
Mr.thread 登台演出:19
Mr.thread 登台演出:20
MS.Runnable登台演出:11
MS.Runnable登台演出:12
线程只要启动之后不让他等待,就会执行一次代码循环后结束。但其中的过程并不是连续的,代码中指令执行一个while循环后,会释放系统资源使得其他线程有机会竞争系统资源,倘若当前线程休眠(等待),就不会再对资源进行争用,自然也就不会执行下一个while了。而剩下的线程是活跃的,自然只能看到他们争用资源了。
然后 创建一个舞台类和军队类:
public class ArmyRunnable implements Runnable {
//volatile保证线程可以真确的读取其他线程写入的值
//可见性的问题所以要声明为volatile ref.JMM ,happens-before原则
volatile boolean keepRunning=true;
//军队线程
//模拟作战双方的行为
public void run() {
while (keepRunning){//从Stage中引入keepRunning的值
//发动5连击
for(int i=0;i<5;i++){
System.out.println(Thread.currentThread().getName()+"进攻对方【"+i+"】");
//让出很处理器时间,下次谁进攻不一定
Thread.yield();
}
}
System.out.println(Thread.currentThread().getName()+"结束了战斗!");
}
}
public class Stage extends Thread {
/**
* 隋唐演义舞台
*/
public void run(){
//引入军队
ArmyRunnable armyTaskOfSuiDynasty=new ArmyRunnable();
ArmyRunnable armyTaskOfRevolt=new ArmyRunnable();
//创建线程
Thread armyOfSuiDynasty= new Thread(armyTaskOfSuiDynasty,"隋军");
Thread armyOfRevolt=new Thread(armyTaskOfRevolt,"龙鸣起义军");
//启动线程
armyOfSuiDynasty.start();//start()作用: 使该线程开始执行;Java 虚拟机调用该线程的 run 方法。
armyOfRevolt.start();
//舞台线程休眠 大家专心观看军队的厮杀
//为什么要让舞台线程休眠呢 :三个线程在运行如果不让舞台线程休眠 两个军队线程进攻一个五连击就会执行stage类的其他代码
//让它休眠:两个军队就可以进攻500毫秒的时间 (Thread.sleep(500))再执行其他的代码 keepRunning就会变成false
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
armyTaskOfSuiDynasty.keepRunning=false;
armyTaskOfRevolt.keepRunning=false;
try {
armyOfRevolt.join();
} catch (InterruptedException e) {
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new Stage().start();
}
}