java.util.concurrent Uml

本文详细介绍了Java并发编程中核心组件的设计与实现原理,包括各种队列、锁、线程池及原子类等,并深入探讨了它们之间的联系与区别。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

java.util.concurrent(不包含外部类的继承关系)

non-exception

这里写图片描述

普通队列

  • ConcurrentLinkedDeque
  • ConcurrentLinkedQueue

阻塞队列

  • LinkedBlockingDeque
  • LinkedTransferQueue
  • ArrayBlockingQueue
    • final Object[] items
    • final ReentrantLock lock
    • new 需要指定大小
  • DelayQueue
  • LinkedBlockingQueue
    • Node {item, next}
    • 双向链表:Node head,Node last
    • 初始化即 last = head = new Node(null),即每个时刻头尾都是 item=null,这样就实现了 2 个操作(put take)或锁的无关性,提高性能,类似分段锁
    • ReentrantLock takeLock
    • ReentrantLock putLock
    • new 大小默认 Integer.MAX_VALUE
  • PriorityBlockingQueue
  • SynchronousQueue

List & Set

  • ConcurrentSkipListSet
    • 基于 ConcurrentSkipListMap
    • 内部有 static UNSAFE,但是是再 clone() 的时候用到
  • Copy-on-Write 解释:写时复制,即用于读多写少多场景,读不加锁,写的时候加锁,再复制一份修改,修改好了再提换掉原值
  • CopyOnWriteArrayList
    • 基于 ReentrantLock
    • volatile Object[] array
    • get 不加锁
    • add 等包含修改等操作先加锁,再通过 Arrays.copyOf -> native void System.arraycopy 复制一份进行 add(或其它操作),然后再 set 当前 array
  • CopyOnWriteArraySet
    • 基于 CopyOnWriteArrayList<E> al

Map

  • ConcurrentHashMap
    • Java 7 & 8 HashMap ConcurrentHashMap
    • Java 7
      • 分段锁
      • final Segment<K,V>[] segments 默认大小 16,记住大小设置后不可变
      • volatile Node<K,V>[] nextTable rehash 用到
      • Segment 继承自 ReentrantLock,内部有 volatile HashEntry<K,V>[] table
      • Segment 提供个 map 的一些操作:put remove rehash 等
      • HashEntry 是一个 {hash, k, v, next} 链
      • get:(k = e.key) == key || (e.hash == h && key.equals(k))
    • Java 8:红黑树
      • volatile Node<K,V>[] table 默认大小 16
      • Node 是一个 {hash, k, v, next} 链
      • TreeNode

Others

  • CountDownLatch
  • Exchanger
  • Executors
  • ForkJoinWorkerThread
  • Phaser
  • Semaphore
  • ThreadLocalRandom

PlantUml

@startuml

enum TimeUnit

interface BlockingDeque
interface BlockingQueue
interface Callable
interface CompletionService
interface CompletionStage
interface ConcurrentMap
interface ConcurrentNavigableMap
interface Delayed
interface Executor
interface ExecutorService
interface Future
interface RejectedExecutionHandler
interface RunnableFuture
interface RunnableScheduledFuture
interface ScheduledExecutorService
interface ScheduledFuture
interface ThreadFactory
interface TransferQueue

Executor <|-- ExecutorService
ConcurrentMap <|-- ConcurrentNavigableMap
Future <|-- RunnableFuture
ScheduledFuture <|-- RunnableScheduledFuture
RunnableFuture <|-- RunnableScheduledFuture
ExecutorService <|-- ScheduledExecutorService
Future <|-- ScheduledFuture
Delayed <|-- ScheduledFuture
BlockingQueue <|-- TransferQueue

abstract class AbstractExecutorService
abstract class CountedCompleter
abstract class ForkJoinTask
abstract class RecursiveAction
abstract class RecursiveTask

ExecutorService <|.. AbstractExecutorService

BlockingQueue <|.. ArrayBlockingQueue
BlockingQueue <|-- BlockingDeque

Future <|.. CompletableFuture
CompletionStage <|.. CompletableFuture

ConcurrentMap <|.. ConcurrentHashMap

ConcurrentNavigableMap <|.. ConcurrentSkipListMap

Future <|.. ForkJoinTask
ForkJoinTask <|-- CountedCompleter

