V2EX azygote
 azygote 最近的时间轴更新
azygote

azygote

  Amazon.com / Software Development Engineer
V2EX 第 243985 号会员,加入于 2017-07-23 15:50:32 +08:00
今日活跃度排名 11938
azygote 最近回复了
2020-11-19 15:40:04 +08:00
回复了 levizheng 创建的主题 Java 咨询一个关于 synchronized 问题
@levizheng 仔细读这段 "Due to the semantics of some programming languages, the code generated by the compiler is allowed to update the shared variable to point to a partially constructed object before A has finished performing the initialization. For example, in Java if a call to a constructor has been inlined then the shared variable may immediately be updated once the storage has been allocated but before the inlined constructor initializes the object.[6]"
如果不加 volatile, 有个线程对 uniqueInstance 初始化到一半,另外一个线程执行到 synchronized 外面的那个 if (uniqueInstance == null) 是可能返回 false 的,也就读到的不是 null,而是一个初始化了一半的对象。
2020-11-19 15:27:36 +08:00
回复了 aiqier 创建的主题 程序员 Java 的 web 服务会在什么情况下读磁盘?
如果日志框架是 log4j/log4j2, 然后启用了 rolling file appender 的话,是会向硬盘写日志的
2020-11-19 15:15:35 +08:00
回复了 levizheng 创建的主题 Java 咨询一个关于 synchronized 问题
volatile 的作用主要是为了 synchronized 外面那个 if 的。如果不加 volatile 的话,会出现一个情况:有一个线程正在对 uniqueInstance 赋值,由于没有 volatile 关键字,uniqueInstance 可能返回一个初始化一半了的对象,然后这时恰好另外一个线程正好执行到 if (uniqueInstance == null),得到结果为 false 然后直接返回, 但实际上这个 uniqueInstance 读到的值并没有初始化完成,导致返回了初始化了一半的对象,最后造成程序 crash 。

引自维基百科: https://en.wikipedia.org/wiki/Double-checked_locking

Intuitively, this algorithm seems like an efficient solution to the problem. However, this technique has many subtle problems and should usually be avoided. For example, consider the following sequence of events:

Thread A notices that the value is not initialized, so it obtains the lock and begins to initialize the value.

Due to the semantics of some programming languages, the code generated by the compiler is allowed to update the shared variable to point to a partially constructed object before A has finished performing the initialization. For example, in Java if a call to a constructor has been inlined then the shared variable may immediately be updated once the storage has been allocated but before the inlined constructor initializes the object.[6]

Thread B notices that the shared variable has been initialized (or so it appears), and returns its value. Because thread B believes the value is already initialized, it does not acquire the lock. If B uses the object before all of the initialization done by A is seen by B (either because A has not finished initializing it or because some of the initialized values in the object have not yet percolated to the memory B uses (cache coherence)), the program will likely crash.
估计是内存泄露了
你需要这个包 https://github.com/FasterXML/jackson-datatypes-collections 来进行 Guava 里面的一些 Collections 类的序列 /反序列化
基本使用 Waze, 偶尔用 Google Map, 坐标美帝
回溯法
关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     926 人在线   最高记录 6679       Select Language
创意工作者们的社区
World is powered by solitude
VERSION: 3.9.8.5 19ms UTC 19:34 PVG 03:34 LAX 12:34 JFK 15:34
Do have faith in what you're doing.
ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86