java 多线程Callable和Runable执行顺序问题详解
详见:http://blog.yemou.net/article/query/info/tytfjhfascvhzxcyt125
毫无疑问 Runnable会进行异步执行,此处不多说,主要说明Callable的使用,看实例:
1、
1
2
3
4
5
6
7
8
9
10
11
|
public class ThreadTest { public static void main(String[] args) throws InterruptedException, ExecutionException { ExecutorService executor = Executors.newFixedThreadPool( 4 ); MyTread m1 = new MyTread(); Future f = executor.submit(m1); // System.out.println(f.get()); executor.shutdown(); System.out.println( "主线程执行完了" ); } } |
1
2
3
4
5
6
7
8
9
10
11
12
13
|
class MyTread implements Callable<String> { @Override public String call() { try { System.out.println( "线程调度:" + Thread.currentThread()); TimeUnit.SECONDS.sleep( 3 ); } catch (InterruptedException e) { e.printStackTrace(); } return "123" ; } } |
此程序虽然获取了call方法的返回值,但是没有做处理,所以主线程main和m1同时执行,执行结果如下:
主线程执行完了
线程调度:Thread[pool-1-thread-1,5,main]
2、
1
2
3
4
5
6
7
8
9
10
11
|
public class ThreadTest { public static void main(String[] args) throws InterruptedException, ExecutionException { ExecutorService executor = Executors.newFixedThreadPool( 4 ); MyTread m1 = new MyTread(); Future f = executor.submit(m1); System.out.println(f.get()); // 进行了输出 executor.shutdown(); System.out.println( "主线程执行完了" ); } } |
1
2
3
4
5
6
7
8
9
10
11
12
|
class MyTread implements Callable<String> { @Override public String call() { try { System.out.println( "线程调度:" + Thread.currentThread()); TimeUnit.SECONDS.sleep( 3 ); } catch (InterruptedException e) { e.printStackTrace(); } return "123" ; } } |
在2中,对m1中call方法的返回值在main中进行了处理(输出),所以在此种情况下,main需要等待m1执行完,再继续执行,执行结果如下
线程调度:Thread[pool-1-thread-1,5,main]
123
主线程执行完了
3、再看当主线程中同时启动两个由Callable生成的线程时
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
public class ThreadTest { public static void main(String[] args) throws InterruptedException, ExecutionException { ExecutorService executor = Executors.newFixedThreadPool( 4 ); MyTread m1 = new MyTread(); MyTread2 m2 = new MyTread2(); Long time1 = TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis()); Future f = executor.submit(m1); Future f2 = executor.submit(m2); System.out.println(f.get()); // 进行了输出m1 System.out.println(f2.get()); // 进行了输出m2 executor.shutdown(); System.out.println( "主线程执行完了" ); Long time2 = TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis()); System.out.println( "主线程等待了" + (time2 - time1) + "秒" ); } } |
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
|
class MyTread implements Callable<String> { @Override public String call() { try { System.out.println( "线程调度:" + Thread.currentThread()); TimeUnit.SECONDS.sleep( 3 ); } catch (InterruptedException e) { e.printStackTrace(); } return "123" ; } } class MyTread2 implements Callable<String> { @Override public String call() { try { System.out.println( "线程调度2:" + Thread.currentThread()); TimeUnit.SECONDS.sleep( 3 ); } catch (InterruptedException e) { e.printStackTrace(); } return "abc" ; } } |
当不对m1和m2做输出时,main和m1、m2并发执行,当对m1和m2中任意一个的返回值进行处理的时候,main需要等待,但是m1和m2之前仍然是并发执行,执行结果如下:
线程调度2:Thread[pool-1-thread-2,5,main]
线程调度:Thread[pool-1-thread-1,5,main]
123
abc
主线程执行完了
主线程等待了3秒
博客已转移至 http://blog.yemou.net/