爲什麼有一個的AtomicInteger在DefaultThreadFactory的AtomicInteger在threadDefaultFactory
private static final AtomicInteger poolNumber = new AtomicInteger(1);
只是因爲它增加和存儲值還是其他原因呢?
爲什麼有一個的AtomicInteger在DefaultThreadFactory的AtomicInteger在threadDefaultFactory
private static final AtomicInteger poolNumber = new AtomicInteger(1);
只是因爲它增加和存儲值還是其他原因呢?
因爲它可以自動遞增。
遞增一個AtomicInteger
包括三個操作:
這三個動作被作爲一個原子執行的增加的值操作。
這意味着另一個線程將不能介入,例如,在步驟1和2之間或在步驟2和3之間並且破壞計數。這種行爲使得AtomicInteger
可以安全地用於多線程環境。
該靜態值在DefaultThreadFactory的構造函數中遞增。用戶可以在不同的線程中創建工廠實例,並且增量可能會丟失,所以AtomicInteger使這種方法成爲線程安全的。
如果我有ThreadPoolExecutor在其中我將給我的自定義ThreadFactory我應該使用AtomicInteger或不?增加它自動罰款,但有沒有其他原因?/ –
原子,而不是自動。 –
爲什麼實際上當一個線程完成新線程創建時,ThreadFactory會被污染?我的意思是伯爵。我們可以存儲它以前的值並增加它 –