azygote 最近的时间轴更新
azygote

azygote

🏢  Amazon.com / Software Development Engineer
V2EX 第 243985 号会员,加入于 2017-07-23 15:50:32 +08:00
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.
2020-09-24 16:44:59 +08:00
回复了 selfcreditgiving 创建的主题 问与答 什么情况,占用内存 2 个 G! springboot 这么耗资源嘛
估计是内存泄露了
2020-07-10 15:50:35 +08:00
回复了 JasonLaw 创建的主题 Java Jackson 序列化时,如何将 final 类型的类型信息保存起来?
你需要这个包 https://github.com/FasterXML/jackson-datatypes-collections 来进行 Guava 里面的一些 Collections 类的序列 /反序列化
2020-02-10 15:35:19 +08:00
回复了 dik88chen 创建的主题 问与答 想问一下大家, 2020 年 IOS 用户大家在用哪个导航 APP。
基本使用 Waze, 偶尔用 Google Map, 坐标美帝
2018-11-28 01:41:24 +08:00
回复了 Vegetables 创建的主题 算法 如何在 数组 [0, 1, 2, 3, ..., n] 中选取 m 个不相邻的整数?
回溯法
关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   3668 人在线   最高记录 6679   ·     Select Language
创意工作者们的社区
World is powered by solitude
VERSION: 3.9.8.5 · 15ms · UTC 04:23 · PVG 12:23 · LAX 20:23 · JFK 23:23
Developed with CodeLauncher
♥ Do have faith in what you're doing.