创建线程的第三种方式:实现Callable接口
程序员文章站
2022-07-10 18:15:40
...
以前创建线程有两种方式:一种是继承Thread类,重写run()方法;另一种就是实现runnable接口的run()方法。我们先来看看这两种方式的实现。
1、继承Thread类
package com.jdk8.Thread;
public class ThreadDemo extends Thread{
@Override
public void run() {
for(int i = 0 ; i < 5 ; i++){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName());
}
}
public static void main(String[] args) {
ThreadDemo td = new ThreadDemo();
td.start();
System.out.println("main thread");
}
}
运行结果:
main thread
Thread-0
Thread-0
Thread-0
Thread-0
Thread-0
2、实现Runnable接口
package com.jdk8.Thread;
public class RunnableDemo implements Runnable {
public static void main(String[] args) {
new Thread(new RunnableDemo()).start();
System.out.println("main thread");
}
@Override
public void run() {
for (int i = 0; i < 5; i++) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName());
}
}
}
运行结果:
main thread
Thread-0
Thread-0
Thread-0
Thread-0
Thread-0
以上两种创建线程的方式,run()方法都是void,不能返回值。
Callable接口自带泛型,call()方法的返回值要跟接口的泛型一致
3、实现Callable接口,结合Future
package com.jdk8.Thread;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
public class CallableDemo implements Callable<String> {
@Override
public String call() throws Exception {
for (int i = 0; i < 5; i++) {
Thread.sleep(1000);
System.out.println(Thread.currentThread().getName() + ":" + i);
}
return "over";
}
public static void main(String[] args) {
ExecutorService pool = Executors.newCachedThreadPool();
Future<String> f = pool.submit(new CallableDemo());
try {
System.out.println(f.get());
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
} finally {
pool.shutdown();
}
System.out.println("main thread");
}
}
运行结果:
pool-1-thread-1:0
pool-1-thread-1:1
pool-1-thread-1:2
pool-1-thread-1:3
pool-1-thread-1:4
over
main thread
从上面的结果可以看出,f.get()方法可以获取到线程的返回结果,并且会阻塞当前线程,直到线程执行完拿到返回结果之后,main线程才继续执行。