1.线程的六种状态.
线程的生命周期主要有以下六种状态:
1. New(新创建).
2. Runnable(可运行).
3. Blocked(被阻塞).
4. Waiting(等待).
5. Timed Waiting(计时等待).
6. Terminated(被终止)
2.多线程编程套路3步:
第一步:创建被多线程竞争的资源类 ,包括类的属性和操作方法
第二步:细化操作方法的实现:
(1)判断
(2)干活
(3)释放锁
第三步:创建多个线程,调用资源类的操作方法
/**
* User: ldj
* Date: 2023/3/3
* Time: 10:28
* Description: 切记不要使用if判断 会出现虚假唤醒 (它哪里睡在哪里醒,会跳过if判断)
*/
public class ThreadTest {
public static void main(String[] args) {
//创建2个线程调用资源类操作方法
SourceClass sourceClass = new SourceClass();
new Thread(()->{
for (int i = 0; i < 5; i++) {
try {
sourceClass.decr();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
},"A").start();
new Thread(()->{
for (int i = 0; i < 5; i++) {
try {
sourceClass.incr();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
},"B").start();
}
}
/**
* 资源类
*/
class SourceClass {
private Integer num = 0;
public synchronized void incr() throws InterruptedException {
//1.判断
while (num == 1) {
//等待其它线程-1变0
this.wait();
}
//2.干活
num = num + 1;
System.out.print(num);
//3.释放锁 唤醒其它线程
this.notifyAll();
}
public synchronized void decr() throws InterruptedException {
//1.判断
while (num == 0) {
//等待其它线程+1变1
this.wait();
}
//2.干活
num = num - 1;
System.out.print(num);
//3.释放锁 唤醒其它线程
this.notifyAll();
}
}
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
/**
* User: ldj
* Date: 2023/3/3
* Time: 11:21
* Description: No Description
*/
public class ThreadTest2 {
public static void main(String[] args) {
//创建2个线程调用资源类操作方法
SourceClass sourceClass = new SourceClass();
new Thread(() -> {
for (int i = 0; i < 5; i++) {
try {
sourceClass.decr();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}, "A").start();
new Thread(() -> {
for (int i = 0; i < 5; i++) {
try {
sourceClass.incr();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}, "B").start();
}
}
/**
* 资源类
*/
class SourceClass2 {
private Integer num = 0;
private Lock lock = new ReentrantLock(false);
private Condition condition = lock.newCondition();
public void incr() throws InterruptedException {
lock.lock();
try {
while (num == 1) {
condition.await();
}
num = num + 1;
System.out.print(num);
} finally {
lock.unlock();
}
}
public void decr() throws InterruptedException {
lock.unlock();
try {
while (num == 0) {
condition.await();
}
num = num - 1;
System.out.print(num);
} finally {
lock.unlock();
}
}
}
3.创建线程4种分类
import java.util.concurrent.*;
/**
* User: ldj
* Date: 2023/3/2
* Time: 8:30
* Description: 创建线程方式本质是一种 都是new Thread(**),根据传入的参数类型划分
*/
public class CreateThread {
public static void main(String[] args) throws ExecutionException, InterruptedException {
//1.继承
new MyThread().start();
//2.实现Runnable
new Thread(new MyRunnable()).start();
new Thread(()->System.out.println("写法2:通过实现Runnable方式创建线程")).start();
//3.实现Callable (FutureTask implements RunnableFuture extends Runnable, Future),结论:FutureTask 也算属于 Runnable
new Thread(new FutureTask<Void>(new MyCallable())).start();
new Thread(new FutureTask<Void>(()->{
System.out.println("写法2:通过实现Callable方式创建线程");
return null;})).start();
//4.线程池的线程工厂
//4.1 创建一个固定的线程池的大小
ExecutorService executorService = Executors.newFixedThreadPool(5);
//4.2 提交任务,并用 Future提交返回结果
Future< Integer > future = executorService.submit(new MyCallable2());
System.out.println(future.get());
}
}
class MyThread extends Thread {
@Override
public void run() {
System.out.println("通过继承 Thread 类创建线程");
}
}
class MyRunnable implements Runnable{
@Override
public void run() {
System.out.println("写法1:通过实现Runnable方式创建线程");
}
}
class MyCallable implements Callable<Void>{
@Override
public Void call() throws Exception {
System.out.println("写法1:通过实现Callable方式创建线程");
return null;
}
}
class MyCallable2 implements Callable<Integer>{
@Override
public Integer call() throws Exception {
System.out.println("通过线程池+Callable方式创建线程并返回处理结果");
return 666688;
}
}