In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
Editor to share with you what the java atomic face test questions are, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!
Overview
An atomic operation is an operation that will not be interrupted by a thread scheduling mechanism. Once the operation starts, it runs to the end without any thread context switching.
Atomic operation can be one step or multiple operation steps, but its order can not be disrupted, nor can it be cut and only part of it can be carried out. It is the core feature of atomicity to regard the whole operation as a whole.
Many atomic classes are provided in java. Here, the author mainly divides these atomic classes into four categories.
Atomic update base type or reference type
If it is a basic type, replace its value, if it is a reference, replace its reference address, these classes are:
(1) AtomicBoolean
The atom updates the Boolean type, internally using value storage 1 and 0 of the int type to represent true and false, and the underlying atomic operation on the int type.
(2) AtomicInteger
The atom updates the int type.
(3) AtomicLong
The atom updates the long type.
(4) AtomicReference
The atom updates the reference type, specifying the class to operate on through generics.
(5) AtomicMarkableReference
Atomic update of reference types, internal use of Pair to carry reference objects and tags that have been updated or not, avoiding ABA problems.
(6) AtomicStampedReference
Atomic update of reference types, internal use of Pair to carry reference objects and updated postmarks, avoiding ABA problems.
The operations of these classes are basically similar, and the bottom layer is implemented by calling compareAndSwapXxx () of Unsafe. The basic usage is as follows:
Private static void testAtomicReference () {AtomicInteger atomicInteger = new AtomicInteger (1); atomicInteger.incrementAndGet (); atomicInteger.getAndIncrement (); atomicInteger.compareAndSet (3666); System.out.println (atomicInteger.get ()); AtomicStampedReference atomicStampedReference = new AtomicStampedReference (1,1); atomicStampedReference.compareAndSet (1,2,1,3); atomicStampedReference.compareAndSet (2,666,3,5); System.out.println (atomicStampedReference.getReference ()) System.out.println (atomicStampedReference.getStamp ());} Atom updates elements in the array
The atom updates the elements in the array, and you can update the elements at the specified index position in the array. These classes are:
(1) AtomicIntegerArray
The atom updates the elements in the int array.
(2) AtomicLongArray
The atom updates the elements in the long array.
(3) AtomicReferenceArray
The atom updates the elements in the Object array.
The operations of these classes are basically similar, and you need to specify the index position in the array when updating the element. the basic usage is as follows:
Private static void testAtomicReferenceArray () {AtomicIntegerArray atomicIntegerArray = new AtomicIntegerArray (10); atomicIntegerArray.getAndIncrement (0); atomicIntegerArray.getAndAdd (1666); atomicIntegerArray.incrementAndGet (2); atomicIntegerArray.addAndGet (3,666); atomicIntegerArray.compareAndSet (4,0,666); System.out.println (atomicIntegerArray.get (0)); System.out.println (atomicIntegerArray.get (1)); System.out.println (atomicIntegerArray.get (2)) System.out.println (atomicIntegerArray.get (3)); System.out.println (atomicIntegerArray.get (4)); System.out.println (atomicIntegerArray.get (5));} Fields in the Atomic Update object
Atom updates the fields in the object, and you can update the fields with the specified field name in the object. These classes are:
(1) AtomicIntegerFieldUpdater
The int type field in the atomic update object.
(2) AtomicLongFieldUpdater
The long type field in the atomic update object.
(3) AtomicReferenceFieldUpdater
The reference type field in the atomic update object.
The operations of these classes are basically similar. You need to pass in the name of the field to be updated. The basic usage is as follows:
Private static void testAtomicReferenceField () {AtomicReferenceFieldUpdater updateName = AtomicReferenceFieldUpdater.newUpdater (User.class, String.class, "name"); AtomicIntegerFieldUpdater updateAge = AtomicIntegerFieldUpdater.newUpdater (User.class, "age"); User user = new User ("tong ge", 21); updateName.compareAndSet (user, "tong ge", "read source code"); updateAge.compareAndSet (user, 21,25); updateAge.incrementAndGet (user); System.out.println (user);} private static class User {volatile String name Volatile int age; public User (String name, int age) {this.name = name; this.age = age;} @ Override public String toString () {return "name:" + name + ", age:" + age;}} High performance Atomic Class
High-performance atomic classes are atomic classes added to java8. They use the idea of segmentation to hash different threads to different segments, and then add the values of these segments to get the final values. These classes are mainly:
(1) Striped64
The parents of the following four classes.
(2) LongAccumulator
For aggregators of long type, you need to pass in a binary operation of long type, which can be used to calculate various aggregation operations, including addition and multiplication.
(3) LongAdder
Long type accumulators, a special case of LongAccumulator, can only be used to calculate additions, starting with 0.
(4) DoubleAccumulator
For aggregators of double type, you need to pass in a binary operation of double type, which can be used to calculate various aggregation operations, including addition and multiplication.
(5) DoubleAdder
Double type accumulators, a special case of DoubleAccumulator, can only be used to calculate additions, starting with 0.
The operations of these classes are basically similar, in which the underlying layers of DoubleAccumulator and DoubleAdder are also implemented in long. The basic usage is as follows:
Private static void testNewAtomic () {LongAdder longAdder = new LongAdder (); longAdder.increment (); longAdder.add (666); System.out.println (longAdder.sum ()); LongAccumulator longAccumulator = new LongAccumulator ((left, right)-> left + right * 2666); longAccumulator.accumulate (1); longAccumulator.accumulate (3); longAccumulator.accumulate (- 4); System.out.println (longAccumulator.get ());} problem
With regard to the problems of atomic classes, the author has sorted out the following:
(1) what is Unsafe?
(3) Why is Unsafe unsafe?
(4) how to obtain an instance of Unsafe?
(5) CAS operation of Unsafe?
(6) blocking / waking up operation of Unsafe?
(7) Unsafe instantiates a class?
(8) six ways of instantiating classes?
(9) what is the atomic operation?
(10) what is the relationship between atomic operations and An in database ACID?
(11) how does AtomicInteger realize atomic operation?
(12) what are the main problems solved by AtomicInteger?
(13) what are the shortcomings of AtomicInteger?
14) what is ABA?
(15) the harm of ABA?
(16) the solution of ABA?
(17) how does AtomicStampedReference solve ABA?
(18) have you ever encountered ABA problems in practical work?
(19) what is the cache architecture of CPU?
(20) what is the cache line of CPU?
(21) what is the memory barrier?
(22) what is the cause of pseudo-sharing?
(23) how to avoid pseudo-sharing?
(24) eliminate the application of pseudo-sharing in java?
(25) how to implement LongAdder?
(26) how does LongAdder eliminate pseudo-sharing?
(27) performance comparison between LongAdder and AtomicLong?
(28) is the cells array in LongAdder infinitely expanded?
The above is all the contents of the article "what are the java atomic test questions?" Thank you for your reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.