Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use lock-free technology in Java

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces the knowledge of "how to use lock-free technology in Java". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

1. Atomic tool class

In JDK 1.8, the classes under the java.util.concurrent.atomic package are all atomic classes, and atomic classes are all implemented based on sun.misc.Unsafe.

In order to solve the concurrency problem, CPU provides CAS instruction, full name Compare And Swap, that is, compare and interact.

The CAS instruction requires three parameters, variables, comparison values, and new values. Update the variable to the new value only when the current value of the variable is equal to the comparison value

CAS is a CPU instruction that is guaranteed atomicity at the CPU hardware level

The atoms in the java.util.concurrent.atomic package are divided into: atomic basic data type, atomic object reference type, atomic array, atomic object attribute updater and atomic accumulator.

Atomic basic data types: AtomicBoolean, AtomicInteger, AtomicLong

Atomic object reference types: AtomicReference, AtomicStampedReference, AtomicMarkableReference

Atomic arrays: AtomicIntegerArray, AtomicLongArray, AtomicReferenceArray

Atomic object property updates: AtomicIntegerFieldUpdater, AtomicLongFieldUpdater, AtomicReferenceFieldUpdater

Atomic accumulator: DoubleAccumulator, DoubleAdder, LongAccumulator, LongAdder

Modify the class that we tested for atomicity problems earlier, using a simple example of AtomicInteger

Package constxiong.concurrency.a026; import java.util.concurrent.atomic.AtomicInteger; / * Test atomic class AtomicInteger * * @ author ConstXiong * / public class TestAtomicInteger {/ / count variable static volatile AtomicInteger count = new AtomicInteger (0); public static void main (String [] args) throws InterruptedException {/ / Thread 1 adds 10000 Thread T1 = new Thread (())-> {for (int j = 0; j {for (int j = 0; j)

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report