Java线程的创建方式
文章目录
1 继承Thread类
// 继承Thread类
public class ThreadTest extends Thread {
@Override
public void run() {
for (int i = 0; i < 10; i++) {
System.out.println(Thread.currentThread() + ":" + i);
}
}
public static void main(String[] args) {
ThreadTest t1 = new ThreadTest();
ThreadTest t2 = new ThreadTest();
ThreadTest t3 = new ThreadTest();
t1.start();
t2.start();
t3.start();
}
}
2 实现Runnable接口
//实现Runnable接口
class ThreadTest1 implements Runnable {
@Override
public void run() {
for (int i = 0; i < 10; i++) {
System.out.println(Thread.currentThread() + ":" + i);
}
}
public static void main(String[] args) {
ThreadTest1 t1 = new ThreadTest1();
ThreadTest1 t2 = new ThreadTest1();
ThreadTest1 t3 = new ThreadTest1();
Thread thread1 = new Thread(t1);
Thread thread2 = new Thread(t2);
Thread thread3 = new Thread(t3);
thread1.start();
thread2.start();
thread3.start();
}
}
3 实现Callable接口
3.1 Callable接口和Runnable接口的区别
1、Callable接口规定的方法是call方法,Runnable接口规定的方法是run方法;
2、Callable任务执行后有返回值,Runnable任务执行没有返回值;
3、Callable的call方法可以抛出异常,Runnable的run方法不会抛出异常;
4、通过Callable任务可以拿到一个Future对象,表示异步计算的结果,通过Future可以得到计算的结果,判断是否执行完成,还可以取消任务。
3.2 Callable的两种执行方式
3.2.1 使用FutureTask类执行
FutureTask类实现了RunnableFuture接口,RunnableFuture接口继承了Runnable接口和Future接口,所以FutureTask是一个提供异步计算结果的任务。FutureTask的继承关系如下。
使用FutureTask执行Callable任务代码如下:
class CallableTest implements Callable<String> {
private String name;
public CallableTest(String name) {
this.name = name;
}
@Override
public String call() throws Exception {
return name;
}
public static void main(String[] args) {
Callable callable = new CallableTest("name");
FutureTask futureTask = new FutureTask(callable);
Thread thread = new Thread(futureTask);
thread.start();
try {
System.out.println(futureTask.get().toString());
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
}
}
3.2.1 使用线程池
class CallableTest implements Callable<String> {
private String name;
public CallableTest(String name) {
this.name = name;
}
@Override
public String call() throws Exception {
return name;
}
public static void main(String[] args) {
//创建一个大小为5的线程池
ExecutorService pool = Executors.newFixedThreadPool(5);
List<Future> futures = new ArrayList<>();
// 创建Callable对象,通过线程池提交任务,并用Future获取执行结果
for (int i = 0; i < 5; i++) {
Callable c = new CallableTest("name" + i);
Future future = pool.submit(c);
futures.add(future);
}
for (int i = 0; i < futures.size(); i++) {
try {
System.out.println(futures.get(i).get().toString());
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
}
}
}
4 基于线程池创建
class ExecutorTest implements Runnable {
private String name;
public ExecutorTest(String name) {
this.name = name;
}
@Override
public void run() {
System.out.println(name);
}
public static void main(String[] args) {
// 提交多个线程任务并执行
ExecutorService pool = Executors.newFixedThreadPool(10);
for (int i = 0; i < 10; i++) {
pool.submit(new ExecutorTest("name" + i));
}
pool.shutdown();
}
}