Java实例 定义任务的一个发射小程序 实现Runnable接口并编写run()方法

来源:互联网 发布:盘锦大数据 编辑:程序博客网 时间:2024/04/29 04:47

举例:


class LiftOff implements Runnable {
protected int countDown = 10;  //默认值
private static int taskCount = 0 ;
private final int id = taskCount++;

public LiftOff(int countDown) {
this.countDown = countDown;
}


public LiftOff() {
// TODO Auto-generated constructor stub
}


public String status() {
return "#" + id + "(" + (countDown >0 ? countDown : "Liftoff!") + ") ";
}



@Override
public void run() {
// TODO Auto-generated method stub
do {
System.out.println(status());
Thread.yield();
} while (countDown-- >0) ;
}


}


public class Test05 {
public static void main(String[] args) {
LiftOff launch = new LiftOff();
launch.run();
}


}



运行结果:

#0(10) 
#0(9) 
#0(8) 
#0(7) 
#0(6) 
#0(5) 
#0(4) 
#0(3) 
#0(2) 
#0(1) 
#0(Liftoff!) 


拓展:Thread类

public class Thread implements Runnable {}


改进:


class LiftOff implements Runnable {
protected int countDown = 10;  //默认值
private static int taskCount = 0 ;
private final int id = taskCount++;

public LiftOff(int countDown) {
this.countDown = countDown;
}


public LiftOff() {
// TODO Auto-generated constructor stub
}


public String status() {
return "#" + id + "(" + (countDown >0 ? countDown : "发射!") + ") ";
}



@Override
public void run() {
// TODO Auto-generated method stub
do {
try {
Thread.sleep(1000); 

} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(status());
Thread.yield();
} while (countDown-- >0) ;
}


}


public class Test05 {
public static void main(String[] args) {
Thread t = new Thread(new LiftOff());
t.start();

System.out.println("倒计时十秒,等待发射!");

}

}



演示多个火箭发射:

class LiftOff implements Runnable {
protected int countDown = 10;  //默认值
private static int taskCount = 0 ;
private final int id = taskCount++;

public LiftOff(int countDown) {
this.countDown = countDown;
}


public LiftOff() {
// TODO Auto-generated constructor stub
}


public String status() {
return "#" + id + "(" + (countDown >0 ? countDown : "发射!") + ") ";
}



@Override
public void run() {
// TODO Auto-generated method stub
do {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(status());
Thread.yield();
} while (countDown-- >0) ;
}


}


public class Test05 {
public static void main(String[] args) {
// Thread t = new Thread(new LiftOff());
// t.start();
// System.out.println("倒计时十秒,火箭1等待发射!");
//
// Thread t2 = new Thread(new LiftOff());
// t2.start();
// System.out.println("倒计时十秒,火箭2等待发射!");
//
// Thread t3 = new Thread(new LiftOff());
// t3.start();
// System.out.println("倒计时十秒,火箭3等待发射!");
for (int i = 1; i < 5; i++) {
new Thread(new LiftOff()).start();
System.out.println("倒计时十秒,火箭"+i+"等待发射!");
}


}


}


运行结果:

倒计时十秒,火箭1等待发射!
倒计时十秒,火箭2等待发射!
倒计时十秒,火箭3等待发射!
倒计时十秒,火箭4等待发射!
#1(10) 
#0(10) 
#3(10) 
#2(10) 
#1(9) 
#0(9) 
#3(9) 
#2(9) 
#1(8) 
#0(8) 
#3(8) 
#2(8) 
#1(7) 
#0(7) 
#2(7) 
#3(7) 
#0(6) 
#1(6) 
#2(6) 
#3(6) 
#0(5) 
#1(5) 
#3(5) 
#2(5) 
#1(4) 
#0(4) 
#3(4) 
#2(4) 
#1(3) 
#0(3) 
#2(3) 
#3(3) 
#1(2) 
#2(2) 
#3(2) 
#0(2) 
#2(1) 
#1(1) 
#3(1) 
#0(1) 
#3(发射!) 
#2(发射!) 
#1(发射!) 
#0(发射!) 


知识说明:

(1)sleep()方法和wait()方法的区别

对于sleep()方法,我们首先要知道该方法是属于Thread类中的。而wait()方法,则是属于Object类中的。

sleep()方法导致了程序暂停执行指定的时间,让出cpu该其他线程,但是他的监控状态依然保持者,当指定的时间到了又会自动恢复运行状态。

在调用sleep()方法的过程中,线程不会释放对象锁。

而当调用wait()方法的时候,线程会放弃对象锁,进入等待此对象的等待锁定池,只有针对此对象调用notify()方法后本线程才进入对象锁定池准备


(2) 认识Thread的 start() 和 run()

1.start():
     使该线程开始执行;Java 虚拟机调用该线程的 run 方法。
     结果是两个线程并发地运行;当前线程(从调用返回给 start 方法)和另一个线程(执行其 run 方法)。
     多次启动一个线程是非法的。特别是当线程已经结束执行后,不能再重新启动。
     用start方法来启动线程,真正实现了多线程运行,这时无需等待run方法体代码执行完毕而直接继续执行下面的代码。通过调用Thread类的 start()方法来启动一个线程,这时此线程处于就绪(可运行)状态,并没有运行,一旦得到cpu时间片,就开始执行run()方法,这里方法 run()称为线程体,它包含了要执行的这个线程的内容,Run方法运行结束,此线程随即终止。


2.run():
     如果该线程是使用独立的 Runnable 运行对象构造的,则调用该 Runnable 对象的 run 方法;否则,该方法不执行任何操作并返回。
    Thread 的子类应该重写该方法。

     run()方法只是类的一个普通方法而已,如果直接调用Run方法,程序中依然只有主线程这一个线程,其程序执行路径还是只有一条,还是要顺序执行,还是要等待run方法体执行完毕后才可继续执行下面的代码,这样就没有达到写线程的目的。

3。总结:
     调用start方法方可启动线程,而run方法只是thread的一个普通方法调用,还是在主线程里执行。



0 0