BlockingQueue <|.. DelayQueue

CompletionService <|.. ExecutorCompletionService

AbstractExecutorService <|-- ForkJoinPool

RunnableFuture <|.. FutureTask

BlockingDeque <|.. LinkedBlockingDeque
BlockingQueue <|.. LinkedBlockingQueue
TransferQueue <|.. LinkedTransferQueue
BlockingQueue <|.. PriorityBlockingQueue

ForkJoinTask <|-- RecursiveAction
ForkJoinTask <|-- RecursiveTask

ScheduledExecutorService <|.. ScheduledThreadPoolExecutor
ThreadPoolExecutor <|-- ScheduledThreadPoolExecutor

BlockingQueue <|.. SynchronousQueue

AbstractExecutorService <|-- ThreadPoolExecutor

class ConcurrentLinkedDeque
class ConcurrentLinkedQueue
class ConcurrentSkipListSet
class CopyOnWriteArrayList
class CopyOnWriteArraySet
class CountDownLatch
class Exchanger
class Executors
class ForkJoinWorkerThread
class Phaser
class Semaphore
class ThreadLocalRandom

@enduml

exception

这里写图片描述

@startuml

class BrokenBarrierException
class CancellationExceptpion
class CompletionException
class ExecutionException
class RejectedExecutionException
class TimeoutException
class CyclicBarrier

@enduml

java.util.concurrent.atomic Uml

这里写图片描述

PlantUml

@startUml

package java.lang {
  abstract class Number
}
package sun.misc {
  class Unsafe
}

package java.util.concurrent.atomic {
  class AtomicBoolean {
    -static final Unsafe unsafe
    -volatile int value
  }
  class AtomicInteger {
    -static final Unsafe unsafe
    -volatile int value
  }
  class AtomicIntegerArray {
    -static final Unsafe unsafe
    final int[] value
  }
  abstract class AtomicIntegerFieldUpdater
  class AtomicLong {
    -static final Unsafe unsafe
    -volatile long value
  }
  class AtomicLongArray {
    -static final Unsafe unsafe
    -final long[] array
  }
  abstract class AtomicLongFieldUpdater
  class AtomicMarkableReference {
    -static final Unsafe UNSAFE
  }
  class AtomicReference {
    -static final Unsafe unsafe
    -volatile V value
  }
  class AtomicReferenceArray {
    -static final Unsafe unsafe
    -final Object[] array
  }
  abstract class AtomicReferenceFieldUpdater
  class AtomicStampedReference {
    -static final Unsafe UNSAFE
  }
  class DoubleAccumulator
  class DoubleAdder
  class LongAccumulator
  class LongAdder
  abstract class Striped64 {
    -static final Unsafe UNSAFE
  }
}

Unsafe <.. AtomicBoolean
Unsafe <.. AtomicInteger
Unsafe <.. AtomicIntegerArray
Unsafe <.. AtomicIntegerFieldUpdater
Unsafe <.. AtomicLong
Unsafe <.. AtomicLongArray
Unsafe <.. AtomicLongFieldUpdater
Unsafe <.. AtomicMarkableReference
Unsafe <.. AtomicReference
Unsafe <.. AtomicReferenceArray
Unsafe <.. AtomicReferenceFieldUpdater
Unsafe <.. AtomicStampedReference
Unsafe <.. Striped64

Number <|-- AtomicInteger
Number <|-- AtomicLong
Number <|-- Striped64

Striped64 <|-- DoubleAccumulator
Striped64 <|-- DoubleAdder
Striped64 <|-- LongAccumulator
Striped64 <|-- LongAdder

@endUml

java.util.concurrent.locks

这里写图片描述

Lock

  • ReentrantLock
    • 默认非公平锁
    • Sync NonfairSync FairSync
    • 只有排它锁,所以释放和获取都只有一种情况
    • state 代表当前线程获取锁对次数,0 代表没有线程获取,exclusiveOwnerThread 代表持有排它锁对线程
    • 都是对上面 3 个锁对包装
  • ReentrantReadWriteLock
    • 默认非公平锁
    • Sync NonfairSync FairSync ReadLock WriteLock
    • ReadLock WriteLock 包装 Sync
    • 有排它锁和共享锁,所以释放和获取有 2 种情况
    • 都是对上面 5 个锁对包装
  • StampedLock
    • Java 8 才有
    • ReadLockView
    • WriteLockView
    • ReadWriteLockView

