State of Loom
(cr.openjdk.java.net)
8 則討論
IngramChen
張貼於
比較在意 ThreadLocal 關聯的應用場景..
var oldValue = myTL.get();
myTL.set(newValue);
try {
...
} finally {
myTL.set(oldValue);
}
Without this imposed structure, when a thread is shared among multiple tasks, one task’s TL values might leak into another. Virtual threads solve that problem by being lightweight enough to not require sharing.
沒看懂,為何 lightweight 就不用 sharing.
他是說以前一定要寫 finally 去回溯/移除 自己放在 ThreadLocal 的變數,因為 Thread 多半有 pool,是大家共用的。
之後用 virtual thread 就沒人在共用了,所以 ThreadLocal 不用怕碰到別人塞的值,也不用 finally 去處理。
而你講的 sharing 是該 thread 被用的當下,很多人都想塞變數,這個的確沒變,要靠 Scope Variable 的結構減少忘記互蓋的問題。
阿對齁,他必須得讓 ThreadLocal 在 virtual thread 中行為像之前一樣,了解了。