CAS非阻塞锁和synchronized重量级锁比较

实验证明:在并发数为100时:同步消耗17113389ns,CAS消耗10796860ns,由于资源竞争相对较小,同步锁进行线程阻塞和唤醒切换以及用户态内核态间的切换操作额外浪费消耗cpu资源,而CAS操作自旋的几率相对较小,因此效率会高一些。在并发数10000时:同步消耗785700151ns,CAS消耗860302497ns,由于资源竞争比较严重,所以CAS操作自旋等待的几率比较大,造成浪费了许多cpu资源而效率低于同步锁,因此CAS这种非阻塞锁更适用于资源竞争比较小的场景,否则会适得其反。 <无> .CodeEntity .code_pieces ul.piece_anchor{width:25px;position:absolute;top:25px;left:-30px;z-index:1000;}.CodeEntity .code_pieces ul.piece_anchor li{width:25px;background: #efe;margin-bottom:2px;}.CodeEntity .code_pieces ul.piece_anchor li{border-left:3px #40AA63 solid;border-right:3px #efe solid;}.CodeEntity .code_pieces ul.piece_anchor li:hover{border-right:3px #40AA63 solid;border-left:3px #efe solid;}.CodeEntity .code_pieces ul.piece_anchor li a{color: #333;padding: 3px 10px;}.CodeEntity .code_pieces .jump_to_code{visibility:hidden;position:relative;}.CodeEntity .code_pieces .code_piece:hover .jump_to_code{visibility:visible;}.CodeEntity .code_pieces .code_piece:hover .jump_to_code a{text-decoration:none;}.CodeEntity .code_pieces h2 i{float:right;font-style:normal;font-weight:normal;}.CodeEntity .code_pieces h2 i a{font-size:9pt;background: #FFFFFF;color:#00A;padding: 2px 5px;text-decoration:none;}

CAS非阻塞锁和synchronized重量级锁比较

相关文章:

你感兴趣的文章:

标签云: