package com.yuan.test; public class LiftOff implements Runnable { protected int countDown = 10; // Default 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.print(status()); Thread.yield(); } } public static void main(String[] args){ LiftOff lo=new LiftOff(); lo.status(); lo.run(); LiftOff lo1=new LiftOff(); lo1.status(); lo1.run(); } } ///:~
The identifier id distinguishes between multiple instances of the task. It is final because it is
not expected to change once it is initialized.
上述ID 是用来区分线程的个数的,而且因为它是final 类型的所以一旦实例化了就不会被修改了。
A task’s run( ) method usually has some kind of loop that continues until the task is no
longer necessary, so you must establish the condition on which to break out of this loop (one
option is to simply return from run( )).
Often, run( ) is cast in the form of an infinite loop,which means that, barring some factor that causes run( ) to terminate, it will continue forever (later in the chapter you’ll see how to safely terminate tasks).
The call to the static method Thread.yield( ) inside run( ) is a suggestion to the thread
scheduler (the part of the Java threading mechanism that moves the CPU from one thread to
the next) that says,
Thread.yields()是一个静态的方法,用来给CPU提一下建议说,“您已经帮我完成最重要的工作了,你要是忙,你就先走吧”
"I’ve done the important parts of my cycle and this would be a good time
to switch to another task for a while."
It’s completely optional, but it is used here because it tends to produce more interesting output in these examples: You’re more likely to see evidence of tasks being swapped in and out.
public class TestThread { public static void main(String[] args) { Thread t = new Thread(new LiftOff()); t.start(); System.out.println("Waiting for LiftOff1"); Thread t1 = new Thread(new LiftOff()); t1.start(); System.out.println("Waiting for LiftOff2"); } }
output:
Waiting for LiftOff1
Waiting for LiftOff2
#1(9), #0(9), #0(8), #0(7), #0(6), #0(5), #0(4), #0(3), #0(2), #0(1), #0(Liftoff!), #1(8), #1(7), #1(6), #1(5), #1(4), #1(3), #1(2), #1(1), #1(Liftoff!),
When main( ) creates the Thread objects, it isn’t capturing the references for any of them.
With an ordinary object, this would make it fair game for garbage collection, but not with a
Thread. Each Thread "registers" itself so there is actually a reference to it someplace, and
the garbage collector can’t clean it up until the task exits its run( ) and dies. You can see
from the output that the tasks are indeed running to conclusion, so a thread creates a
separate thread of execution that persists after the call to start( ) completes.