- A+
在前面的文章中我们讲述了创建线程的2种方式,一种是直接继承Thread,另外一种就是实现Runnable接口。
一.Callable与Runnable
1
2
3
|
public interface Runnable { public abstract void run(); } |
1
2
3
4
5
6
7
8
9
|
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; } |
1
2
3
|
<T> Future<T> submit(Callable<T> task); <T> Future<T> submit(Runnable task, T result); Future<?> submit(Runnable task); |
二.Future
1
2
3
4
5
6
7
8
|
public interface Future<V> { boolean cancel( boolean mayInterruptIfRunning); boolean isCancelled(); boolean isDone(); V get() throws InterruptedException, ExecutionException; V get( long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException; } |
-
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)用来获取执行结果,如果在指定时间内,还没获取到结果,就直接返回null。
三.FutureTask
1
|
public class FutureTask<V> implements RunnableFuture<V> |
1
2
3
|
public interface RunnableFuture<V> extends Runnable, Future<V> { void run(); } |
1
2
3
4
|
public FutureTask(Callable<V> callable) { } public FutureTask(Runnable runnable, V result) { } |
四.使用示例
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
public class Test { public static void main(String[] args) { ExecutorService executor = Executors.newCachedThreadPool(); Task task = new Task(); Future<Integer> result = executor.submit(task); executor.shutdown(); try { Thread.sleep( 1000 ); } catch (InterruptedException e1) { e1.printStackTrace(); } System.out.println( "主线程在执行任务" ); try { System.out.println( "task运行结果" +result.get()); } catch (InterruptedException e) { e.printStackTrace(); } catch (ExecutionException e) { e.printStackTrace(); } System.out.println( "所有任务执行完毕" ); } } class Task implements Callable<Integer>{ @Override public Integer call() throws Exception { System.out.println( "子线程在进行计算" ); Thread.sleep( 3000 ); int sum = 0 ; for ( int i= 0 ;i< 100 ;i++) sum += i; return sum; } } |
View Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
|
public class Test { public static void main(String[] args) { //第一种方式 ExecutorService executor = Executors.newCachedThreadPool(); Task task = new Task(); FutureTask<Integer> futureTask = new FutureTask<Integer>(task); executor.submit(futureTask); executor.shutdown(); //第二种方式,注意这种方式和第一种方式效果是类似的,只不过一个使用的是ExecutorService,一个使用的是Thread /*Task task = new Task(); FutureTask<Integer> futureTask = new FutureTask<Integer>(task); Thread thread = new Thread(futureTask); thread.start();*/ try { Thread.sleep( 1000 ); } catch (InterruptedException e1) { e1.printStackTrace(); } System.out.println( "主线程在执行任务" ); try { System.out.println( "task运行结果" +futureTask.get()); } catch (InterruptedException e) { e.printStackTrace(); } catch (ExecutionException e) { e.printStackTrace(); } System.out.println( "所有任务执行完毕" ); } } class Task implements Callable<Integer>{ @Override public Integer call() throws Exception { System.out.println( "子线程在进行计算" ); Thread.sleep( 3000 ); int sum = 0 ; for ( int i= 0 ;i< 100 ;i++) sum += i; return sum; } } |