Runnable接口
Runnable接口只定义了一个方法public void run(),这个方法要求实现Runnable接口的类实现,Runnable对象称为可运行对象,一个线程的运行就是执行该对象的run()方法
run()方法没有返回值void,而且不能抛出异常
Thread类中的定义
private Runnable target;
@Override
public void run() {
if (target != null) {
target.run();
}
}
public class Test1 {
public static void main(String[] args) {
// 简化写法
new Thread(() -> {
for (int i = 0; i < 10; i++) {
System.out.println("左手...");
try {
Thread.sleep(30);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
}
}
class MyRunnable implements Runnable {
@Override
public void run() throws Exception {//语法报错,这里不允许抛出异常,如果其中有异常则需要使用try.catch处理
// 没有返回值,如果需要记录处理结果,需要自己编程处理
}
}
Callable接口
继承Thread或实现Runnable接口这2种方式都有一个缺陷就是:在执行完任务之后无法获取执行结果。
如果需要获取执行结果,就必须通过共享变量或者使用线程通信的方式来达到效果,这样使用起来就比较麻烦。call()方法有返回值,这个返回值可以通过泛型进行约束,允许抛出异常
public class Test2 {
public static void main(String[] args) {
// 简化写法
new Thread(new FutureTask<>(() -> {
for (int i = 0; i < 10; i++) {
System.out.println("右手画彩虹");
Thread.sleep(30);// 因为call方法允许抛出异常
}
return null;
})).start();
}
}
class MyRunnable2 implements Callable<Number> {
// <>中用于指定返回值类型,必须使用引用类型,不能使用简单类型
public Number call() throws Exception {// 允许抛出异常
return null;
}
}
Future接口
Future表示一个任务的生命周期,并提供了方法来判断是否已经完成或取消以及获取任务的结果和取消任务等
Future就是对于具体的Runnable或者Callable任务的执行结果进行取消、查询是否完成、获取结果。必要时可以通过get方法获取执行结果,该方法会阻塞直到任务返回结果。
cancel方法用来取消任务,如果取消任务成功则返回true,如果取消任务失败则返回false。参数mayInterruptIfRunning表示是否允许取消正在执行却没有执行完毕的任务,如果设置true,则表示可以取消正在执行过程中的任务。如果任务已经完成,则无论mayInterruptIfRunning为true还是false,此方法肯定返回false,即如果取消已经完成的任务会返回false;如果任务正在执行,若mayInterruptIfRunning设置为true,则返回true,若mayInterruptIfRunning设置为false,则返回false;如果任务还没有执行,则无论mayInterruptIfRunning为true还是false,肯定返回true。
isCancelled方法表示任务是否被取消成功,如果在任务正常完成前被取消成功,则返回 true
isDone方法表示任务是否已经完成,若任务完成,则返回true
有用的方法
get()方法用来获取执行结果,这个方法会产生阻塞,会一直等到任务执行完毕才返回
get(long timeout, TimeUnit unit)用来获取执行结果,如果在指定时间内,还没获取到结果,就抛出TimeoutException超时异常。
public class Test3 {
public static void main(String[] args) throws InterruptedException {
Future f = new FutureTask(() -> {
for (int i = 0; i < 1000; i++) {
System.out.println(Thread.currentThread() + "...start..." + i);
Thread.sleep(10); //模拟一个处理过程
System.out.println(Thread.currentThread() + "...end..." + i);
}
return null;
});
if (f instanceof Runnable) {
new Thread((Runnable) f).start();
int counter = 0;
while (true) {
Thread.sleep(20);
System.out.println("任务是否被取消:" + f.isCancelled() + "--" + counter);
System.out.println("任务是否执行完毕:" + f.isDone());
counter++;
if (counter > 10)
f.cancel(true);// 取消任务的执行
if (counter > 50)
break;
}
}
}
}
public class Test31 {
public static void main(String[] args) throws InterruptedException, ExecutionException, TimeoutException {
Future f = new FutureTask(() -> {
int res = 0;
for (int i = 0; i < 1000; i++) {
Thread.sleep(10);
res += i;
}
return res;
});
if (f instanceof Runnable) {
new Thread((Runnable) f).start();
int counter = 0;
long start = System.currentTimeMillis();
// Object obj=f.get();
Object obj = f.get(5, TimeUnit.SECONDS);// 参数1为超时时长,参数2为时长的单位,是一个枚举类型数据,超时TimeoutException
long end = System.currentTimeMillis();
System.out.println("get...执行时间为:" + (end - start) + "ms");
System.out.println("线程执行结果为:" + obj);
}
}
}
FutureTask
具体使用
FutureTask<Integer> future = new FutureTask<Integer>(callable);
new Thread(future).start();
FutureTask实现了两个接口,Runnable和Future,所以它既可以作为Runnable被线程执行,又可以作为Future得到Callable的返回值
FutureTask是一个可取消的异步计算,FutureTask实现了Future的基本方法,提供startcancel 操作,可以查询计算是否已经完成,并且可以获取计算的结果。结果只可以在计算完成之后获取,get方法会阻塞当计算没有完成的时候,一旦计算已经完成, 那么计算就不能再次启动或是取消。
一个FutureTask 可以用来包装一个 Callable 或是一个Runnable对象。因为FurtureTask实现了Runnable方法,所以一个 FutureTask可以提交(submit)给一个Excutor执行(excution)。它同时实现了Callable, 所以也可以作为Future得到Callable的返回值。