skywalker

Results 11 issues of skywalker

代码示例: ```java public enum StatusEnum { CREATING(0), PUBLISHED(1), FINISHED(2); private final int status; StatusEnum(int status) { this.status = status; } public int getStatus() { return status; } private static final...

读了[一次“错误”的并发控制引发的思考](https://zhuanlan.zhihu.com/p/78310087)一文,觉得有些疑问。对于下面的代码: ```java class MultiProcessorTask { private boolean flag = true; public void runMethod() { while (flag) { synchronized (new Simple(1)){} } } public void stopMethod() { System.out.println("change 'flag' field ...");...

# JMM 根据: [The JSR-133 Cookbook for Compiler Writers](http://gee.cs.oswego.edu/dl/jmm/cookbook.html),Java内存模型定义了四种内存屏障: 1. LoadLoad 2. StoreStore 3. LoadStore 4. StoreLoad 其实就是load和store操作的全排列。 # Linux 而Linux定义了三种内存屏障: 1. 读屏障`smp_rmb` 2. 写屏障`smp_wmb` 3. 通用屏障`smp_wb` # 关系 从字面上来看,`smp_rmb`对应LoadLoad,`smp_wmb`对应StoreStore,`smp_mb`对应LoadStore和StoreLoad,这样来说Java的定义更加细致一些,区分了Load和Store重排的两个不同方向。...

# 锁还是打印 在 #11 的基础上,把代码再改写为: ```java package test; class MultiProcessorTask { private boolean flag = true; public void runMethod() { while (flag) { System.out.println(1); } } public void stopMethod() throws...

通常情况下关于volatile用法的正确例子是这样: ```java private volatile boolean flag = true; @org.junit.Test public void testVolatile() { new Thread(() -> { try { System.out.println("子线程启动"); TimeUnit.SECONDS.sleep(3); flag = false; System.out.println("flag false"); } catch (InterruptedException ignore)...

来自: [Is Parallel Programming Hard, And, If So, What Can You Do About It?](https://mirrors.edge.kernel.org/pub/linux/kernel/people/paulmck/perfbook/perfbook-1c.2019.12.22a.pdf) # 我们能信任的东西 即所有CPU(不同架构)均遵从的准则: 1. 一个特定的CPU的所有访问操作都与该CPU上的程序顺序是一致的。 2. **所有CPU对单个变量的访问都与存储这个变量的全局顺序有一些一致性**。 3. 内存屏障成对操作。 4. 互斥锁原语提供这样的手段。 第二条有意思,暂时不太准确理解其意思。几句耐人寻味的原文摘抄过来: > Because current commercially...

# 加锁 以NonfairSync为例: ```java final void lock() { if (compareAndSetState(0, 1)) setExclusiveOwnerThread(Thread.currentThread()); else acquire(1); } ``` 关键在于compareAndSetState方法: ```java /** * Atomically sets synchronization state to the given updated * value...

精度问题出现的本质原因是浮点数无法精确的表示大多数十进制小数,以下面的计算为例: ```java System.out.println(81.6 * 10); System.out.println(81.6 * 100); ``` 其输出结果是: ```java 816.0 8159.999999999999 ``` 数字81.6在写出来之后就不是精确表示了,随后的计算所以也随之不准确。下面的除法是准确的: ```java System.out.println(816 / 100D); ``` 因为对于整数,816和100都是可以精确表示的,所以最后的结果便是对的。

In real-life scenarios, we need to configure one of the options individually, rather than having to turn them on or off simultaneously, thanks.

If we create a AsyncHttpClient instance with below code: ```java DefaultAsyncHttpClientConfig.Builder cfgBuilder = new DefaultAsyncHttpClientConfig.Builder(); cfgBuilder.setDisableHttpsEndpointIdentificationAlgorithm(true); AsyncHttpClient client = AsyncHttpClientFactory.getAsyncHttpClient(cfgBuilder.build()); ``` The actual behavior of `getAsyncHttpClient` method is to call...