Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

What is concurrent programming and thread safety in java

Shulou Source: shulou.com Published: 2022-06-02 02:39:13 09月15日 Update

Today, I will talk to you about what is concurrent programming and thread safety in java. Many people may not know much about it. In order to make you understand better, the editor has summarized the following content for you. I hope you can get something according to this article.

The code has multiple threads running at the same time, and these threads may run the same piece of code at the same time. If the result of each run is the same as that of a single thread, we consider it thread-safe. Thread unsafety means that the thread does not provide access protection, and multiple threads may change the data one after another, resulting in dirty data or errors in calculation.

The contents of the new project project-1,pom.xml file are as follows:

4.0.0 com.vincent concurrency 1.0-SNAPSHOT UTF-8 1.8 1.8 6.2.3 org.springframework.boot spring-boot-dependencies 2.1.4.RELEASE import pom Org.springframework.boot spring-boot-starter-web

Create a new annotation ThreadSafe.java, and what we expect is that for a thread-safe class, we use ThreadSafe to identify it, because the code behind us often uses thread-safe and unsafe. The contents are as follows:

Package com.vincent.annotation;import java.lang.annotation.ElementType;import java.lang.annotation.Retention;import java.lang.annotation.RetentionPolicy;import java.lang.annotation.Target;/** * A class used to mark thread safety or written as * / @ Target (ElementType.TYPE) @ Retention (RetentionPolicy.SOURCE) public @ interface ThreadSafe {String value () default ";}

Next, define a thread-unsafe comment:

/ * * the class used to mark threads [unsafe] or write * / @ Target (ElementType.TYPE) @ Retention (RetentionPolicy.SOURCE) public @ interface NotThreadSafe {String value () default ";}

Define a recommended comment:

/ * * the class used to mark threads [recommended] or write * / @ Target (ElementType.TYPE) @ Retention (RetentionPolicy.SOURCE) public @ interface Recommend {String value () default ";}

Define a deprecated comment:

/ * classes used to mark threads [not recommended] or written * / @ Target (ElementType.TYPE) @ Retention (RetentionPolicy.SOURCE) public @ interface NotRecommend {String value () default ";} after reading the above, do you have any further understanding of concurrent programming and thread safety in java? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.

Tags: Threads security content writing tags annotations recommendations runs code data programming at the same time multiple results and then files more identification Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Xiaomi Redmi vpn OPPO Reno Shulou Information