
并发
文章平均质量分 68
自驱
ALOHA HEJA HE
展开
-
【golang】用semaphore 实现一致性 worker pool 【普通版】
【代码】【golang】用semaphore 实现一致性 worker pool 【普通版】原创 2022-10-15 20:36:40 · 265 阅读 · 1 评论 -
【golang】分别用 sync.WaitGroup 和 semaphore 实现 worker pool 【普通版】
【代码】【golang】分别用 sync.WaitGroup 和 semaphore 实现 worker pool 【普通版】原创 2022-10-15 19:57:32 · 343 阅读 · 0 评论 -
Why does Facebook use delete to remove the key-value pair in Cache instead of updating the Cache?
Just imagine what if two concurrent updates of the same data element occur? You might have different values of the same data item in DB and in memcached. Which is bad. There is a certain number of ways to avoid or to decrease probability of this. Here is t原创 2021-07-01 09:42:07 · 244 阅读 · 0 评论 -
【锁】Synchronized轻量级锁自旋,真的错了?!
1 文章结构 Synchronized 相关class文件知识介绍以及加锁流程图 锁升级--本文核心自旋 源码解析 小结 2Synchronized 相关class文件知识介绍(多图预警) 0 Synchronized底层队列(摘自网络图片)和类源码结构 _cxq 竞争线程首先入队,竞争失败再进入_EntryList堵塞 Blocked 队列节点类型 1 锁和MarkWord关系,自Hotspot wiki 2 网上流传的一张图,摘自网络,这个图流程轻量级锁流程部分有很...原创 2021-03-13 14:55:03 · 641 阅读 · 0 评论 -
【LongAdder】Sentinel 使用的黑科技
LongAdder 源码 LongAdder 到底用了什么黑科技能做到高性比 AtomicLong 还要好呢呢?对于同样的一个 add() 操作,上文说到 AtomicLong 只对一个 Long 值进行 CAS 操作。而 LongAdder 是针对 Cell 数组的某个 Cell 进行 CAS 操作 ,把线程的名字的 hash 值,作为 Cell 数组的下标,然后对 Cell[i] 的 long 进行 CAS 操作。简单粗暴的分散了高并发下的竞争压力。 add() 操作步骤: 如果 cells.原创 2021-03-03 19:02:24 · 352 阅读 · 1 评论 -
【Thread】Thread并发知识 JUC all-in-one
1 并发知识库 2 JAVA 线程实现/创建方式 继承Thread类,native start()方法; 实现Runnable接口(自己的类已经extends 另一个情况,new Thread()); ExecutorService、Callable<Class>、Future 有返回值线程(有返回值的任务必须实现 Callable 接口,类似的,无返回值的任务必须 Runnable 接口) 基于线程池的方式(线程和数据库连接这些资源都是非常宝贵的资源。那么每次需要的时候创建,不需原创 2021-02-18 14:49:19 · 314 阅读 · 0 评论 -
【MAP】 HashMap ConcurrentHashMap all-in-one
0 文章结构 HashMap 1.7 vs 1.8 ConcurrentHashMap 1.7 vs 1.8 1 HashMap 1.7 1.8(数组+链表OR红黑树) HashMap 根据键的 hashCode 值存储数据,大多数情况下可以直接定位到它的值,因而具有很快 的访问速度,但遍历顺序却是不确定的。 HashMap 最多只允许一条记录的键为 null,允许多条记 录的值为 null。HashMap 非线程安全,即任一时刻可以有多个线程同时写 HashMap,可能会导 致数据的不...原创 2021-02-17 21:53:15 · 2040 阅读 · 5 评论 -
[AQS]AQS 中断响应和不可中断 CLH 队列 对比
1 AQS 中断响应和不可中断 CLH 队列变化对比 2 模拟第二种情况,中断响应 public boolean tryLock(long timeout, TimeUnit unit) throws InterruptedException 以java.util.concurrent.locks.AbstractQueuedSynchronizer#doAcquireNanos 为例,响应中断方式获取时.如果超时返回false,并执行java.util.concurrent.locks.Abst原创 2021-01-31 15:35:07 · 377 阅读 · 0 评论 -
【JVM】线上用户线程执行SQL停顿的5点猜想,并最终解决
1 现象 正常应用,出现某个sql偶尔卡顿,而sql都是根据Id主键查询的不会因为索引误用等引起。 排除: Sql 索引没有用上或者选择错误的索引。 函数操作导致索引失效。 表过大,多表多库联合查询。 2 偶尔慢的原因: 猜想 2.1 数据库在刷新脏页。------通过监控看到mysql集群的磁盘刷新次数都是平稳的-排除。 猜想 2.2 数据库的自然数连接占满了。------ 通过druid 监控排除。 猜想 2.3 拿不到锁。排除--- 都是查...原创 2021-01-05 19:36:36 · 384 阅读 · 2 评论 -
【不懂就画一千零一夜】AQS 三个线程获取同一个锁时,CLH队列和Node waitStatus 状态快照
public class MyLockSupport { public static void main(String[] args) { Lock lock = new ReentrantLock(); new Thread(()->{ try{ lock.lock(); Thread.sleep(60000); } catch (Inte...原创 2020-11-20 10:48:19 · 447 阅读 · 0 评论 -
【面试】--三个线程轮流打印ABC
1 lock实现 public class ABCLock { private static Lock lock = new ReentrantLock(); private static Condition conditionA = lock.newCondition(); private static Condition conditionB = lock.ne原创 2020-08-16 15:13:53 · 369 阅读 · 0 评论 -
【AQS】队列同步器的应用举例
1【BooleanLatch】 当signal调用时,触发所有等待线程 import java.util.concurrent.locks.AbstractQueuedSynchronizer; class BooleanLatch { private static class Sync extends AbstractQueuedSynchronizer { Intege...原创 2019-12-15 10:07:33 · 347 阅读 · 0 评论 -
【HashMap】 putIfAbsent是不是原子性的更新操作?
putIfAbsent是不是原子性的更新操作? HashMap 不是,只有 ConcurrentHashMap是 因为使用了casTabAt 方法,机器原语级别的更新; 用到了arrayIndexScale方法也是一个本地方法,可以获取数组的转换因子,也就是数组中元素的增量地址。将arrayBaseOffset与arrayIndexScale配合使用,可以定位数组中每个元素在内存中的位置。 ...原创 2019-10-13 16:47:46 · 979 阅读 · 0 评论 -
【ConcurrentHashMap 1.8】 理解性知识整理
1【理解】 ConcurrentHashMap:检索操作(包括get)通常不会阻塞,因此可能与更新操作(包括put和remove)重叠,ConcurrentHashMap跟Hashtable类似但不同于HashMap,它不可以存放空值,key和value都不可以为null【null值用来判断是否需要加锁立即重试】。 ConcurrentHashMap从JDK1.5开始随java.util...原创 2019-10-13 11:42:16 · 482 阅读 · 0 评论 -
知识库--STM内存事务简介(118)
Shared mutable variables had to synchronize. Instead of relaxing and enjoying that feeling of a job well done, you probably had a nagging feeling of doubt, wondering whether you managed to synchronize原创 2017-02-19 12:03:35 · 399 阅读 · 0 评论 -
知识库--Creating Actors By Akka Using Java(139)
1 Creating Actors 2 Sending and Receiving Messages 3 Working with Multiple Actors 4 Coordinating Actors 5 Using Typed Actors 6 Typed Actors and Murmurs 7 Mixing Actors and STM 8 Using Transactor翻译 2017-03-12 17:21:56 · 471 阅读 · 0 评论 -
知识库--Sending and Receiving Messages By Akka Using Java(139)
1 Creating Actors 2 Sending and Receiving Messages 3 Working with Multiple Actors 4 Coordinating Actors 5 Using Typed Actors 6 Typed Actors and Murmurs 7 Mixing Actors and STM 8 Using Tra翻译 2017-03-12 19:26:25 · 366 阅读 · 0 评论 -
知识库--Working with Multiple Actors No Using Thread Pool (139)
1 Creating Actors 2 Sending and Receiving Messages 3 Working with Multiple Actors 4 Coordinating Actors 5 Using Typed Actors 6 Typed Actors and Murmurs 7 Mixing Actors and STM 8 Using Tra翻译 2017-03-12 22:07:53 · 282 阅读 · 0 评论 -
【知识库 】--Coordinating Actors In Java Use Akka(150)
背景:统计系统目录文件大小设计: 使用Akka actors 不同角色 执行相应的职责 如下图表达 实现: 【1】首先定义 SizeCollector 类可以接受与的消息类型 【2】遍历和计算文件大小的处理器 FileProcessor 【3】总和执行的动作 【4】启动import akka.actor.UntypedActor; import akka.actor.UntypedA原创 2017-03-23 08:34:40 · 279 阅读 · 0 评论 -
【知识库】--FutureTask 异步原理(242)
1 任务提交给线程池后,可以直接--此处可能堵塞--park /** * @throws CancellationException {@inheritDoc} */ public V get() throws InterruptedException, ExecutionException { int s = state; if原创 2017-06-27 14:29:20 · 260 阅读 · 0 评论 -
【面试】--三个线程轮流打印ABC
1 lock实现 public class ABCLock { private static Lock lock = new ReentrantLock(); private static Condition conditionA = lock.newCondition(); private static Condition conditionB = lock.ne原创 2017-11-19 17:03:41 · 1487 阅读 · 0 评论 -
后来者别动,线程安全的处理对象中的变量 (集合)Threading - synchronized(this)
关键词 :Threading - synchronized(this) 上下文:经常遇到项目中需要线程安全的处理对象中的一个变量(如回调函数集合),处理过程中其他线程不能操作该变量。如何实现呢? 解决方案:Threading - synchronized(this) ,检查变量在synchronized(this)同步原语中进行,如果检查通过使用local变量承接老的变量,并将老的变量设...原创 2019-03-21 15:29:32 · 218 阅读 · 0 评论 -
redis--事务提交后 server端是如何处理并发来的其他请求的?exec multi pipleline
答案:单线程按照指令顺序执行;即 server端执行事务时,其他指令是不能被执行的,注意:请求是可以进来的,只是指令排到事务指令集合的后面。 参考redis server端源码;C写;epoll 事件模型;多路复用技术;3个处理模型【请求流程,处理流程,响应流程】 测试代码:目的是模拟普通请求和事务型请求并发 @Autowired private RedisCacheUti...原创 2019-04-26 00:39:26 · 323 阅读 · 0 评论 -
【dubbo】负载均衡 RoundRobinLoadBalance Dubbo-2.6.5 提供的最新算法!线程安全性有什么影响?
1 如果阅读过源码,请忽略下面一大段描述: RoundRobinLoadBalance -------------------------------------------------------------------可忽略下面--------------------------------------------------------------------------------...原创 2019-06-22 00:33:26 · 1694 阅读 · 0 评论 -
【ConcurrentHashMap 1.7】 理解性知识整理
概念性知识: 1ConcurrentHashMap 介绍 从JDK1.5开始随java.util.concurrent包一起引入JDK中,在JDK8以前,ConcurrentHashMap都是基于Segment分段锁来实现的,在JDK8以后,就换成synchronized和CAS这套实现机制了。 2 问题 + 解释 2.0 key 和 value 都不能为null,...原创 2019-09-29 07:14:22 · 713 阅读 · 0 评论 -
【JVM 重排序】知识分类和认知
重排序 内存模型描述了程序的可能行为。具体的编译器实现可以产生任意它喜欢的代码 -- 只要所有执行这些代码产生的结果,能够和内存模型预测的结果保持一致。这为编译器实现者提供了很大的自由,包括操作的重排序。 编译器生成指令的次序,可以不同于源代码所暗示的“显然”版本。重排序后的指令,对于优化执行以及成熟的全局寄存器分配算法的使用,都是大有脾益的,它使得程序在计算性能上有了很大的提升。 重排序类...原创 2019-09-29 07:18:58 · 211 阅读 · 0 评论 -
知识库--Limitations of STM 【高并发写 not in favor】(137)
STM的优点 STM eliminates explicit synchronization. We no longer have to worry if we forgot to synchronize or if we synchronized at the wrong level. There are no issues of failing to cross the memory barr翻译 2017-03-10 08:32:58 · 505 阅读 · 0 评论 -
知识库--Akka 事务配置-using jvm(134)
Akka assumes a number of default settings, but it allows us to change these either programmatically or through the configuration file akka.conf.使用编程方式配置事务 We can programmatically change the settings o翻译 2017-03-07 08:46:50 · 362 阅读 · 0 评论 -
知识库--Creating Nested Transactions By Akka 内存嵌套事务(129)
public class Account { final private Ref<Integer> balance = new Ref<Integer>(); public Account(int initialBalance) { balance.swap(initialBalance);//自带事务 } public int getBalanc翻译 2017-03-02 08:28:33 · 483 阅读 · 0 评论 -
技能库--统计文件大小 + CountDownLatch(87)
使用countdownlatch工具public class CountdownLatch0 { private ExecutorService service; final private AtomicLong pendingFileVisits = new AtomicLong(); final private AtomicLong totalSize = new Ato原创 2017-01-20 08:48:59 · 305 阅读 · 0 评论 -
技能库--统计文件大小 + Executors+Future(86)
1 首先使用单线程来计算 文件的总大小public class TotalFileSizeSequential { private long getTotalSizeOfFilesInDir(final File file){ if(file.isFile()) return file.length(); final File[] chi原创 2017-01-19 23:55:48 · 563 阅读 · 0 评论 -
技能库--统计文件大小 + ExchangeData+BlockingQueue(89)
主线程负责不断的汇总结果,pool线程池负责explore每个文件或者目录,并不断的put结果到线程交换数据的–BlockingQueue package com.qunar.finance.paper.countFileSize;import java.io.File; import java.util.concurrent.*; import java.util.concurrent.atomi原创 2017-01-22 08:40:49 · 344 阅读 · 0 评论 -
知识库--Separation of Identity and State 身份和状态的分离(118)
//状态和身份的分离Quick, what is the price of Google stock? We may argue that the value of the stock changes by the minute when the market is open, but that is in some way just playing with words. To take a si翻译 2017-02-19 17:50:55 · 483 阅读 · 0 评论 -
知识库--面向对象编程的缺陷(The Deficiency of the Object Model)(118)
The Deficiency of the Object Model// 身份和状态—-合并 As Java programmers, we are well versed in object-oriented programming(OOP). But the language has greatly infulenced the way we model OO apps. OOP did no翻译 2017-02-19 16:57:51 · 342 阅读 · 0 评论 -
知识库--Synchronization Damns Concurrency(118)
Synchronization has some fundamental flaws.//同步缺陷还是不少的If we use it improperly or forget it totally, the changes made by a thread may not be visible to other threads. We often learn the hard way where w翻译 2017-02-19 15:09:04 · 303 阅读 · 0 评论 -
知识库--Creating Transaction in Java Using Akka(124)
For transactional support, extend the Atomic class and place code into the atomically() method of this class to wrap it into a transaction. To run transactional code, call the execute() method of the A翻译 2017-02-26 08:24:12 · 439 阅读 · 0 评论 -
知识库--Enhance Concurrency(加强并发性)(116)
We want to ensure that the synchronization happens at the right level for each class so we don’t compromise thread safety but still enjoy good concurrency.//同步一个对象or相应的互斥的方法 Synchronizing instances i原创 2017-02-17 08:31:09 · 361 阅读 · 0 评论 -
知识库--Ensure Atomicity 原子性保证(117)
Ensure AtomicityThe previous example had no explicit synchronization in code, but any related euphoria(辛福感) is short-lived. We can not avoid synchronization if our mutable state has more than one relat原创 2017-02-18 16:06:14 · 369 阅读 · 0 评论 -
知识库--Concurrency+ThreadPool+Executors(79)
阻塞系数=0.9 四核处理器public abstract class AbstractNAV { public static Map<String, Integer> readTickers() throws IOException { final BufferedReader reader = new BufferedReader(new原创 2017-01-12 20:16:25 · 245 阅读 · 0 评论 -
知识库--Concurrency+Determining the Number of Threads(76)
Determining the Number of Threads //线程数量是计算出来的 绝不是猜测出来的,dubbo的超时时间,重试次数亦如此For a large problem, we’d want to have at least as many threads as the number of available cores. This will ensure that as ma原创 2017-01-11 00:21:44 · 696 阅读 · 0 评论