简体   繁体   中英

What is the Java equivalent for C# `Interlocked.Exchange(Object, Object) : Object`?

What is the Java equivalent for C# Interlocked.Exchange(Object, Object) : Object ? Is there a way in Java to perform the following actions in a single atomic step without lock : 1) store locally the reference of a variable 2) set another reference to the same variable?

There's no operation to do this for an arbitrary variable, as far as I'm aware... but this is what the AtomicReference type is for:

private AtomicReference<String> stringReference;

...

String oldValue = stringReference.getAndSet(newValue);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM