java创建线程的三种方式
- 实现Runnable,重写run方法
- 继承Thread,重写run方法
- 实现Callable接口,重写call方法
前两种不能返回执行结果,如果需要,必须通过共享变量等线程通信方式获取,比较麻烦。通过Callable 和Future ,可以得到认知执行完毕之后的结果。Callable定义了执行的任务和返回的结果。Future 用来(1)获取返回的结果;(2)判断是否执行完成;(3)取消任务。
@FunctionalInterface
public interface Callable<V> {
/**
* Computes a result, or throws an exception if unable to do so.
*
* @return computed result
* @throws Exception if unable to compute a result
*/
V call() throws Exception;
}
Callable 的使用
- ExecutorService 中有若干个submit()方法,可以执行Callable,得到返回的Future
<T> Future<T> submit(Callable<T> task);
demo:
package com.sting.jdk;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.FutureTask;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
/**
* @author susq
* @since 2019-01-12
*/
public class CallableTest {
public static void main(String[] args) {
// 要执行的任务
Task task1 = new Task();
ExecutorService executorService = Executors.newFixedThreadPool(2);
Future<String> future = executorService.submit(task1);
while (true) {
if (future.isDone()) {
try {
String res1 = future.get();
System.out.println(res1);
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
}
return;
}
}
}
static class Task implements Callable<String> {
@Override
public String call() throws Exception {
return "模拟执行了一段操作得到了一个字符串" + Thread.currentThread().getName();
}
}
}
- 配合Future 接口的实现类 FutureTask 使用
demo:
package com.sting.jdk;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.FutureTask;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
/**
* @author susq
* @since 2019-01-12
*/
public class CallableTest {
public static void main(String[] args) {
// 要执行的任务
Task task1 = new Task();
Task task2 = new Task();
FutureTask<String> future1 = new FutureTask<>(task1);
FutureTask<String> future2 = new FutureTask<>(task2);
ExecutorService executorService = Executors.newFixedThreadPool(2);
executorService.execute(future1);
executorService.execute(future2);
while (true) {
if (future1.isDone() && future2.isDone()) {
System.out.println("done !");
executorService.shutdown();
return;
}
try {
if (!future1.isDone()) {
// get()方法在线程没有执行完成的时候,会一直等待直到完成。
System.out.println(future1.get());
}
System.out.println("等线程2完成");
// get(timeout, timeunit) 方法会等待指定的时间,如果没有执行网返回null, 执行完了就返回执行结果
String res = future2.get(200L, TimeUnit.MILLISECONDS);
if (res != null) {
System.out.println("线程2执行结果:" + res);
} else {
System.out.println("线程2还没执行完");
}
} catch (InterruptedException | ExecutionException | TimeoutException e) {
e.printStackTrace();
}
}
}
static class Task implements Callable<String> {
@Override
public String call() throws Exception {
return "模拟执行了一段操作得到了一个字符串" + Thread.currentThread().getName();
}
}
}