在JAVA中实现多线程有三种方式:
一、继承 Thread 类
直接继承 Thread 类是最简单的多线程实现方式,你可以根据实际需求新建出多种不同的子类线程,并重写他们的 run 方式来实现不同类型的线程干不同的事情,并让他们有规划的相互合作。
优点: 直接继承就可以使用,代码简单清晰
缺点: 由于 JAVA 是单继承的,继承了 Thread 类就无法继承其他类
新建类: MainThread.java
/**
* @author tom
* @date 2019-11-01 09:37
* @Description 通过继承 Thread 类来创建线程
*/
public class MainThread extends Thread{
public static void main(String[] args) {
// 新建了一个 MainThread 的类实例,这个类继承了 Thread
MainThread thread1 = new MainThread();
// 设置这个实例的名称(可重复)
thread1.setName("tom thread");
// 启动线程,此时线程开始跑 run 方法
thread1.start();
System.out.println("main over");
// 运行 main 函数的线程叫主线程,循环打印主线程的内容
// 由于主线程和其他线程存在竞争关系,故可以在打印台看到两种线程存在交互情况
for (int i=0; i<50; i++) {
System.out.println(Thread.currentThread().getName() + " i = " + i);
}
}
/**
* @author tom
* @date 2019/11/1 9:39
* @params
* @return
* @Description 继承了 Thread 类,必须重写 run 方法,来让这一类的线程做自己想要让他做的事情
*/
@Override
public void run() {
// 循环打印出这个线程的名字
for (int i=0; i<50; i++) {
System.out.println(Thread.currentThread().getName() + " i = " + i);
}
}
}
运行类中的 main 方法,会得到:
可以看到存在 主线程 main 和 子线程 tom 的打印日志是交互显示的,证明线程之间是交互执行的,而不是同时并行执行。若同时并行执行,应是 main 一条, tom 一条。
......
tom thread i = 14
tom thread i = 15
tom thread i = 16
main i = 22
main i = 23
main i = 24
main i = 25
main i = 26
main i = 27
main i = 28
tom thread i = 17
tom thread i = 18
main i = 29
main i = 30
main i = 31
main i = 32
main i = 33
main i = 34
main i = 35
main i = 36
main i = 37
tom thread i = 19
tom thread i = 20
tom thread i = 21
main i = 38
main i = 39
tom thread i = 22
tom thread i = 23
tom thread i = 24
tom thread i = 25
tom thread i = 26
tom thread i = 27
tom thread i = 28
......
二、实现 Runnable 接口
由于 JAVA 的单继承特性,继承了 Thread 的类无法再成为其他类的子类,而通过 Runnable 接口就可以解决这种情况,让我们的线程实现类可以继承其他类。
新建类: MainRunnable.java
/**
* @author tom
* @date 2019-11-01 09:59
* @Description 使用 Runnable 接口创建子线程
*/
public class MainRunnable implements Runnable {
public static void main(String[] args) {
// 创建 Runnable 实例
MainRunnable mRu = new MainRunnable();
// 创建 Thread 并传入 Runnable 实例
Thread th1 = new Thread(mRu);
// 设置子线程名称
th1.setName("children thread");
// 开启子线程
th1.start();
for (int i = 0; i < 50; i++) {
System.out.println(Thread.currentThread().getName() + " i = " + i);
}
}
/**
* @author tom
* @date 2019/11/1 10:00
* @params
* @return
* @Description this is a function
*/
@Override
public void run() {
// 打印子线程信息
for (int i = 0; i < 50; i++) {
System.out.println(Thread.currentThread().getName() + " i = " + i);
}
}
}
运行 main 方法,会得到:
......
children thread i = 30
children thread i = 31
children thread i = 32
main i = 19
main i = 20
children thread i = 33
children thread i = 34
children thread i = 35
main i = 21
main i = 22
main i = 23
main i = 24
main i = 25
main i = 26
main i = 27
main i = 28
main i = 29
main i = 30
main i = 31
main i = 32
main i = 33
main i = 34
main i = 35
children thread i = 36
children thread i = 37
children thread i = 38
children thread i = 39
children thread i = 40
children thread i = 41
main i = 36
main i = 37
main i = 38
main i = 39
main i = 40
main i = 41
children thread i = 42
children thread i = 43
children thread i = 44
children thread i = 45
children thread i = 46
children thread i = 47
children thread i = 48
children thread i = 49
main i = 42
main i = 43
main i = 44
main i = 45
......
三、继承 Callable 类
继承 Callable 类可以实现线程的回调以及抛出异常,而前面两种不能。
新建类: MainCallable.java
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
/**
* @author tom
* @date 2019-11-01 10:08
* @Description
*/
public class MainCallable implements Callable<Integer> {
public static void main(String[] args) {
// 构建 Callable 实例类
MainCallable mCall = new MainCallable();
// 通过 FutureTask 类来实现回调
// 需要指定回调类型,此时回调类型是 Integer,并将call实例类添加到 FutureTask 实例类中
FutureTask<Integer> mFutureTask = new FutureTask<>(mCall);
// 构建线程,并在构建时将 FutureTask 实例传入
Thread mThread = new Thread(mFutureTask);
// 设置子线程名
mThread.setName("children thread");
// 开启线程
mThread.start();
// 重要的一步,通过 FutureTask 实例的 get() 方法获取到线程返回的结果
try {
int count = mFutureTask.get();
System.out.println("callable count = " + count);
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
}
/**
* @author tom
* @date 2019/11/1 10:08
* @params
* @return
* @Description this is a function
*/
@Override
public Integer call() throws Exception {
int count = 0;
for (int i=0; i<50; i++) {
count++;
System.out.println(Thread.currentThread().getName() + " i = " + i);
}
return count;
}
}
运行 main 方法,会得到:
......
children thread i = 32
children thread i = 33
children thread i = 34
children thread i = 35
children thread i = 36
children thread i = 37
children thread i = 38
children thread i = 39
children thread i = 40
children thread i = 41
children thread i = 42
children thread i = 43
children thread i = 44
children thread i = 45
children thread i = 46
children thread i = 47
children thread i = 48
children thread i = 49
callable count = 50
可以看到最终 main 方法里拿到的返回值是50。