In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
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.
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.