一、继承 Thread 类
- 继续 Thread 的子类,需要用到的方法:
方法名 | 说明 |
---|---|
void run() | 在线程开启后,此方法将被调用执行,不能直接调用该方法实现多线程 |
void start() | 使此方法开启一个新线程并开始执行,Java虚拟机会自动调用 run方法 |
-
实现步骤:
(1) 定义一个类MyThread继承Thread类 (2) 在MyThread类中重写run()方法 (3) 创建MyThread类的对象 (4) 启动线程
示例
public class MyThread extends Thread {
private String name;
public MyThread(String name) {
this.name = name;
}
@Override
public void run() {
for (int i = 0; i < 10; i++) {
System.out.println(Thread.currentThread().getName() + ":" + i + ":" + name);
}
}
}
public static void main(String[] args) {
MyThread myt1 = new MyThread("a");
MyThread myt2 = new MyThread("b");
myt1.start();
myt2.start();
}
二、实现 Runnable 接口
- 实现 Runnable 的方法,需要用到的方法:
方法名 | 说明 |
---|---|
Thread(Runnable target) | 传入实现了 Runnable 接口的类,构造一个 Thread 对象 |
Thread(Runnable target, String name) | 传入实现了 Runnable 接口的类,构造一个名称为 name 的 Thread 对象 |
-
实现步骤:
(1) 定义一个类 MyRunnable 实现 Runnable 接口 (2) 在 MyRunnable 类中实现 run() 方法 (3) 创建 MyRunnable 类的对象 (4) 创建 Thread 类的对象,把 MyRunnable 对象作为构造方法的参数 (5) 启动线程
示例
public class MyRunnable implements Runnable {
@Override
public void run() {
for(int i=0; i<10; i++) {
System.out.println(Thread.currentThread().getName() + ":" + i);
}
}
}
public static void main(String[] args) {
MyRunnable myr1 = new MyRunnable();
MyRunnable myr2 = new MyRunnable();
Thread t1 = new Thread(myr1,"线程1");
Thread t2 = new Thread(myr2,"线程2");
t1.start();
t2.start();
}
三、实现 Callable 接口
- 实现 Callable 的方法,需要用到的方法:
方法名 | 说明 |
---|---|
V call() | 这是 Callable 接口中要实现的方法,相当于 Runnable 接口中的 run 方法 |
FutureTask(Callable<V> callable) | 使用 Callable 接口实现类实例创建一个 FutureTask,它运行时会调配用 Callable 接口中的 call 方法 |
V get() | FutureTask 实例的 get 方法,可以阻塞代码继续往下执行,直到获取到异步线程中的返回结果为止 |
-
实现步骤:
(1) 定义一个类 MyCallable 实现 Callable 接口 (2) 在 MyCallable 类中重实现 call() 方法 (3) 创建 MyCallable 类的对象 (4) 创建 Thread 类的对象,把 FutureTask 对象作为构造方法的参数 (5) 启动线程 (6) 如果想获取返回值的话,可以调用get方法,就可以获取线程结束之后的结果
示例
public class MyCallable implements Callable<String> {
@Override
public String call() throws Exception {
for (int i = 0; i < 100; i++) {
System.out.println(Thread.currentThread().getName() + ":" + i);
}
//这里返回一个字符串
return "success";
}
}
public static void main(String[] args) {
MyCallable mc = new MyCallable();
//因为 MyCallable 实现了 String 类型的 Callable 接口
//所以返回值也是 String 类型,所以创建的是 String 类型的 FutureTask 对象
FutureTask<String> ft = new FutureTask<>(mc);
//传入 FutureTask 实例,创建线程对象
Thread ct1 = new Thread(ft);
//不能在这个地方使用 FutureTask 的 get 方法获取异步线程的返回值,否则程序将卡死在这里。
//因为 ct1 线程还没有执行,所以无法获取到返回值,所以如果执行 get 方法,程序将卡死在这里。
//String s = ft.get();
//开启新线程,异步执行 MyCallable 实例中的 call 方法逻辑
ct1.start();
//可以做一些其它比较耗时的任务
// TODO
//获取异步线程的返回值
String s = ft.get();
}
四、总结
1 继承 Thread 类这种实现方式,实现比较简单,但是扩展性差,因为类只能单继承。
2 实现 Runnable 接口、Callable 接口这两种实现方式,实现比较复杂,但是扩展性比较强。
3 如果想要获取到异步线程中的返回值的话,可以采用实现 Callable 接口这种实现方式。