下载此文档

java线程池主线程等待子线程执行完成.doc


文档分类:IT计算机 | 页数:约13页 举报非法文档有奖
1/13
下载提示
  • 1.该资料是网友上传的,本站提供全文预览,预览什么样,下载就什么样。
  • 2.下载该文档所得收入归上传者、原创者。
  • 3.下载的文档,不会出现我们的网址水印。
1/13 下载此文档
文档列表 文档介绍
java线程池主线程等待子线程执行完成
public class Threads { static ExecutorService executorService = Executors . newFixedThreadPool ( 1 ) ;

***@SuppressWarnings ( “rawtypes” )
public static void main (String[] args ) throws InterruptedException , ExecutionException
{
SubThread thread = new SubThread () ;
// ();
Future future = executorService . submit (thread) ;
mainThreadOtherWork () ;
System . out . println ( “now
waiting sub thread done.” ) ;
future . get () ;
// try {
// ();
// } catch (InterruptedException e) {
// ();
// }
System . out . println ( “now
all done.” ) ;
executorService . shutdown () ;
} private static void mainThreadOtherWork ()
{
System . out . println ( “main
thread work start” ) ;
try {
Thread . sleep ( 3000L ) ;
} catch (InterruptedException e) {
e . printStackTrace () ;
}
System . out . println ( “main
thread work done.” ) ;
} public static class SubThread extends Thread{
***@Override
public void run ()
{
working () ;
} private void working ()
{
System . out . println ( “sub
thread start working.” ) ;
busy () ;
System . out . println ( “sub
thread stop working.” ) ;
} private void busy ()
{
try {
sleep ( 5000L ) ;
} catch (InterruptedException e) {
e . printStackTrace () ;
}
}

}

}这 里, ThreadPoolExecutor 是实现了 ExecutorService的方法, sumbit的过程就是把一个Runnable接口对象包装成一个 Callable接口对象, 然后放到 workQueue里等待调度执行. 当然, 执行的启动也是调用了thread的start来做到的, 只不过这里被包装掉了. 另外, 这里的thread是会

java线程池主线程等待子线程执行完成 来自淘豆网m.daumloan.com转载请标明出处.

非法内容举报中心
文档信息
  • 页数13
  • 收藏数0 收藏
  • 顶次数0
  • 上传人学习的一点
  • 文件大小42 KB
  • 时间2021-07-25