码迷,mamicode.com
首页 > 编程语言 > 详细

线程池回顾

时间:2021-06-13 10:28:58      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:tar   turn   extend   pac   接口   void   static   integer   ble   

package cn.ruhsang.gaoji;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

//回顾线程池的创建
public class ThreadNew {
public static void main(String[] args) {
//1.第一个方法的调用
new MyThread1().start();
//2.第二个方法的调用
MyThread2 myThread2 = new MyThread2();
new Thread(myThread2).start();
//3.第三个方法的启动
FutureTask<Integer> futureTask = new FutureTask<Integer>(new MyThread3());
new Thread(futureTask).start();

try {
Integer integer = futureTask.get();
System.out.println(integer);
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}


}
}
//1.继承Thread类
class MyThread1 extends Thread{
@Override
public void run() {
System.out.println("MyThread1");
}
}
//2.实现Runnable接口
class MyThread2 implements Runnable{
@Override
public void run() {
System.out.println("MyThread2");
}
}
//3.实现callable接口
class MyThread3 implements Callable<Integer>{

@Override
public Integer call() throws Exception {
System.out.println("MyThread3");
return 100;
}
}

线程池回顾

标签:tar   turn   extend   pac   接口   void   static   integer   ble   

原文地址:https://www.cnblogs.com/nigustudent/p/14878610.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!