In the getAndIncrement method of the AtomicInteger class,the getAndAddInt method of the unsafe class is called,which contains the compareAndSwapInt method and the spin(do...while). The compareAndSwapInt method is thread-safe,so Why not just change the value in the compareAndSwapInt method, but also need the spin to be safe? Thank you for your answer.
What is the meaning of the spin in the getAndIncrement method in the AtomicInteger class?
100 views Asked by 黄慧茹 At
1
There are 1 answers
Related Questions in JAVA
- I need the BIRT.war that is compatible with Java 17 and Tomcat 10
- Creating global Class holder
- No method found for class java.lang.String in Kafka
- Issue edit a jtable with a pictures
- getting error when trying to launch kotlin jar file that use supabase "java.lang.NoClassDefFoundError"
- Does the && (logical AND) operator have a higher precedence than || (logical OR) operator in Java?
- Mixed color rendering in a JTable
- HTTPS configuration in Spring Boot, server returning timeout
- How to use Layout to create textfields which dont increase in size?
- Function for making the code wait in javafx
- How to create beans of the same class for multiple template parameters in Spring
- How could you print a specific String from an array with the values of an array from a double array on the same line, using iteration to print all?
- org.telegram.telegrambots.meta.exceptions.TelegramApiException: Bot token and username can't be empty
- Accessing Secret Variables in Classic Pipelines through Java app in Azure DevOps
- Postgres && statement Error in Mybatis Mapper?
Related Questions in COMPARE-AND-SWAP
- Thread-safe lock-free min where both operands can change c++
- How to get Word VBA Convert Selection Range from one Shade to another Confined to End of Selected Range?
- C++ atomics Correct memory order and thread fencing for a shared_ptr implementation
- Is it safe to synchronize access to non-Sync data using only a separate atomic variable?
- How to implement a SpinLock
- Is my code thread-safe? [Java, CAS, money transfer]
- x86: Instruction reorder related with `cmpxchg` (without lock prefix)?
- Are _Atomic members of unions a good idea?
- Understanding of C++'s std::atomic<T> and compare-and-swap
- why are CAS / other common operations limited to fractions of a cache line?
- Can we use Compare-and-Swap operation on non-atomic variable?
- What happens if `compare_exchange_weak` is called on a dangling pointer? How is the code in my textbook safe?
- How to determine whether the minimum number of adjacent swaps required for sorting is odd or even?
- How can I check for null at a specific index of a String Array to change its value?
- How to use atomic operations with user-space variable in BPF?
Related Questions in ATOMICINTEGER
- Incorect value of incremented value inside lambda and new Thread
- Shared counter concurrent incrementation using atomic integer
- Java Thread Atomic Integer checking not working?
- multi level filtering by index of customized list in java 11
- Can we achieve visibility and atomicity using Volatile and Synchronized together?
- How to use AtomicInteger getAndSet correctly?
- ForkJoinFramework with AtomicLong is not giving consistent result
- AtomicInteger & lambda expressions in single-threaded app
- Compare and swap value in atomic integer kotlin
- Is each cycle of a for loop an atomic operation?
- Atomic Variables Accessed within inner classes
- Java 8 variable should be final or effectively final issue
- How to do comparison and adding AtomicInteger
- Java Volatile variable still causing race condition
- Thread safety if loop is thread safe
Popular Questions
- How do I undo the most recent local commits in Git?
- How can I remove a specific item from an array in JavaScript?
- How do I delete a Git branch locally and remotely?
- Find all files containing a specific text (string) on Linux?
- How do I revert a Git repository to a previous commit?
- How do I create an HTML button that acts like a link?
- How do I check out a remote Git branch?
- How do I force "git pull" to overwrite local files?
- How do I list all files of a directory?
- How to check whether a string contains a substring in JavaScript?
- How do I redirect to another webpage?
- How can I iterate over rows in a Pandas DataFrame?
- How do I convert a String to an int in Java?
- Does Python have a string 'contains' substring method?
- How do I check if a string contains a specific word?
Popular Tags
Trending Questions
- UIImageView Frame Doesn't Reflect Constraints
- Is it possible to use adb commands to click on a view by finding its ID?
- How to create a new web character symbol recognizable by html/javascript?
- Why isn't my CSS3 animation smooth in Google Chrome (but very smooth on other browsers)?
- Heap Gives Page Fault
- Connect ffmpeg to Visual Studio 2008
- Both Object- and ValueAnimator jumps when Duration is set above API LvL 24
- How to avoid default initialization of objects in std::vector?
- second argument of the command line arguments in a format other than char** argv or char* argv[]
- How to improve efficiency of algorithm which generates next lexicographic permutation?
- Navigating to the another actvity app getting crash in android
- How to read the particular message format in android and store in sqlite database?
- Resetting inventory status after order is cancelled
- Efficiently compute powers of X in SSE/AVX
- Insert into an external database using ajax and php : POST 500 (Internal Server Error)
What source are you looking at? At least since JDK11, and still in the current JDK16, the implementation of
Unsafe'sgetAndAddIntis:I have no idea where you got
compareAndSwapIntfrom.The above code has a spin because that's how CAS always works. (Compare-And-Set): That code says, with for example an AtomicInteger that is currently '5' and you are trying to add 2 to it: