进程(Process)是计算机中的程序关于某数据集合上的一次运行活动,是系统进行资源分配和调度的基本单位;线程(thread)是操作系统能够进行运算调度的最小单位,被包含在进程之中,是进程中的实际运作单位,一个进程可以并发多个线程。
JUC 的核心在于:以锁、线程池与并发容器等工具,将复杂的线程协作封装为可复用的安全组件,从而让开发者无需直接操作底层同步原语即可编写正确的并发程序。
1. 概述
本文围绕 Java 并发编程中的 JUC(Java Util Concurrent)工具包展开,从线程的基本使用与同步机制入手,逐步深入到锁、Monitor、线程池与动态代理等核心组件。JUC 的核心在于以锁、线程池与并发容器等工具,将复杂的线程协作封装为可复用的安全组件,使开发者无需直接操作底层同步原语即可编写正确的并发程序。相比手动使用 synchronized、wait/notify 等底层原语容易出现死锁、竞态条件等问题,JUC 通过封装锁、线程池、阻塞队列等组件,大幅降低了并发编程的复杂度。
2. 线程的基本使用
1.1 创建线程
Java 中创建线程主要有三种方式,本质上都是将任务逻辑交给 Thread 执行。
| 方式 | 说明 | 示例 |
|---|---|---|
| 继承 Thread | 匿名子类重写 run() 方法 |
new Thread(){ public void run(){...} } |
| 实现 Runnable | 将任务逻辑封装为 Runnable 传入 |
new Thread(runnable, "test1") |
| Lambda 简化 | 使用 Lambda 表达式直接传入任务 | new Thread(()->{...}, "test2") |
创建线程方式 1(继承 Thread)
Thread t = new Thread(){
@Override
public void run() {
log.info("test");
}
};
t.start();
log.info("main");
创建线程方式 2(实现 Runnable)
Runnable runnable = new Runnable() {
@Override
public void run() {
log.info("test1");
}
};
Thread t = new Thread(runnable,"test1");
t.start();
log.info("main1");
创建线程方式 3(Lambda 简化)
new Thread(()->{
log.info("test2");
},"test2").start();
log.info("main2");
1.2 sleep 与 yield
1.3 同步应用:join
| 方法 | 说明 |
|---|---|
join() |
等待线程结束 |
join(long n) |
等待 n 毫秒,若仍未执行完成则不再等待 |
1.4 interrupt 打断机制
1.4.1 打断 sleep / wait / join 的线程
Thread t1 = new Thread(){
@Override
public void run() {
log.info("sleep");
try {
Thread.sleep(500);
}catch (InterruptedException e){
e.printStackTrace();
}
}
};
t1.setName("t1");
t1.start();
Thread.sleep(1000);
log.info("interrupter");
t1.interrupt();
log.info("打断标记{}",t1.isInterrupted());
运行输出:
[t1] INFO richuff.top.MyThread - sleep
[main] INFO richuff.top.MyThread - interrupter
java.lang.InterruptedException: sleep interrupted
at java.base/java.lang.Thread.sleep(Native Method)
at richuff.top.MyThread$1.run(MyThread.java:13)
[main] INFO richuff.top.MyThread - 打断标记false
1.4.2 打断正常线程
Thread t1 = new Thread(){
@Override
public void run() {
while (true){
}
}
}; t1.setName("t1");
t1.start();
Thread.sleep(1000);
log.info("interrupter");
t1.interrupt();
log.info("打断标记{}",t1.isInterrupted());
打断标记的用途
Thread t1 = new Thread(){
@Override
public void run() {
log.info("sleep");
while (true){
boolean interrupted = Thread.currentThread().isInterrupted();
if (interrupted){
log.info("quit");
break;
}
}
}
};
t1.setName("t1");
t1.start();
Thread.sleep(1000);
log.info("interrupter");
t1.interrupt();
log.info("打断标记{}",t1.isInterrupted());
运行输出:
[t1] INFO richuff.top.MyThread - sleep
[main] INFO richuff.top.MyThread - interrupter
[t1] INFO richuff.top.MyThread - quit
[main] INFO richuff.top.MyThread - 打断标记true
1.5 两阶段终止模式
两阶段终止(Two Phase Termination)模式用于优雅地停止一个运行中的线程:先发出停止信号,再等待线程在合适的检查点自行退出。
public class WatchThread {
public static void main(String[] args) throws Exception{
TwoPhaseTermination twoPhaseTermination = new TwoPhaseTermination();
twoPhaseTermination.start();
Thread.sleep(6000);
twoPhaseTermination.stop();
}
}
@Slf4j(topic = "TwoPhaseTermination")
class TwoPhaseTermination{
private Thread minitor;
public void start(){
minitor = new Thread(()->{
while (true){
Thread current = Thread.currentThread();
if (current.isInterrupted()){
log.info("处理后续事务");
break;
}
try{
Thread.sleep(1000); //在睡眠时被打断
log.info("处理逻辑"); //在处理逻辑时被打断
}catch (InterruptedException e){
e.printStackTrace();
current.interrupt();
}
}
},"minitor");
minitor.start();
}
public void stop(){
minitor.interrupt();
}
}
1.6 线程的五种状态
| 状态 | 说明 |
|---|---|
| 初始状态(NEW) | 仅在语言层面创建了对象,还未与操作系统相关联 |
| 可运行状态(RUNNABLE) | 可以由 CPU 进行调度 |
| 运行状态(RUNNING) | 指获取了 CPU 时间片中的状态 |
| 阻塞状态(BLOCKED) | 等待监视器锁等阻塞 |
| 终止状态(TERMINATED) | 线程执行完毕,生命周期已经结束 |
3. synchronized
synchronized 用于保证临界区代码的原子性与可见性,同一时刻只能有一个线程进入被保护的代码块。
public static int count = 0;
public static void main(String[] args) throws InterruptedException {
Thread t1 = new Thread(() -> {
for (int i = 0; i < 5000; i++) {
synchronized (sync.class) {
count++;
}
}
}, "t1");
Thread t2 = new Thread(()->{
for (int i=0;i<5000;i++){
synchronized (sync.class) {
count--;
}
}
},"t2");
t1.start();
t2.start();
t1.join();
t2.join();
System.out.println(count);
}
4. Monitor(锁)
每个 Java 对象都可以关联一个 Monitor 对象。
对象头中的 Mark Word 会随锁状态变化而存储不同信息:
|-------------------------------------------------------|--------------------|
| Mark Word (32 bits) | State |
|-------------------------------------------------------|--------------------|
| identity_hashcode:25 | age:4 | biased_lock:1 | lock:2 | Normal |
|-------------------------------------------------------|--------------------|
| thread:23 | epoch:2 | age:4 | biased_lock:1 | lock:2 | Biased |
|-------------------------------------------------------|--------------------|
| ptr_to_lock_record:30 | lock:2 | Lightweight Locked |
|-------------------------------------------------------|--------------------|
| ptr_to_heavyweight_monitor:30 | lock:2 | Heavyweight Locked |
|-------------------------------------------------------|--------------------|
| | lock:2 | Marked for GC |
|-------------------------------------------------------|--------------------|
5. 设计模式 —— 保护性暂停
保护性暂停(Guarded Suspension)模式:一个线程等待另一个线程的结果,结果产生前等待线程被挂起,结果产生后被唤醒。
4.1 基础实现
package richuff.top.richu6;
public class GuardObject{
private Object response;
public void get(){
synchronized (this){
while(response == null){
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public void set(Object response){
synchronized (this){
this.response = response;
this.notify();
}
}
}
运行测试
public static void main(String[] args) {
GuardObject guard = new GuardObject();
new Thread(()->{
log.info("begin... get");
guard.get();
log.info("end... get");
},"t1").start();
new Thread(()->{
Object response = new Object();
log.info("begin...response");
guard.set(response);
log.info("end...response");
},"t2").start();
}
运行输出:
[t1] INFO richuff.top.richu6.A - begin... get
[t2] INFO richuff.top.richu6.A - begin...response
[t2] INFO richuff.top.richu6.A - begin
[t1] INFO richuff.top.richu6.A - end... get
4.2 增加超时设置
public class GuardObject{
private Object response;
public void get(int sTime){
long begin = System.currentTimeMillis();
long end = 0;
synchronized (this){
while(response == null){
long hTIme = sTime - end;
if (hTIme <= 0){
break;
}
try {
this.wait(hTIme);
} catch (InterruptedException e) {
e.printStackTrace();
}
end = System.currentTimeMillis() - begin;
}
}
}
public void set(Object response){
synchronized (this){
this.response = response;
this.notify();
}
}
}
测试
public static void main(String[] args) {
GuardObject guard = new GuardObject();
new Thread(()->{
log.info("begin... get");
guard.get(2000);
log.info("end... get");
},"t1").start();
new Thread(()->{
Object response = new Object();
log.info("begin...response");
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
guard.set(response);
log.info("end...response");
},"t2").start();
}
运行结果:
[t1] INFO richuff.top.richu6.A - begin... get
[t2] INFO richuff.top.richu6.A - begin...response
[t1] INFO richuff.top.richu6.A - end... get
[t2] INFO richuff.top.richu6.A -end...response
4.3 进一步优化(带 id 的接收)
public class GuardObject{
private Object response;
private int id;
public GuardObject(int id){
this.id = id;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public Object get(int sTime){
long begin = System.currentTimeMillis();
long end = 0;
synchronized (this){
while(response == null){
long hTIme = sTime - end;
if (hTIme <= 0){
break;
}
try {
this.wait(hTIme);
} catch (InterruptedException e) {
e.printStackTrace();
}
end = System.currentTimeMillis() - begin;
}
return response;
}
}
public void set(Object response){
synchronized (this){
this.response = response;
this.notifyAll();
}
}
}
实现通过 id 进行接收的功能
class Post extends Thread{
private static final Logger log = LoggerFactory.getLogger(Post.class);
private int id;
private String msg;
public Post(int id, String msg) {
this.id = id;
this.msg = msg;
}
@Override
public void run() {
GuardObject guardObject = Boxes.getGuardById(id);
log.info("发送: id{},内容{}",guardObject.getId(),msg);
guardObject.set(msg);
}
}
class Position extends Thread{
private static final Logger log = LoggerFactory.getLogger(Position.class);
@Override
public void run() {
GuardObject guardObject = Boxes.createGuardObject();
log.info("开始接收收: id{}",guardObject.getId());
Object o = guardObject.get(5000);
log.info("收到内容: {}",o.toString());
}
}
class Boxes{
private static Map<Integer,GuardObject> boxes = new HashMap<>();
private static int id = 1;
private static synchronized int generatedId(){
return id++;
}
public static GuardObject createGuardObject(){
GuardObject guardObject = new GuardObject(generatedId());
boxes.put(guardObject.getId(),guardObject);
return guardObject;
}
public static Set<Integer> getAll(){
return boxes.keySet();
}
public static GuardObject getGuardById(int id){
return boxes.remove(id);
}
}
Post 发送消息,Position 通过 id 接收消息,两者间使用 Boxes 通信。
6. ReentrantLock
5.1 Lock 与可打断锁
| 方法 | 说明 |
|---|---|
lock() |
获取锁,不可被 interrupt 打断 |
lockInterruptibly() |
可打断锁,等待锁的过程中可被其他线程 interrupt |
private static ReentrantLock Lock = new ReentrantLock();
public static void main(String[] args) {
Thread t1 = new Thread(()->{
try {
//如果没有竞争那么此方法会获取Lock对象锁
//如果有竞争就进入阻塞队列,可以被其他线程用interrupt打断
log.info("尝试获取锁");
Lock.lockInterruptibly();
}catch (Exception e){
e.printStackTrace();
log.info("没有获取到锁");
return;
}try {
log.info("获取到锁");
}finally {
Lock.unlock();
}
});
Lock.lock();
t1.start();
}
5.2 锁超时 tryLock
tryLock() 尝试加锁,成功上锁返回 true,未成功返回 false。
private static ReentrantLock Lock = new ReentrantLock();
public static void main(String[] args) {
Thread t1 = new Thread(()->{
log.info("尝试获取锁");
if (! Lock.tryLock()){
log.info("获取不到锁");
return;
}
try {
log.info("获取到锁");
}catch (Exception e){
e.printStackTrace();
}finally {
Lock.unlock();
}
});
Lock.lock();
t1.start();
}
带参数的 tryLock() 会等待指定时间:
private static ReentrantLock Lock = new ReentrantLock();
public static void main(String[] args) {
Thread t1 = new Thread(()->{
log.info("尝试获取锁");
try {
if (! Lock.tryLock(1, TimeUnit.SECONDS)){
log.info("获取不到锁");
return;
}
}catch (Exception e){
e.printStackTrace();
}
try {
log.info("获取到锁");
}catch (Exception e){
e.printStackTrace();
}finally {
Lock.unlock();
}
});
Lock.lock();
t1.start();
}
5.3 公平锁
ReentrantLock 默认是非公平锁,可通过构造参数设为公平锁:
private static ReentrantLock Lock = new ReentrantLock(true);
5.4 条件变量
通过 lock.newCondition() 创建条件变量,实现更灵活的等待/唤醒:
private static ReentrantLock lock = new ReentrantLock();
public static void main(String[] args) throws Exception{
//创建条件变量
Condition condition1 = lock.newCondition();
Condition condition2 = lock.newCondition();
lock.lock();
//进入等待
condition1.wait();
condition1.signal();
condition1.signalAll();
}
5.5 固定运行顺序(t2 先于 t1 执行)
使用 synchronized + wait/notify:
public static Logger logger = LoggerFactory.getLogger(Thread.class);
public static boolean t2haverun = false;
public static final Object lock = new Object();
public static void main(String[] args) {
Thread t1 = new Thread(()->{
synchronized (lock){
while(!t2haverun){
try {
lock.wait();
}catch (Exception e){
e.printStackTrace();
}
}
logger.info("1");
}
},"t1");
Thread t2 = new Thread(()->{
synchronized (lock){
logger.info("2");
t2haverun = true;
lock.notify();
}
},"t2");
t1.start();
t2.start();
}
使用 park 和 unpark:
public static void main(String[] args) {
Thread t1 = new Thread(()->{
LockSupport.park();
logger.info("1");
},"t1");
Thread t2 = new Thread(()->{
logger.info("2");
LockSupport.unpark(t1);
},"t2");
t1.start();
t2.start();
}
5.6 循环顺序打印
public class Test {
public static void main(String[] args) {
WaitNotify wn = new WaitNotify(1, 5);
new Thread(() -> {
wn.Print("a", 1, 2);
}).start();
new Thread(() -> {
wn.Print("b", 2, 3);
}).start();
new Thread(() -> {
wn.Print("c", 3, 1);
}).start();
}
}
class WaitNotify {
public void Print(String str, int waitFlag, int nextFlag) {
for (int i = 0; i < loopNumber; i++) {
synchronized (this){
while (falg != waitFlag) {
try {
this.wait();
} catch (InterruptedException e) {
Thread.currentThread().interrupt(); // 重新设置中断状态
return; // 或者处理异常
}
}
System.out.println(str);
falg = nextFlag;
this.notifyAll();
}
}
}
private int falg;
private int loopNumber;
public WaitNotify(int falg, int loopNumber) {
this.falg = falg;
this.loopNumber = loopNumber;
}
}
7. JMM(Java 内存模型)
JMM(Java Memory Model,Java 内存模型)定义了多线程下变量的访问规则,从原子性、可见性、有序性三个维度保障并发正确。
| 特性 | 含义 |
|---|---|
| 原子性 | 保证指令不受线程上下文切换的影响 |
| 可见性 | 保证指令不会受 CPU 缓存的影响 |
| 有序性 | 保证指令不会受 CPU 指令并行优化的影响 |
6.1 Unsafe 对象的获取
通过反射获取 Unsafe 实例:
Field theUnsafe = Unsafe.class.getDeclaredField("theUnsafe");
theUnsafe.setAccessible(true);
Unsafe unsafe = (Unsafe) theUnsafe.get(null);
System.out.println(unsafe);
6.2 不可变对象的设计
以模拟连接池为例,使用 AtomicIntegerArray 管理连接状态:
class Pool{
private final int poolSize;
private Connection[] connPools;
private AtomicIntegerArray states;
public Pool(int poolSize) {
this.states = new AtomicIntegerArray(new int[poolSize]);
this.connPools = new Connection[poolSize];
this.poolSize = poolSize;
}
public Connection borrow() {
while (true){
for (int i = 0; i < poolSize; i++) {
if (states.get(i) == 0){
if (states.compareAndSet(i,0,1)){
return connPools[i];
}
}
}
//无空闲连接
synchronized (this){
try{
wait();
}catch (Exception e){
e.printStackTrace();
}
}
}
}
public void free(Connection conn){
for (int i = 0; i < poolSize; i++) {
if (conn == connPools[i] && states.get(i) == 1){
states.set(i,0);
synchronized (this){
this.notifyAll();
}
break;
}
}
}
}
6.3 阻塞队列的实现
基于 ReentrantLock 与条件变量实现自定义阻塞队列 BlockingQueue:
public class BlockingQueue<T>{
private static final Logger log = LoggerFactory.getLogger(BlockingQueue.class);
private Deque<T> queue = new ArrayDeque<>();
private ReentrantLock lock = new ReentrantLock();
//生产者条件变量
private Condition fullWaitSet = lock.newCondition();
//消费者条件变量
private Condition emptyWaitSet = lock.newCondition();
//容量
private int capcity;
public BlockingQueue(int capcity) {
this.capcity = capcity;
}
// 带超时的阻塞获取
public T poll(long time, TimeUnit unit){
//将时间转为统一的单位
lock.lock();
try {
long nanoTime = unit.toNanos(time);
while (queue.isEmpty()){
if (nanoTime <= 0){
return null;
}
nanoTime = emptyWaitSet.awaitNanos(nanoTime);
}
}catch (Exception e){
e.printStackTrace();
}finally {
lock.unlock();
}
fullWaitSet.signalAll();
return queue.removeFirst();
}
//阻塞获取
public T take(){
lock.lock();
try {
while (queue.isEmpty()){
emptyWaitSet.await();
}
}catch (Exception e){
e.printStackTrace();
}finally {
lock.unlock();
}
fullWaitSet.signalAll();
return queue.removeFirst();
}
public void put(T t){
lock.lock();
while(queue.size() == capcity){
try{
fullWaitSet.await();
}catch (Exception e){
e.printStackTrace();
}finally {
lock.unlock();
}
}
queue.addLast(t);
emptyWaitSet.signalAll();
}
public int size(){
lock.lock();
try {
return queue.size();
}finally {
lock.unlock();
}
}
}
6.4 线程池的实现
@Slf4j
class TestPool{
public static Logger log = LoggerFactory.getLogger(TestPool.class);
public static void main(String[] args) {
log.info("begin");
ThreadPool threadPool = new ThreadPool(1000, 2, TimeUnit.MILLISECONDS, 10);
for (int i = 0; i < 15; i++) {
threadPool.excute(()->{
try {
Thread.sleep(1000000L);
}catch (Exception e){
e.printStackTrace();
}
log.info("test");
});
}
}
}
public class BlockingQueue<T>{
private static final Logger log = LoggerFactory.getLogger(BlockingQueue.class);
private Deque<T> queue = new ArrayDeque<>();
private ReentrantLock lock = new ReentrantLock();
//生产者条件变量
private Condition fullWaitSet = lock.newCondition();
//消费者条件变量
private Condition emptyWaitSet = lock.newCondition();
//容量
private int capcity;
public BlockingQueue(int capcity) {
this.capcity = capcity;
}
// 带超时的阻塞获取
public T poll(long time, TimeUnit unit){
//将时间转为统一的单位
lock.lock();
try {
long nanoTime = unit.toNanos(time);
while (queue.isEmpty()){
if (nanoTime <= 0){
return null;
}
nanoTime = emptyWaitSet.awaitNanos(nanoTime);
}
}catch (Exception e){
e.printStackTrace();
}finally {
lock.unlock();
}
log.info("唤醒线程");
fullWaitSet.signalAll();
return queue.removeFirst();
}
//阻塞获取
public T take(){
lock.lock();
try {
while (queue.isEmpty()){
emptyWaitSet.await();
}
}catch (Exception e){
e.printStackTrace();
}finally {
lock.unlock();
}
log.info("唤醒线程");
fullWaitSet.signalAll();
return queue.removeFirst();
}
public void put(T t){
lock.lock();
try {
while (queue.size() == capcity) {
try {
fullWaitSet.await();
} catch (Exception e) {
e.printStackTrace();
}
}
log.info("加入任务队列");
queue.addLast(t);
emptyWaitSet.signalAll();
}
finally {
lock.unlock();
}
}
public int size(){
lock.lock();
try {
return queue.size();
}finally {
lock.unlock();
}
}
}
class ThreadPool{
private static final Logger log = LoggerFactory.getLogger(ThreadPool.class);
private BlockingQueue<Runnable> taskQueue;
private HashSet<Worker> workers = new HashSet<>();
private int currentSize;
//设置超时时间
private long timeout;
private TimeUnit unit;
public void excute(Runnable task){
synchronized (workers){
if (workers.size() < currentSize){
Worker worker = new Worker(task);
log.info("加入工作队列");
workers.add(worker);
worker.start();
}else{
taskQueue.put(task);
}
}
}
public ThreadPool(long timeout, int currentSize, TimeUnit unit,int capacity) {
this.timeout = timeout;
this.currentSize = currentSize;
this.taskQueue = new BlockingQueue<>(capacity);
this.unit = unit;
}
class Worker extends Thread {
private Runnable task;
Worker(Runnable task) {
this.task = task;
}
@Override
public void run() {
//执行任务 如果当前任务对象不为空,并且从任务队列也不为空
while (task != null || (task = taskQueue.poll(timeout,TimeUnit.MILLISECONDS)) != null) {
try {
task.run();
} catch (Exception e) {
e.printStackTrace();
} finally {
task = null;
}
}
synchronized (workers) {
log.info("移除worker{}", this);
workers.remove(this);
}
}
@Override
public String toString() {
return super.toString();
}
}
}
6.5 带拒绝策略的线程池
class TestPool{
public static Logger log = LoggerFactory.getLogger(TestPool.class);
public static void main(String[] args) {
log.info("begin");
ThreadPool threadPool = new ThreadPool(1000, 2, TimeUnit.MILLISECONDS, 10,(queue,task)->{
//queue.put(task);
queue.offer(task,1000,TimeUnit.MILLISECONDS);
});
for (int i = 0; i < 15; i++) {
threadPool.excute(()->{
try {
Thread.sleep(20000L);
}catch (Exception e){
e.printStackTrace();
}
log.info("test");
});
}
}
}
@FunctionalInterface
interface RejectPolicy<T>{
void reject(BlockingQueue<T> queue,T task);
}
public class BlockingQueue<T>{
private static final Logger log = LoggerFactory.getLogger(BlockingQueue.class);
private Deque<T> queue = new ArrayDeque<>();
private ReentrantLock lock = new ReentrantLock();
//生产者条件变量
private Condition fullWaitSet = lock.newCondition();
//消费者条件变量
private Condition emptyWaitSet = lock.newCondition();
//容量
private int capcity;
public BlockingQueue(int capcity) {
this.capcity = capcity;
}
// 带超时的阻塞获取
public T poll(long time, TimeUnit unit){
//将时间转为统一的单位
lock.lock();
try {
long nanoTime = unit.toNanos(time);
while (queue.isEmpty()){
if (nanoTime <= 0){
return null;
}
nanoTime = emptyWaitSet.awaitNanos(nanoTime);
}
log.info("唤醒线程");
fullWaitSet.signalAll();
}catch (Exception e){
e.printStackTrace();
}finally {
lock.unlock();
}
return queue.removeFirst();
}
//阻塞获取
public T take(){
lock.lock();
try {
while (queue.isEmpty()){
emptyWaitSet.await();
}
log.info("唤醒线程");
fullWaitSet.signalAll();
}catch (Exception e){
e.printStackTrace();
}finally {
lock.unlock();
}
return queue.removeFirst();
}
public void put(T t){
lock.lock();
try {
while (queue.size() == capcity) {
try {
fullWaitSet.await();
} catch (Exception e) {
e.printStackTrace();
}
}
log.info("加入任务队列");
queue.addLast(t);
emptyWaitSet.signalAll();
}
finally {
lock.unlock();
}
}
public boolean offer(T t,long time,TimeUnit unit){
lock.lock();
try {
long nanoTime = unit.toNanos(time);
while (queue.size() == capcity) {
try {
if (nanoTime <= 0){
log.info("任务超时");
return false;
}
nanoTime = fullWaitSet.awaitNanos(nanoTime);
} catch (Exception e) {
e.printStackTrace();
}
}
log.info("加入任务队列");
queue.addLast(t);
emptyWaitSet.signalAll();
}
finally {
lock.unlock();
}
return true;
}
public int size(){
lock.lock();
try {
return queue.size();
}finally {
lock.unlock();
}
}
public void tryPut(RejectPolicy<T> rejectPolicy, T task) {
lock.lock();
try {
if (queue.size() == capcity){
rejectPolicy.reject(this,task);
}else{
log.info("加入任务队列");
queue.addLast(task);
emptyWaitSet.signalAll();
}
}finally {
lock.unlock();
}
}
}
class ThreadPool{
private static final Logger log = LoggerFactory.getLogger(ThreadPool.class);
private BlockingQueue<Runnable> taskQueue;
private HashSet<Worker> workers = new HashSet<>();
private int currentSize;
//设置超时时间
private long timeout;
private TimeUnit unit;
private RejectPolicy<Runnable> rejectPolicy;
public void excute(Runnable task){
synchronized (workers){
if (workers.size() < currentSize){
Worker worker = new Worker(task);
log.info("加入工作队列");
workers.add(worker);
worker.start();
}else{
//taskQueue.put(task);
taskQueue.tryPut(rejectPolicy,task);
}
}
}
public ThreadPool(long timeout, int currentSize, TimeUnit unit,int capacity,RejectPolicy<Runnable> rejectPolicy) {
this.timeout = timeout;
this.currentSize = currentSize;
this.taskQueue = new BlockingQueue<>(capacity);
this.unit = unit;
this.rejectPolicy = rejectPolicy;
}
class Worker extends Thread {
private Runnable task;
Worker(Runnable task) {
this.task = task;
}
@Override
public void run() {
//执行任务 如果当前任务对象不为空,并且从任务队列也不为空
while (task != null || (task = taskQueue.poll(timeout,TimeUnit.MILLISECONDS)) != null) {
try {
task.run();
} catch (Exception e) {
e.printStackTrace();
} finally {
task = null;
}
}
synchronized (workers) {
log.info("移除worker{}", this);
workers.remove(this);
}
}
@Override
public String toString() {
return super.toString();
}
}
}
6.6 ThreadPoolExecutor 与 Executors
6.6.1 newFixedThreadPool
线程池的线程数是固定的,即使任务数量增加,线程池中的线程数也不会超过指定的数量。线程池中的线程是复用的,当某个线程完成了任务,它可以继续执行下一个任务,而不需要重新创建线程。
ExecutorService pool = Executors.newFixedThreadPool(2, new ThreadFactory() {
private AtomicInteger t = new AtomicInteger(1);
@Override
public Thread newThread(Runnable r) {
return new Thread(
r,"my_thread"+t.getAndIncrement()
);
}
});
pool.execute(()->{
log.info("1");
});
pool.execute(()->{
log.info("2");
});
pool.execute(()->{
log.info("3");
});
6.6.2 newCachedThreadPool
ExecutorService service = Executors.newCachedThreadPool();
6.6.3 提交任务
| 方法 | 说明 |
|---|---|
execute(Runnable) |
提交 Runnable 任务,无返回值 |
submit(Callable) |
提交 Callable 任务,返回 Future |
ExecutorService pool = Executors.newFixedThreadPool(2);
/* Future<String> future = pool.submit(new Callable<String>() {
@Override
public String call() throws Exception {
Thread.sleep(1000);
return "hhh";
}
});*/
//lambda 简化
Future<String> future = pool.submit(()->{
Thread.sleep(1000);
return "hhh";
});
log.info(future.get());
invokeAll() 执行其中所有的任务:
ExecutorService pool = Executors.newFixedThreadPool(2);
List<Future<String>> futures = pool.invokeAll(Arrays.asList(
() -> {
Thread.sleep(500);
return "1";
},
() -> {
Thread.sleep(1000);
return "2";
},
() -> {
Thread.sleep(2000);
return "3";
}
));
for (Future<String> future:futures){
log.info(future.get());
}
invokeAny() 哪个任务先完成,就直接返回任务结果,其他的任务终止:
ExecutorService pool = Executors.newFixedThreadPool(2);
String res = pool.invokeAny(Arrays.asList(
() -> {
Thread.sleep(500);
return "1";
},
() -> {
Thread.sleep(1000);
return "2";
},
() -> {
Thread.sleep(2000);
return "3";
}
));
log.info("{}",res);
6.7 newScheduledThreadPool
6.7.1 scheduleAtFixedRate
ScheduledFuture<?> scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit);
| 参数 | 说明 |
|---|---|
command |
一个实现了 Runnable 接口的无参数函数,它将在指定的时间间隔内重复执行 |
initialDelay |
首次执行前的初始延迟时间。如果设置为 0,则任务将立即执行 |
period |
连续执行之间的时间间隔 |
unit |
period 参数的时间单位,TimeUnit 是一个枚举,定义了时间的单位(如天、小时、分钟、秒、毫秒等) |
6.7.2 schedule
ScheduledFuture<?> schedule(Runnable command, long delay, TimeUnit unit);
| 参数 | 说明 |
|---|---|
command |
一个实现了 Runnable 接口的无参数函数,它将在指定的延迟后执行一次 |
delay |
首次执行的延迟时间,从现在开始计算 |
unit |
delay 参数的时间单位 |
//延迟执行
pool.schedule(()->{
log.info("test");
},1,TimeUnit.SECONDS);
6.7.3 scheduleWithFixedDelay
| 参数 | 说明 |
|---|---|
command |
一个 Runnable 任务,将在线程池中的某个线程上执行 |
initialDelay |
首次执行任务之前的初始延迟时间,例如 1 秒表示线程池启动后 1 秒开始执行 |
delay |
每次任务执行完毕后,到下一次任务开始执行之前的固定延迟时间,例如 1 秒 |
unit |
initialDelay 和 delay 参数的时间单位,例如 TimeUnit.SECONDS 表示秒 |
//固定等待时长 不会等待前一个任务完成
pool.scheduleWithFixedDelay(()->{
log.info("fixed");
},1,1,TimeUnit.SECONDS);
6.8 每周执行一次任务
//获取当前时间
LocalDateTime localDateTime = LocalDateTime.now();
//获取周四时间
LocalDateTime dateTime = localDateTime.withHour(16).withMinute(0).withSecond(0).with(DayOfWeek.MONDAY);
//如果当前时间大于周四
if (localDateTime.isAfter(dateTime)){
dateTime.plusWeeks(1);
}
long initialDelay = Duration.between(localDateTime,dateTime).toMillis();
//间隔一周
long period = 1000 * 60 * 60 * 24 * 7;
ScheduledExecutorService pool = Executors.newScheduledThreadPool(1);
pool.scheduleAtFixedRate(()->{
log.info("test");
},initialDelay,period, TimeUnit.MILLISECONDS);
6.9 基于 AQS 自定义锁
通过继承 AbstractQueuedSynchronizer(AQS)实现自定义可重入锁:
public class MRennterLock {
public static Logger log = LoggerFactory.getLogger(MRennterLock.class);
public static RFLock lock = new RFLock();
public static void main(String[] args) {
new Thread(()->{
log.info("t1 begin..");
lock.lock();
try {
Thread.sleep(3000);
}catch (InterruptedException e){
e.printStackTrace();
}finally {
lock.unlock();
log.info("t1 end...");
}
},"t1").start();
new Thread(()->{
log.info("t2 begin..");
lock.lock();
try {
Thread.sleep(1000);
}catch (InterruptedException e){
e.printStackTrace();
}finally {
lock.unlock();
log.info("t2 end...");
}
},"t2").start();
}
}
class RFLock implements Lock{
static class ReRFLock extends AbstractQueuedSynchronizer{
@Override
protected boolean tryRelease(int arg) {
if (compareAndSetState(0,1)){
setExclusiveOwnerThread(Thread.currentThread());
return true;
}else{
return false;
}
}
@Override
protected boolean tryAcquire(int arg) {
setExclusiveOwnerThread(null);
setState(0);
return true;
}
public Condition newCondition(){
return new ConditionObject();
}
@Override
public String toString() {
return super.toString();
}
}
private final ReRFLock reRFLock = new ReRFLock();
@Override
public void lock() {
reRFLock.acquire(1);
}
@Override
public void lockInterruptibly() throws InterruptedException {
reRFLock.acquireInterruptibly(1);
}
@Override
public boolean tryLock() {
return reRFLock.tryAcquire(1);
}
@Override
public boolean tryLock(long time, TimeUnit unit) throws InterruptedException {
return reRFLock.tryAcquireSharedNanos(1, unit.toNanos(time));
}
@Override
public void unlock() {
reRFLock.release(1);
}
@Override
public Condition newCondition() {
return reRFLock.newCondition();
}
}
8. 动态代理
7.1 介绍
在 Java 动态代理机制中 InvocationHandler 接口和 Proxy 类是核心。
Proxy 类中使用频率最高的方法是:newProxyInstance(),这个方法主要用来生成一个代理对象。
public static Object newProxyInstance(ClassLoader loader,
Class<?>[] interfaces,
InvocationHandler h)
throws IllegalArgumentException
{
......
}
这个方法一共有 3 个参数:
- loader:类加载器,用于加载代理对象;
- interfaces:被代理类实现的一些接口;
- h:实现了
InvocationHandler接口的对象。
要实现动态代理,还必须实现 InvocationHandler 来自定义处理逻辑。当动态代理对象调用一个方法时,这个方法的调用会被转发到实现 InvocationHandler 接口类的 invoke 方法来调用。
public interface InvocationHandler {
/**
* 当你使用代理对象调用方法的时候实际会调用到这个方法
*/
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable;
}
invoke() 方法有下面三个参数:
- proxy:动态生成的代理类;
- method:与代理类对象调用的方法相对应;
- args:当前 method 方法的参数。
也就是说:你通过 Proxy 类的 newProxyInstance() 创建的代理对象在调用方法的时候,实际会调用到实现 InvocationHandler 接口的类的 invoke() 方法。 你可以在 invoke() 方法中自定义处理逻辑,比如在方法执行前后做什么事情。
7.2 JDK 动态代理使用步骤
- 定义一个接口及其实现类;
- 自定义
InvocationHandler并重写invoke方法,在invoke方法中调用原生方法(被代理类的方法)并自定义一些处理逻辑; - 通过
Proxy.newProxyInstance(ClassLoader loader, Class<?>[] interfaces, InvocationHandler h)方法创建代理对象。
7.3 代码示例
1. 定义发送短信的接口
public interface SmsService {
String send(String message);
}
2. 实现发送短信的接口
public class SmsServiceImpl implements SmsService {
public String send(String message) {
System.out.println("send message:" + message);
return message;
}
}
3. 定义一个 JDK 动态代理类
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
/**
* @author shuang.kou
* @createTime 2020年05月11日 11:23:00
*/
public class DebugInvocationHandler implements InvocationHandler {
/**
* 代理类中的真实对象
*/
private final Object target;
public DebugInvocationHandler(Object target) {
this.target = target;
}
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws InvocationTargetException, IllegalAccessException {
//调用方法之前,我们可以添加自己的操作
System.out.println("before method " + method.getName());
Object result = method.invoke(target, args);
//调用方法之后,我们同样可以添加自己的操作
System.out.println("after method " + method.getName());
return result;
}
}
invoke() 方法:当动态代理对象调用原生方法的时候,最终实际上调用到的是 invoke() 方法,然后 invoke() 方法代替我们去调用了被代理对象的原生方法。
7.4 获取代理对象的工厂类
public class JdkProxyFactory {
public static Object getProxy(Object target) {
return Proxy.newProxyInstance(
target.getClass().getClassLoader(), // 目标类的类加载器
target.getClass().getInterfaces(), // 代理需要实现的接口,可指定多个
new DebugInvocationHandler(target) // 代理对象对应的自定义 InvocationHandler
);
}
}
getProxy():主要通过 Proxy.newProxyInstance() 方法获取某个类的代理对象。
7.5 实际使用
SmsService smsService = (SmsService) JdkProxyFactory.getProxy(new SmsServiceImpl());
smsService.send("java");
运行上述代码之后,控制台打印出:
before method send
send message:java
after method send
一句话总结:JUC 围绕线程基础、锁机制、线程池与动态代理等核心组件,提供了一套在保障线程安全的前提下提升并发能力的标准工具集。