PlantUml

@startUml

package sun.misc {
  class Unsafe
}

package java.util.concurrent.locks {
  abstract class AbstractOwnableSynchronizer {
    -transient Thread exclusiveOwnerThread
    #AbstractOwnableSynchronizer()
    #final void setExclusiveOwnerThread(Thread thread)
    #final Thread getExclusiveOwnerThread()
  }
  abstract class AbstractQueuedLongSynchronizer {
    -static final Unsafe unsafe
    -volatile long state
  }
  abstract class AbstractQueuedSynchronizer {
    -static final Unsafe unsafe
    -volatile int state
  }
  interface Condition {
    +void await() throws InterruptedException
    +void awaitUninterruptibly()
    +long awaitNanos(long nanosTimeout) throws InterruptedException
    +boolean await(long time, TimeUnit unit) throws InterruptedException
    +boolean awaitUntil(Date deadline) throws InterruptedException
    +void signal()
    +void signalAll()
  }
  interface Lock {
    +void lock()
    +void lockInterruptibly() throws InterruptedException
    +boolean tryLock()
    +boolean tryLock(long time, TimeUnit unit) throws InterruptedException
    +void unlock()
    +Condition newCondition()
  }
  class LockSupport {
    -static final Unsafe UNSAFE
    +static void unpark(Thread thread)
    +static void park(Object blocker)
    +static void parkNanos(Object blocker, long nanos)
    +static void parkUntil(Object blocker, long deadline)
    +static Object getBlocker(Thread t)
    +static void park()
    +static void parkNanos(long nanos)
    +static void parkUntil(long deadline)
    ~static final int nextSecondarySeed()
  }
  interface ReadWriteLock {
    +Lock readLock()
    +Lock writeLock()
  }
  class ReentrantLock
  class ReentrantReadWriteLock {
    -static final Unsafe UNSAFE
    -ReadLock readerLock
    -WriteLock writerLock
  }
  class StampedLock
}

AbstractOwnableSynchronizer <|-- AbstractQueuedLongSynchronizer
AbstractOwnableSynchronizer <|-- AbstractQueuedSynchronizer
Lock <.. ReadWriteLock
Lock <|.. ReentrantLock
ReadWriteLock <|.. ReentrantReadWriteLock

Unsafe <.. AbstractQueuedLongSynchronizer
Unsafe <.. AbstractQueuedSynchronizer
Condition <.. Lock
Unsafe <.. LockSupport
Condition <.. ReentrantLock
Unsafe <.. ReentrantReadWriteLock
ReadWriteLock <.. StampedLock

@endUml
内容概要:本文档详细介绍了一个基于MATLAB实现的跨尺度注意力机制(CSA)结合Transformer编码器的多变量时间序列预测项目。项目旨在精准捕捉多尺度时间序列特征,提升多变量时间序列的预测性能,降低模型计算复杂度与训练时间,增强模型的解释性和可视化能力。通过跨尺度注意力机制,模型可以同时捕获局部细节和全局趋势,显著提升预测精度和泛化能力。文档还探讨了项目面临的挑战,如多尺度特征融合、多变量复杂依赖关系、计算资源瓶颈等问题,并提出了相应的解决方案。此外,项目模型架构包括跨尺度注意力机制模块、Transformer编码器层和输出预测层,文档最后提供了部分MATLAB代码示例。 适合人群:具备一定编程基础,尤其是熟悉MATLAB和深度学习的科研人员、工程师和研究生。 使用场景及目标:①需要处理多变量、多尺度时间序列数据的研究和应用场景,如金融市场分析、气象预测、工业设备监控、交通流量预测等;②希望深入了解跨尺度注意力机制和Transformer编码器在时间序列预测中的应用;③希望通过MATLAB实现高效的多变量时间序列预测模型,提升预测精度和模型解释性。 其他说明:此项目不仅提供了一种新的技术路径来处理复杂的时间序列数据,还推动了多领域多变量时间序列应用的创新。文档中的代码示例和详细的模型描述有助于读者快速理解和复现该项目,促进学术和技术交流。建议读者在实践中结合自己的数据集进行调试和优化,以达到最佳的预测效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值