package callable;
//定义一个乌龟类
public class WuGui implements Runnable {
@Override
public void run() {
while (true) {
System.out.println(Thread.currentThread().getName() + "乌龟领先了");
}
}
}
package callable;
//定义一个兔子类
public class TuZi implements Runnable {
@Override
public void run() {
while (true) {
System.out.println(Thread.currentThread().getName() + "领先了");
}
}
}
package callable;
import java.util.Random;
import java.util.concurrent.Callable;
import java.util.concurrent.FutureTask;
public class TestCallable implements Callable<Integer> {
@Override
public Integer call() throws Exception {
return new Random().nextInt(10);
}
public static void main(String[] args) {
TestCallable tc = new TestCallable();
// 中间类FutureTask
FutureTask<Integer> ft = new FutureTask<Integer>(tc);
Thread t = new Thread(ft);
t.start();
// 判断程序是否执行完毕boolean
System.out.println(ft.isDone());// false
try {
System.out.println(tc.call());
} catch (Exception e) {
e.printStackTrace();
}
// 判断程序是否执行完毕boolean
System.out.println(ft.isDone());// true
}
}