Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
请告知 java.util.concurrent.atomic 包是实现同步的另一种选择,假设有一个变量并且 10 个不同的线程正在递增它,那么在这种情况下,原子整数在同步上下文中的作用是什么......!
如果您只有一个整数要递增,那么可以 -AtomicInteger允许您轻松、高效且稳健地实现这一目标,而无需同步。
AtomicInteger
如果您需要做的不止这些——例如,如果您实际上需要更新多个变量,并且必须将它们作为一个单元进行更新,那么您会想要使用同步。