要求

分别是
其它情况(只需了解)
- 可以用 interrupt() 方法打断等待、有时限等待的线程,让它们恢复为可运行状态
- park,unpark 等方法也可以让线程等待和唤醒
这是从 操作系统 层面来描述的

【初始状态】仅是在语言层面创建了线程对象,还未与操作系统线程关联
【可运行状态】(就绪状态)指该线程已经被创建(与操作系统线程关联),可以由 CPU 调度执行
【运行状态】指获取了 CPU 时间片运行中的状态
【阻塞状态】
【终止状态】表示线程已经执行完毕,生命周期已经结束,不会再转换为其它状态
要求
七大参数
workQueue满了,救急线程数目也达到最大了,后面来任务直接抛异常 java.util.concurrent.ThreadPoolExecutor.AbortPolicy

workQueue满了,救急线程数目也达到最大了,新加的任务由调用者执行(谁去submit任务谁就去执行任务) java.util.concurrent.ThreadPoolExecutor.CallerRunsPolicy

workQueue满了,救急线程数目也达到最大了,后面来的任务之间丢弃。java.util.concurrent.ThreadPoolExecutor.DiscardPolicy

workQueue满了,救急线程数目也达到最大了,直接丢弃最早排队任务 (丢弃排队时间最长的任务,把新的任务添加到workQueue)java.util.concurrent.ThreadPoolExecutor.DiscardOldestPolicy

示例代码:

import org.slf4j.Logger;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import static day02.LoggerUtils.*;
public class TestThreadPoolExecutor {
public static void main(String[] args) throws InterruptedException {
AtomicInteger c = new AtomicInteger(1);
ArrayBlockingQueue<Runnable> queue = new ArrayBlockingQueue<>(2);
ThreadPoolExecutor threadPool = new ThreadPoolExecutor(
2,
3,
0,
TimeUnit.MILLISECONDS,
queue,
r -> new Thread(r, "myThread" + c.getAndIncrement()),
new ThreadPoolExecutor.DiscardOldestPolicy());
showState(queue, threadPool);
threadPool.submit(new MyTask("1", 3600000));
showState(queue, threadPool);
threadPool.submit(new MyTask("2", 3600000));
showState(queue, threadPool);
threadPool.submit(new MyTask("3"));
showState(queue, threadPool);
threadPool.submit(new MyTask("4"));
showState(queue, threadPool);
threadPool.submit(new MyTask("5", 3600000));
showState(queue, threadPool);
threadPool.submit(new MyTask("6"));
showState(queue, threadPool);
}
private static void showState(ArrayBlockingQueue<Runnable> queue, ThreadPoolExecutor threadPool) {
try {
Thread.sleep(300);
} catch (InterruptedException e) {
e.printStackTrace();
}
List<Object> tasks = new ArrayList<>();
for (Runnable runnable : queue) {
try {
Field callable = FutureTask.class.getDeclaredField("callable");
callable.setAccessible(true);
Object adapter = callable.get(runnable);
Class<?> clazz = Class.forName("java.util.concurrent.Executors$RunnableAdapter");
Field task = clazz.getDeclaredField("task");
task.setAccessible(true);
Object o = task.get(adapter);
tasks.add(o);
} catch (Exception e) {
e.printStackTrace();
}
}
main.debug("pool size: {}, queue: {}", threadPool.getPoolSize(), tasks);
}
static class MyTask implements Runnable {
private final String name;
private final long duration;
public MyTask(String name) {
this(name, 0);
}
public MyTask(String name, long duration) {
this.name = name;
this.duration = duration;
}
@Override
public void run() {
try {
LoggerUtils.get("myThread").debug("running..." + this);
Thread.sleep(duration);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
@Override
public String toString() {
return "MyTask(" + name + ")";
}
}
}
运行结果:

要求
一个共同点,三个不同点
共同点
不同点
方法归属不同
醒来时机不同
锁特性不同(重点)
下面是一些示例代码。
static final Object LOCK = new Object();
public static void main(String[] args) throws InterruptedException {
LOCK.wait();
}
运行结果:

static final Object LOCK = new Object();
public static void main(String[] args) throws InterruptedException {
synchronized (LOCK){
LOCK.wait();
}
}

示例代码:

运行结果:

示例代码:

运行结果:


运行结果:
