代码来自《java编程思想》
这个程序执行多次,输出是不一样的。挑了一个比较乱的顺序大家感受下
任务:
我们使用多线程,去执行的都是任务,那么我们首先得学会如何定义一个任务。
public class LiftOff implements Runnable{
protected int countDown = 10;
private static int taskCount = 0;
private final int id = taskCount++;
public LiftOff(){}
public LiftOff(int countDown)
{
this.countDown = countDown;
}
public String status(){
return "#"+id+"("+(countDown>0?countDown:"liftOff!")+"),";
}
public void run(){
while(countDown-->0)
System.out.println(status());
Thread.yield();
}
}
这个任务类,实现了Runnable接口,然后覆盖run()方法。这两点是必须的。
如果,我们在main方法里面,调用这个run方法,比如
public static void main(String args[]){
LiftOff launch = new LiftOff();
launch.run();
}
会输出:
#0(9),
#0(8),
#0(7),
#0(6),
#0(5),
#0(4),
#0(3),
#0(2),
#0(1),
#0(liftOff!),
其实这里虽然我们执行了一个任务(launch 实现了Runnable接口),但是这也不是我们今天要看的多线程。他就像一个普通的调用方法一样在执行。整个执行过程中,
只有main()一个线程,run()方法在main线程中执行。
那么我们怎么将这个任务变成多线程执行呢。
Thread类
那么我们怎么将这个任务变成多线程执行呢?将他交给Thread构造器。
public static void main(String args[]){
Thread t = new Thread(new LiftOff());
t.start();
System.out.println("wait for liftoff");
}
输出:
wait for liftoff
#0(9),
#0(8),
#0(7),
#0(6),
#0(5),
#0(4),
#0(3),
#0(2),
#0(1),
#0(liftOff!),
如果我们没有多线程的概念,还是停留在顺序执行的思维中。会不会比较奇怪System.out.println("wait for liftoff");竟然在t.start()方法前面执行!
实际上Thread t = new Thread(new LiftOff()); t.start();这两句是产生一个新的线程,然后去执行任务的run()方法。
这里,我们就有了两个线程,一个main函数的线程,一个run()方法的线程。那么至于main()函数中之后的操作和run()方法以什么样子的顺序执行。这个只能有线程调用器自动控制了。
main()中多几个线程,我们看看究竟怎么执行的
public static void main(String args[]){
for(int i=0;i<5;i++){
new Thread(new LiftOff()).start();
}
System.out.println("wait for liftoff");
}
这个程序执行多次,输出是不一样的。挑了一个比较乱的顺序大家感受下
wait for liftoff
#2(9),
#3(9),
#4(9),
#1(9),
#2(8),
#1(8),
#4(8),
#0(9),
#3(8),
#0(8),
#4(7),
#1(7),
#2(7),
#1(6),
#4(6),
#4(5),
#0(7),
#3(7),
#0(6),
#4(4),
#1(5),
#2(6),
#1(4),
#4(3),
#0(5),
#3(6),
#0(4),
#4(2),
#1(3),
#2(5),
#1(2),
#4(1),
#0(3),
#3(5),
#0(2),
#4(liftOff!),
#1(1),
#2(4),
#1(liftOff!),
#0(1),
#0(liftOff!),
#3(4),
#2(3),
#3(3),
#2(2),
#3(2),
#2(1),
#3(1),
#2(liftOff!),
#3(liftOff!),
补充:从任务中返回值
实现了Runnable接口,覆盖了run方法不会返回任何值,如果希望每个任务返回一个值。那就可以实现Callable接口,覆盖call()方法。但是,必须使用ExecutorService.sunmit()方法调用他。ExecutorService.sunmit()用法见第二篇文章