0%

Java系列之多线程并发

Basic Knowledge

https://www.baeldung.com/tag/java-concurrency/
https://www.baeldung.com/java-concurrent-locks

Overview of the java.util.concurrent

https://www.baeldung.com/java-util-concurrent

  • Executor
  • ExecutorService
  • ScheduledExecutorService
  • Future
  • CountDownLatch
  • CyclicBarrier
  • Semaphore
  • ThreadFactory
  • BlockingQueue
  • DelayQueue
  • Locks
  • Phaser

Semaphores in Java API

https://www.baeldung.com/java-semaphore

多线程知识的要点

参考自知乎这篇文章

ThreadPoolExecutor 四种处理策略

ThreadPoolExecutor.AbortPolicy() 抛出java.util.concurrent.RejectedExecutionException异常
ThreadPoolExecutor.CallerRunsPolicy() 重试添加当前的任务,他会自动重复调用execute()方法
ThreadPoolExecutor.DiscardOldestPolicy() 抛弃旧的任务
ThreadPoolExecutor.DiscardPolicy() 抛弃当前的任务

锁对象的声明

如果只是简单的一个锁对象,没有类额外的对象状态变更的话,可以将锁声明为final:

1
2
3
// 来自org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter
// Spring Security token校验机制
private final Object delegateMonitor = new Object();

乐观锁 vs 悲观锁(PCC)

http://www.imooc.com/article/285147

为什么要写成 final ReentrantLock lock = this.lock; ?

https://blog.csdn.net/zqz_zqz/article/details/79438502

LockSupport.parkNanos(1L); ???

统计当前线程存活量

1
2
3
while (Thread.activeCount() > 1) {
// do something.
}

单例双重锁定检查(Double-Checked Locking with Singleton)

https://www.baeldung.com/java-singleton-double-checked-locking

必要时可以用上线程优先级来实现一些业务逻辑

1
2
3
Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
System.out.println(Thread.currentThread().getPriority());
Thread.currentThread().setPriority(Thread.MAX_PRIORITY);

老旧的有道云笔记