一、Java多线程基础
1. 多线程的基础
1.1 什么是并发与并行
要想学习多线程,必须先理解什么是并发与并行:
- 并行:指两个或多个事件在同一时刻发生(同时发生)。
- 并发:指两个或多个事件在同一个时间段内发生。
1.2 什么是进程、线程
进程:
- 进程是正在运行的程序的实例。
- 进程是线程的容器,即一个进程中可以开启多个线程。
- 比如打开一个浏览器、打开一个word等操作,都会创建进程。
线程:
- 线程是进程内部的一个独立执行单元;
- 一个进程可以同时并发运行多个线程;
- 比如进程可以理解为医院,线程是挂号、就诊、缴费、拿药等业务活动
多线程:多个线程并发执行。
2. 线程的创建
Java中线程有四种创建方式:
- 继承Thread类
- 实现Runnable接口
- 实现Callable接口
- 线程池
2.1 继承Thread类
import java.util.Date;
// 测试类
public class ThreadCreateDemo {
public static void main(String[] args){
//1.创建自定义线程
MyThread thread = new MyThread();
thread.start();
//2.主线程循环打印
for (int i=0; i<10; i++){
System.out.println("main主线程正在执行:"+new Date().getTime());
}
}
}
// 创建自定义线程类
class MyThread extends Thread {
@Override
public void run() {
for (int i = 0; i<10; i++){
System.out.println("mythread线程正在执行:"+new Date().getTime());
}
}
}
运行结果:
2.2 实现Runnable接口
示例代码:
import java.util.Date;
import com.multithread.thread.MyRunable;
import com.multithread.thread.MyThread;
import java.util.Date;
public class ThreadCreateDemo {
public static void main(String[] args){
//1.创建自定义线程
Thread thread = new Thread(new MyRunable());
thread.start();
//2.主线程循环打印
for (int i=0; i<10; i++){
System.out.println("main主线程正在执行:"+new Date().getTime());
}
}
}
class MyRunable implements Runnable {
public void run() {
for (int i=0; i<10; i++){
System.out.println("MyRunnable线程正在执行:"+new Date().getTime());
}
}
}
2.3 实现Callable接口并
Callable需要使用FutureTask类帮助执行,FutureTask类结构如下:
Future接口:
判断任务是否完成:isDone()
能够中断任务:cancel()
能够获取任务执行结果:get()
示例代码:
import java.util.Date;
import java.util.concurrent.Callable;
import com.multithread.thread.MyCallable;
import com.multithread.thread.MyRunable;
import com.multithread.thread.MyThread;
import java.util.Date;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
public class ThreadCreateDemo {
public static void main(String[] args) throws Execution{
FutureTask task = new FutureTask(new MyCallable());
Thread thread = new Thread(task);
thread.start();
for (int i=0; i<10; i++){
System.out.println("main主线程正在执行:"+new Date().getTime());
}
System.out.println(task.get());
}
}
class MyCallable implements Callable<String> {
public String call() throws Exception {
for (int i=0; i<10; i++){
System.out.println("MyCallable正在执行:"+new Date().getTime());
}
return "MyCallable执行完毕!";
}
}
运行结果:
2.4 线程池-Executor(初步了解)
示例代码(Runable):
import java.util.Date;
import com.multithread.thread.MyCallable;
import com.multithread.thread.MyRunable;
import com.multithread.thread.MyThread;
import java.util.Date;
import java.util.concurrent.*;
public class ThreadCreateDemo {
public static void main(String[] args) throws ExecutionException, InterruptedException {
//1.使用Executors创建线程池
ExecutorService executorService = Executors.newFixedThreadPool(10);
//2.通过线程池执行线程
executorService.execute(new MyRunable());
//3.主线程循环打印
for (int i=0; i<10; i++){
System.out.println("main主线程正在执行:"+new Date().getTime());
}
}
}
class MyRunable implements Runnable {
public void run() {
for (int i=0; i<10; i++){
System.out.println("MyRunnable线程正在执行:"+new Date().getTime());
}
}
}
2.5 小结:
Runable接口、Callable接口、Thread类的区别
- 接口更适合多个相同的程序代码的线程去共享同一个资源。
- 接口可以避免java中的单继承的局限性。
- 接口代码可以被多个线程共享,代码和线程独立。
- 线程池只能放入实现Runable或Callable接口的线程,不能直接放入继承Thread的类。
注意(扩充):
在java中,每次程序运行至少启动2个线程。一个是main线程,一个是垃圾收集线程。
3. 线程池创建-Executor
线程池线类关系图
Executor接口:
- 声明了execute(Runnable runnable)方法,执行任务代码
ExecutorService接口:
- 继承Executor接口,声明方法:submit、invokeAll、invokeAny以及shutDown等
AbstractExecutorService抽象类:
- 实现ExecutorService接口,基本实现ExecutorService中声明的所有方法
ScheduledExecutorService接口:
- 继承ExecutorService接口,声明定时执行任务方法
ThreadPoolExecutor类:
- 继承类AbstractExecutorService,实现execute、submit、shutdown、shutdownNow方法
ScheduledThreadPoolExecutor类:
- 继承ThreadPoolExecutor类,实现ScheduledExecutorService接口并实现其中的方法
Executors类:
- 提供快速创建线程池的方法