java high concurrent programming and thread safety

Keywords: Programming Maven Java Apache xml

Code has multiple threads running at the same time, and these threads may run the same code at the same time. If the result of each run is the same as that of a single thread, we think it is thread safe. Thread insecurity means that the thread does not provide access protection, and multiple threads may change the data successively, resulting in dirty data or errors in calculation.

The content of pom.xml file of new project project project-1 is as follows:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.vincent</groupId>
    <artifactId>concurrency</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <es.version>6.2.3</es.version>
    </properties>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>2.1.4.RELEASE</version>
                <scope>import</scope>
                <type>pom</type>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

</project>

Create a new annotation ThreadSafe.java. What we expect is that for a thread safe class, we use ThreadSafe for identification, because the code behind us often uses thread safety and insecurity. 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 or notation used to mark thread safety
 */
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.SOURCE)
public @interface ThreadSafe {
    String value() default "";
}

Next, define a thread unsafe annotation:

/**
 * Class or writing method used to mark thread unsafe
 */
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.SOURCE)
public @interface NotThreadSafe {
    String value() default "";
}

Define a recommended comment:

/**
 * Class or writing method used to mark thread recommendation
 */
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.SOURCE)
public @interface Recommend {
    String value() default "";
}

Define an deprecated comment:

/**
 * Class or writing method used to mark thread [not recommended]
 */
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.SOURCE)
public @interface NotRecommend {
    String value() default "";
}

Posted by Jtech inc. on Fri, 18 Oct 2019 11:59:57 -0700