一、并发编程之多线程的实现
1. 常用的多线程的实现方式
整体说来多线程的实现方式就只有两种,一个是实现runnable接口,实现其run方法;另一个是继承Thread类,重写run方法。其他的创建方式,大多是实现runnable接口的形式去实现的,线程池、callable接口原理都是实现runnable接口的方式。
以下代码涵盖了目前常用的多线程创建方式:
package concurrents.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.FutureTask;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
/**
* @author hjf
* @date 2022-05-17 10:39
*/
public class ThreadDemo {
public static void main(String[] args) {
//①继承Thread类
MyThread thread1 = new MyThread();
MyThread thread2 = new MyThread();
thread1.start();
thread2.start();
//②实现runnable接口
Thread thread3 = new Thread(new MyThreadWithRunnable());
Thread thread4 = new Thread(new MyThreadWithRunnable());
thread3.start();
thread4.start();
//③匿名内部类
Thread thread5 = new Thread(new Runnable() {
@Override
public void run() {
System.out.println(Thread.currentThread().getName() + ":开始工作");
}
});
Thread thread6 = new Thread(() -> {
System.out.println(Thread.currentThread().getName() + ":开始工作");
});
thread5.start();
thread6.start();
//④线程池
ThreadPoolExecutor executor = new ThreadPoolExecutor(3, 3, 10,
TimeUnit.SECONDS, new LinkedBlockingQueue<>());
for (int i = 0; i < 5; i++) {
executor.execute(() -> {
System.out.println(Thread.currentThread().getName() + ":开始工作");
});
}
executor.shutdown();
ExecutorService executorService = Executors.newFixedThreadPool(3);
for (int i = 0; i < 5; i++) {
executorService.execute(() -> {
System.out.println(Thread.currentThread().getName() + ":开始工作");
});
}
executorService.shutdown();
//⑤实现Callable接口,携带返回值
FutureTask futureTask = new FutureTask(new MyCallable());
Thread thread7 = new Thread(futureTask);
thread7.start();
try {
String result = (String) futureTask.get();
System.out.println(result);
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
System.out.println("--------主线程执行-------");
}
/**
* 集成Thread类,并重写其run方法
*/
static class MyThread extends Thread {
@Override
public void run() {
System.out.println(Thread.currentThread().getName() + ":开始工作");
}
}
/**
* 实现Runnable接口,并实现其run方法
*/
static class MyThreadWithRunnable implements Runnable {
@Override
public void run() {
System.out.println(Thread.currentThread().getName() + ":开始工作");
}
}
/**
* 实现Callable接口
*/
static class MyCallable implements Callable{
@Override
public String call() throws Exception {
System.out.println(Thread.currentThread().getName() + ":开始工作");
return Thread.currentThread().getName();
}
}
}
2. 线程的状态
查看Thread源码,其给线程定义了六种状态
public static enum State {
/**
* 开始状态
*/
NEW,
/**
* 可运行状态
*/
RUNNABLE,
/**
* 阻塞状态
*/
BLOCKED,
/**
* 等待状态
*/
WAITING,
/**
* 限时等待状态
*/
TIMED_WAITING,
/**
* 终止状态
*/
TERMINATED;
private State() {
}
}
状态转换图
线程状态代码切换演示:
package concurrents.thread;
/**
* @author hjf
* @date 2022-05-17 16:18
*/
public class ThreadStateDemo {
public static void main(String[] args) throws InterruptedException {
Thread thread = new Thread(() -> {
System.out.println("2. 线程当前状态:"+ Thread.currentThread().getState());
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("4. 线程当前状态:"+ Thread.currentThread().getState());
});
System.out.println("1. 线程当前状态:"+thread.getState());
thread.start();
//等待子线程运行
Thread.sleep(200);
System.out.println("3. 线程当前状态:"+thread.getState());
//等待子线程sleep结束
Thread.sleep(2000);
System.out.println("5. 线程当前状态:"+thread.getState());
}
}
打印结果为:
1. 线程当前状态:NEW
2. 线程当前状态:RUNNABLE
3. 线程当前状态:TIMED_WAITING
4. 线程当前状态:RUNNABLE
5. 线程当前状态:TERMINATED