Poison


  • 首页

  • 归档

  • 标签

  • 搜索
close
Poison

LineNumberTable

发表于 2022-03-04

今天在调试时又遇到行号无法对齐的问题,因为之前开发 Java Agent 时遇到过,常见于源码 jar 与运行时 jar 不一致导致,本文简要记录。

阅读全文 »
Poison

OmitStackTraceInFastThrow

发表于 2022-03-02

今天有一台服务器频繁打印 java.lang.NullPointerException,但是不包含栈帧信息,起初我以为是应用层代码使用 log4j2 打印时直接调用了 log.error(e) 进行打印,因为该方法最终会调用异常对象的 toString() 方法然后打印出异常信息,对于 java.lang.NullPointerException 实例,最终调用的 java.lang.Throwable#toString 获取到需要打印的字符串:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/**
* Returns a short description of this throwable.
* The result is the concatenation of:
* <ul>
* <li> the {@linkplain Class#getName() name} of the class of this object
* <li> ": " (a colon and a space)
* <li> the result of invoking this object's {@link #getLocalizedMessage}
* method
* </ul>
* If {@code getLocalizedMessage} returns {@code null}, then just
* the class name is returned.
*
* @return a string representation of this throwable.
*/
public String toString() {
String s = getClass().getName();
String message = getLocalizedMessage();
return (message != null) ? (s + ": " + message) : s;
}
阅读全文 »
Poison

rangeCheck

发表于 2022-03-01

如果看过 JDK 8 中 ArrayList 的源码,你会发现存在如下代码 ArrayList.java at jdk8-b120:

1
2
3
4
5
6
7
8
9
10
/**
* Checks if the given index is in range. If not, throws an appropriate
* runtime exception. This method does *not* check if the index is
* negative: It is always used immediately prior to an array access,
* which throws an ArrayIndexOutOfBoundsException if index is negative.
*/
private void rangeCheck(int index) {
if (index >= size)
throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
}
阅读全文 »
Poison

Striped

发表于 2022-03-01

在业务中曾有为不同的 key 的操作实现锁语义的需求,常见的实现为构建一个线程安全的 Map 实现然后为每个 key 创建对应的对象用于互斥锁,但是该实现存在内存泄漏的问题,即随着不同 key 不停地出现,该 Map 实例会越来越大,之前为每个 key 创建的对象一直驻留在内存中,即使 key 永远不再出现。比如 JDK 7 引入的支持并行类加载的特性 Multithreaded Custom Class Loaders in Java SE 7 中,引入了如下代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
// Maps class name to the corresponding lock object when the current
// class loader is parallel capable.
// Note: VM also uses this field to decide if the current class loader
// is parallel capable and the appropriate lock object for class loading.
private final ConcurrentHashMap<String, Object> parallelLockMap;

/**
* Returns the lock object for class loading operations.
* For backward compatibility, the default implementation of this method
* behaves as follows. If this ClassLoader object is registered as
* parallel capable, the method returns a dedicated object associated
* with the specified class name. Otherwise, the method returns this
* ClassLoader object.
*
* @param className
* The name of the to-be-loaded class
*
* @return the lock for class loading operations
*
* @throws NullPointerException
* If registered as parallel capable and {@code className} is null
*
* @see #loadClass(String, boolean)
*
* @since 1.7
*/
protected Object getClassLoadingLock(String className) {
Object lock = this;
if (parallelLockMap != null) {
Object newLock = new Object();
lock = parallelLockMap.putIfAbsent(className, newLock);
if (lock == null) {
lock = newLock;
}
}
return lock;
}
阅读全文 »
Poison

ConcurrentHashMap

发表于 2022-02-20

本文主要记录 JDK 12 中 ConcurrentHashMap 的扩容实现。首先需要关注的为 sizeCtl 变量,其定义如下:

1
2
3
4
5
6
7
8
9
/**
* Table initialization and resizing control. When negative, the
* table is being initialized or resized: -1 for initialization,
* else -(1 + the number of active resizing threads). Otherwise,
* when table is null, holds the initial table size to use upon
* creation, or 0 for default. After initialization, holds the
* next element count value upon which to resize the table.
*/
private transient volatile int sizeCtl;
阅读全文 »
1…789…28

138 日志
126 标签
GitHub LeetCode
© 2026 Poison 蜀ICP备16000644号
由 Hexo 强力驱动
主题 - NexT.Mist