![](https://img-blog.csdnimg.cn/20201014180756916.png?x-oss-process=image/resize,m_fixed,h_224,w_224)
并发编程
并发编程
sctttt
源码注释笔记(●ˇ∀ˇ●)
展开
-
ThreadLocal源码解析
hash值的计算。 //初始为0,静态变量。 private static AtomicInteger nextHashCode = new AtomicInteger(); /** * The difference between successively generated hash codes - turns * implicit sequential thread-local IDs into near-optimally spread * multiplic原创 2021-07-26 18:45:35 · 78 阅读 · 0 评论 -
ExecutorCompletionService源码解析
主要来看看FutureTask的done方法的运用。public class ExecutorCompletionService<V> implements CompletionService<V> { //执行任务的线程池 private final Executor executor; //用于调用AbstractExecutorService的newTaskFor方法,来实例化一个实现了RunnableFuture接口的对象 //如果executor原创 2021-07-25 13:51:19 · 175 阅读 · 0 评论 -
FutureTask源码解析
成员变量和常量 /** * The run state of this task, initially NEW. The run state * transitions to a terminal state only in methods set, * setException, and cancel. During completion, state may take on * transient values of COMPLETING (while ou原创 2021-07-25 13:32:30 · 58 阅读 · 0 评论 -
CopyOnWriteArrayList源码分析
成员变量 //用于存放数据的array,object类型 /** The array, accessed only via getArray/setArray. */ private transient volatile Object[] array; //锁 /** The lock protecting all mutators */ final transient ReentrantLock lock = new ReentrantLock();构造函数 //无原创 2021-07-19 18:03:09 · 49 阅读 · 0 评论 -
ConcurrentHashMap源码分析(二) ---- get方法和remove方法
有了前文的基础,直接看到get方法 public V get(Object key) { Node<K,V>[] tab; Node<K,V> e, p; int n, eh; K ek; //计算hash值 int h = spread(key.hashCode()); //如果table初始化过 且 根据hash定位到对应位置的table上有数据 if ((tab = table) != nu原创 2021-06-13 12:00:25 · 234 阅读 · 0 评论 -
ConcurrentHashMap源码分析(一) ---- 插入,扩容,计算总数据量
先看到部分成员变量和常量 //map中最大的容量 private static final int MAXIMUM_CAPACITY = 1 << 30; //默认的容量 private static final int DEFAULT_CAPACITY = 16; //默认的扩容因子 private static final float LOAD_FACTOR = 0.75f; //节点从链表升级为红黑树的阈值 static final int TREEIFY_原创 2021-06-13 11:32:56 · 258 阅读 · 0 评论 -
ThreadPoolExecutor --- 线程池源码分析
首先看到ThreadPoolExecutor类中的一些成员变量 //ctl 初始为 RUNNING 且 线程池中没有线程 111+29个0 private final AtomicInteger ctl = new AtomicInteger(ctlOf(RUNNING, 0)); //COUNT_BITS 32 - 3 = 29 private static final int COUNT_BITS = Integer.SIZE - 3; //线程池最大容量 前3位位0 后2原创 2021-05-30 19:12:15 · 181 阅读 · 0 评论 -
Semaphore源码解析
先看到Semaphore的构造方法 //该方法默认非公平锁 public Semaphore(int permits) { sync = new NonfairSync(permits); } public Semaphore(int permits, boolean fair) { sync = fair ? new FairSync(permits) : new NonfairSync(permits); }看到Semaphore的acqu原创 2021-05-28 19:10:10 · 171 阅读 · 0 评论 -
ReentrantReadWriteLock源码解析
先看到构造方法 public ReentrantReadWriteLock() { //无惨构造函数,默认非公平锁 this(false); } public ReentrantReadWriteLock(boolean fair) { sync = fair ? new FairSync() : new NonfairSync(); readerLock = new ReadLock(this); writerLock原创 2021-05-28 16:24:29 · 86 阅读 · 0 评论 -
ReentrantLock源码解析(二)----- 条件队列
直接看到aqs的ConditionObject类的await方法。 public final void await() throws InterruptedException { if (Thread.interrupted()) throw new InterruptedException(); //将当前线程包装为node,添加到条件队列中(现在节点是在条件等待队列中) Node no原创 2021-05-27 21:45:31 · 206 阅读 · 0 评论 -
ReentrantLock源码解析(一)----- lock和unlock方法
看到ReentrantLock构造方法 //无参构造函数为非公平锁 public ReentrantLock() { sync = new NonfairSync(); } //可以传递一个boolean变量来选择使用公平锁或非公平锁 public ReentrantLock(boolean fair) { sync = fair ? new FairSync() : new NonfairSync(); }先看到lock方法原创 2021-05-27 16:15:03 · 180 阅读 · 0 评论 -
synchronized和锁对象
首先来看到对象头的MarkWord属性偏向锁写段代码,来查看偏向锁的锁对象头的属性import lombok.extern.slf4j.Slf4j;import org.openjdk.jol.info.ClassLayout;@Slf4j(topic = "test")public class MyTest { public static void main(String[] args) throws InterruptedException { //由于有偏向延迟,休眠原创 2021-05-24 18:28:22 · 297 阅读 · 0 评论