Skip to content

Commit

Permalink
java current tool
Browse files Browse the repository at this point in the history
  • Loading branch information
xuMingHai1 committed May 5, 2023
1 parent 6326170 commit 4e63676
Show file tree
Hide file tree
Showing 38 changed files with 2,304 additions and 1,811 deletions.
128 changes: 64 additions & 64 deletions concurrency-test/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,77 +29,77 @@ ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
THE POSSIBILITY OF SUCH DAMAGE.
-->

<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>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
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>xyz.xuminghai</groupId>
<artifactId>concurrency-test</artifactId>
<packaging>jar</packaging>
<version>1.0</version>
<groupId>xyz.xuminghai</groupId>
<artifactId>concurrency-test</artifactId>
<packaging>jar</packaging>
<version>1.0</version>

<dependencies>
<dependency>
<groupId>org.openjdk.jcstress</groupId>
<artifactId>jcstress-core</artifactId>
<version>${jcstress.version}</version>
</dependency>
</dependencies>
<dependencies>
<dependency>
<groupId>org.openjdk.jcstress</groupId>
<artifactId>jcstress-core</artifactId>
<version>${jcstress.version}</version>
</dependency>
</dependencies>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>

<!--
jcstress version to use with this project.
-->
<jcstress.version>0.16</jcstress.version>
<!--
jcstress version to use with this project.
-->
<jcstress.version>0.16</jcstress.version>

<!--
Name of the test Uber-JAR to generate.
-->
<uberjar.name>jcstress</uberjar.name>
<!--
Name of the test Uber-JAR to generate.
-->
<uberjar.name>jcstress</uberjar.name>

</properties>
</properties>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.10.0</version>
</plugin>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.10.0</version>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.4</version>
<executions>
<execution>
<id>main</id>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<finalName>${uberjar.name}</finalName>
<transformers>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>org.openjdk.jcstress.Main</mainClass>
</transformer>
<transformer
implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
<resource>META-INF/TestList</resource>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.4</version>
<executions>
<execution>
<id>main</id>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<finalName>${uberjar.name}</finalName>
<transformers>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>org.openjdk.jcstress.Main</mainClass>
</transformer>
<transformer
implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
<resource>META-INF/TestList</resource>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
/**
* 2022/5/8 15:01 星期日<br/>
* 和{@link DoubleCheckLockTest}比较
*
* @author xuMingHai
*/
@JCStressTest
Expand All @@ -16,52 +17,52 @@
@State
public class CompareDoubleCheckLockTest {

private volatile static Instance instance;
private volatile static Instance instance;

public static Instance getInstance() {
Instance result = instance;
if (result == null) {
synchronized (CompareDoubleCheckLockTest.class) {
result = instance;
if (result == null) {
instance = new Instance();
}
}
}
return instance;
}
public static Instance getInstance() {
Instance result = instance;
if (result == null) {
synchronized (CompareDoubleCheckLockTest.class) {
result = instance;
if (result == null) {
instance = new Instance();
}
}
}
return instance;
}

private static class Instance implements Serializable {
private static final long serialVersionUID = -3475949357133835998L;

@Override
public String toString() {
return "Instance";
}
}
@Actor
public void actor1(L_Result r) {
r.r1 = getInstance();
}

/*
使用4个线程测试
*/

@Actor
public void actor1(L_Result r) {
r.r1 = getInstance();
}
@Actor
public void actor2(L_Result r) {
r.r1 = getInstance();
}

@Actor
public void actor3(L_Result r) {
r.r1 = getInstance();
}

@Actor
public void actor2(L_Result r) {
r.r1 = getInstance();
}
@Actor
public void actor4(L_Result r) {
r.r1 = getInstance();
}

@Actor
public void actor3(L_Result r) {
r.r1 = getInstance();
}
private static class Instance implements Serializable {
private static final long serialVersionUID = -3475949357133835998L;

@Actor
public void actor4(L_Result r) {
r.r1 = getInstance();
}
@Override
public String toString() {
return "Instance";
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
* 双重检查锁单例<br/>
* <p>在维基百科中的<a href="https://zh.m.wikipedia.org/zh-cn/%E5%8F%8C%E9%87%8D%E6%A3%80%E6%9F%A5%E9%94%81%E5%AE%9A%E6%A8%A1%E5%BC%8F">双重检查锁</a>,使用了局部变量代替直接比较 volatile 修饰的变量</p>
* 以下来自 Joshua Bloch "Effective Java, Second Edition", p. 283
* <pre>
* <pre>
* //Double-check idiom for lazy initialization of instance fields
* private volatile FieldType field;
* Fie1dType getFie1d() {
Expand All @@ -25,9 +25,10 @@
* return result;
* }
* </pre>
* 这段代码可能看起来似乎有些费解。尤其对于需要用到局部变量result可能有点不解。
* 这个变量的作用是确保field只在已经被初始化的情况下读取一次。虽然这不是严格需要,但是可以提升性能,并且因为给低级的并发编程应用了一些标准,因此更加优雅。
* 在我的机器上,上述的方法比没用局部变量的方法快了大约25%。
* 这段代码可能看起来似乎有些费解。尤其对于需要用到局部变量result可能有点不解。
* 这个变量的作用是确保field只在已经被初始化的情况下读取一次。虽然这不是严格需要,但是可以提升性能,并且因为给低级的并发编程应用了一些标准,因此更加优雅。
* 在我的机器上,上述的方法比没用局部变量的方法快了大约25%。
*
* @author xuMingHai
*/
@JCStressTest
Expand All @@ -36,53 +37,53 @@
@State
public class DoubleCheckLockTest {

private volatile static Instance instance;
private volatile static Instance instance;

public static Instance getInstance() {
Instance result = instance;
if (result == null) {
synchronized (DoubleCheckLockTest.class) {
result = instance;
if (result == null) {
instance = new Instance();
}
}
}
return instance;
}
public static Instance getInstance() {
Instance result = instance;
if (result == null) {
synchronized (DoubleCheckLockTest.class) {
result = instance;
if (result == null) {
instance = new Instance();
}
}
}
return instance;
}

private static class Instance implements Serializable {

private static final long serialVersionUID = -3949564844175144101L;

@Override
public String toString() {
return "Instance";
}
}
@Actor
public void actor1(L_Result r) {
r.r1 = getInstance();
}

/*
使用4个线程测试
*/

@Actor
public void actor1(L_Result r) {
r.r1 = getInstance();
}
@Actor
public void actor2(L_Result r) {
r.r1 = getInstance();
}

@Actor
public void actor3(L_Result r) {
r.r1 = getInstance();
}

@Actor
public void actor4(L_Result r) {
r.r1 = getInstance();
}

@Actor
public void actor2(L_Result r) {
r.r1 = getInstance();
}
private static class Instance implements Serializable {

@Actor
public void actor3(L_Result r) {
r.r1 = getInstance();
}
private static final long serialVersionUID = -3949564844175144101L;

@Actor
public void actor4(L_Result r) {
r.r1 = getInstance();
}
@Override
public String toString() {
return "Instance";
}
}

}
Loading

0 comments on commit 4e63676

Please sign in to comment.