V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX  ›  fantastM  ›  全部回复第 8 页 / 共 17 页
回复总数  326
1  2  3  4  5  6  7  8  9  10 ... 17  
2020-05-26 17:35:14 +08:00
回复了 hlwjia 创建的主题 English [一周年] 我也来带带各位想学英语的 v 友吧 [第五帖 ]
#二群# bTczOTI2MTc1Nw==
2020-05-15 14:14:38 +08:00
回复了 xiaobaobao 创建的主题 Java Synchronized 的轻量锁
有篇 OpenJDK 文章可以先看一下 https://wiki.openjdk.java.net/display/HotSpot/Synchronization

相关的 JVM 实现代码可以看这里 http://hg.openjdk.java.net/jdk8/jdk8/hotspot/file/87ee5ee27509/src/share/vm/oops/markOop.hpp

按照 OpenJDK 的说法,JVM 是这样实现的(先不考虑偏向锁):

`In the Java HotSpot™ VM, every object is preceded by a class pointer and a header word. The header word, which stores the identity hash code as well as age and marking bits for generational garbage collection, is also used to implement a thin lock scheme.` Java 中的每个对象都有关联的 class pointer 和 header word,Java 是用 header word 来实现 thin lock 的。

`As long as an object is unlocked, the last two bits have the value 01. ` 当对象没有被锁定的时候,header word 最后两位是 01 。

`When a method synchronizes on an object, the header word and a pointer to the object are stored in a lock record within the current stack frame. Then the VM attempts to install a pointer to the lock record in the object's header word via a compare-and-swap operation.` 当对象被 synchronized 的时候,这个对象的 header word 和 class pointer 会先被存储到当前线程 stack frame 中(类似于先保存一下数据的副本),这条栈帧被叫 lock record 。然后 JVM 尝试通过 CAS 操作,把 lock record 的指针记录在对象原先的 header word (前几位)中。

`If it succeeds, the current thread afterwards owns the lock. Since lock records are always aligned at word boundaries, the last two bits of the header word are then 00 and identify the object as being locked.` 如果这个 CAS 操作成功,就表示当前线程获取到了锁,对象的 header word 的最后两位会被设置成 00 。

`If the compare-and-swap operation fails because the object was locked before, the VM first tests whether the header word points into the method stack of the current thread.` 如果这个 CAS 操作失败,JVM 会先检查一下对象的 header word (前几位)里的 lock record 指针是否指向了当先线程的方法栈。(如果是的话,这个对象应该是 recursively locked object,这也就是 synchronized 的可重入特性;如果不是的话,就表示有多个线程在竞争这个对象的锁。)

`Only if two different threads concurrently synchronize on the same object, the thin lock must be inflated to a heavyweight monitor for the management of waiting threads.` 如果有两个不同的线程在竞争同一个对象的锁,thin lock 升级成 heavyweight monitor 。

Java 对象中 header word 的 bit format,可以看第二个链接的 37-54 行。
@maichael #1 其实这两个我都有搜到过,第二个还不支持仅显示选中的行,第一个的话我没搞懂它的 slice 参数是怎么用的......我一直没试成功

我是想着,既然 GitHub 它都支持把 markdown 里的链接渲染成仓库里的代码片段了,就想问问各位有没有什么方法可以直接用它的这个内部功能
2020-05-06 16:39:00 +08:00
回复了 fantastM 创建的主题 Java 热心翻译了 Google Java Style Guide
@hantsy #11 谷歌的风格对代码块要求是缩进 2 个空格 https://google.github.io/styleguide/javaguide.html#s4.2-block-indentation,但是对连续的换行(例如 Stream 的链式调用)要求是缩进 4 个空格 https://google.github.io/styleguide/javaguide.html#s4.5.2-line-wrapping-indent

其实我自己也是主张 4 个空格的...
2020-05-06 14:47:07 +08:00
回复了 fantastM 创建的主题 Java 热心翻译了 Google Java Style Guide
@hantsy #5 我倒是知道谷歌还有个类似于 Golang 中 go fmt 命令的项目 https://github.com/google/google-java-format
2020-05-06 13:10:02 +08:00
回复了 fantastM 创建的主题 Java 热心翻译了 Google Java Style Guide
2020-04-22 00:19:43 +08:00
回复了 noble4cc 创建的主题 游戏 为什么玩游戏再也找不到当初的感觉了?
哎,这个游戏的各个版本,前前后后加起来,我玩了十遍不止 https://i.imgur.com/2b2ejpI.png
2020-04-20 11:43:09 +08:00
回复了 mikemintang 创建的主题 Go 编程语言 基于「Hugo」搭建个人博客网站
我最近也把博客迁移到了 Hugo,不过根据自己的需求,加了挺多个性化配置...

https://blog.fantasticmao.cn/2020/04/08/这个博客的构建和部署 /
2020-04-19 22:23:10 +08:00
回复了 yangyuhan12138 创建的主题 Java 关于 ClassLoader 的一些疑问
「我们直接 new 的时候」这时候程序已经运行在一个被 ClassLoader 加载的类里了,默认就会用这个 ClassLoader 去加载当前类依赖的还没有被加载的其它类。
2020-04-19 21:49:32 +08:00
回复了 yangyuhan12138 创建的主题 Java 关于 ClassLoader 的一些疑问
你想问是的 ClassLoader A 加载了 Class A,为什么在没有显式声明使用 ClassLoader A 的情况下就可以加载 Class A 依赖的 Class B 吗
http://hedengcheng.com/?p=771 这是原文,但图片已经失效了,可以看些转载的文章
2020-03-22 23:11:58 +08:00
回复了 meefly 创建的主题 分享创造 👴开发了 1️⃣🈹7️⃣🧗功能,🦡🦅🦁️用
如果是从 emoji 翻译回来的,我倒是会用一下...
2020-03-20 11:18:41 +08:00
回复了 jwenjian 创建的主题 Java Java socket 程序奇怪的现象
是用 Netty 的吗?代码贴上来看看
2020-03-14 10:26:18 +08:00
回复了 purensong 创建的主题 程序员 Java 程序员进来回答一下
@justin2018 哈哈,这个梗已经非常有名了吧
2020-02-20 13:32:57 +08:00
回复了 SlipStupig 创建的主题 程序员 被 spam 评论给弄疯了,如何有效的建立机器学习模型呢?
学习一下 t66y 社区的管理方式
2020-02-20 02:46:03 +08:00
回复了 mightofcode 创建的主题 程序员 3 分钟让你拥有一个跟王垠一样的博客
王垠不会介意吗...
1  2  3  4  5  6  7  8  9  10 ... 17  
关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1178 人在线   最高记录 6543   ·     Select Language
创意工作者们的社区
World is powered by solitude
VERSION: 3.9.8.5 · 42ms · UTC 18:20 · PVG 02:20 · LAX 11:20 · JFK 14:20
Developed with CodeLauncher
♥ Do have faith in what you're doing.