java 实现多线程的3种方式
1、第一种继承Thread类;
看jdk源码可以发现,Thread类其实是实现了Runnable接口的一个实例,继承Thread类后需要重写run方法并通过start方法启动线程。
继承Thread类耦合性太强了,因为java只能单继承,所以不利于扩展。
2、第二种实现Runnable接口;
通过实现Runnable接口并重写run方法,并把Runnable实例传给Thread对象,Thread的start方法调用run方法再通过调用Runnable实例的run方法启动线程。
所以如果主类继承了另外一个父类,此时要实现多线程就不能通过继承Thread的类实现;
3、第三种实现Callable接口;
通过实现Callable接口并重写call方法,并把Callable实例传给FutureTask对象,再把FutureTask对象传给Thread对象。它与Thread、Runnable最大的不同是Callable能返回一个异步处理的结果Future对象并能抛出异常,而其他两种不能。
代码如下:
package model;
import com.oracle.webservices.internal.api.databinding.DatabindingMode;
/**
* Author: trunk
* Date: 2021/2/9 15:00
* Describe: teacher类
*/
public class Teacher {
private String name;
private int age;
public Teacher(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString(){
return "name="+name+",age="+age;
}
}
import model.Teacher;
import java.util.concurrent.Callable;
import java.util.concurrent.FutureTask;
/**
* Author: trunk
* Date: 2021/2/9 14:50
* Describe:实现多线程的3种方式
*/
public class Asyn {
public static void main(String[] args) throws Exception{
//第一种
Thread t1 = new Thread1();
//第二种
Thread t2 = new Thread(new Thread2());
//第三种
FutureTask<Teacher> ft = new FutureTask<>(new Thread3());
Thread t3 = new Thread(ft);
t1.start();
t2.start();
t3.start();
System.out.println(ft.get());
}
static class Thread1 extends Thread {
@Override
public void run() {
System.out.println("First extends Thread");
}
}
static class Thread2 implements Runnable {
@Override
public void run() {
System.out.println("Second implements Runnable");
}
}
static class Thread3<T> implements Callable<T>{
@Override
public T call() throws Exception {
System.out.println("Three implements Callable");
return (T)new Teacher("name",32);
}
}
}