Last time, i++; It is inherently a thread-unsafe operation because the operation is not atomic and there are two processes of evaluation and assignment, but how on earth is it unsafe? This installment uses a “VMLens” project to demonstrate how thread insecurity can occur. At the end of the article is an introduction to VMLens.

Test code:

public class TestCounter {
	private volatile int i = 0;
	@Interleave
	public void increment() {
	 i++;	
	}
	@Test
	public void testUpdate() throws InterruptedException { Thread first = new Thread( () -> {increment(); }); Thread second = new Thread( () -> {increment(); }); first.start(); second.start(); first.join(); second.join(); } @After public voidcheckResult() { assertEquals( 2 , i ); }}Copy the code

It is important to configure the POM.xml file:

<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. Vmlens < / groupId > < artifactId > examples < / artifactId > < version > 0.0.1 - the SNAPSHOT < / version > < packaging > jar < / packaging > < name > examples < / name > < url > http://maven.apache.org < / url > <pluginRepositories> <pluginRepository> <id>vmlens</id> <url>http://vmlens.com/download</url> </pluginRepository> </pluginRepositories> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependency> <groupId>com.vmlens</groupId> <artifactId>annotation</artifactId> <version>1.0.2</version> <scope>test</scope>
		</dependency>
  
  
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.8.2</version>
      <scope>test</scope> </dependency> </dependencies> <build> <pluginManagement> <plugins> <plugin> . < groupId > org, apache maven plugins < / groupId > < artifactId > maven - compiler - plugin < / artifactId > < version > 3.5.1 track of < / version > <configuration> <source> 1.8 < /source> <target>1.8</target> </configuration> </plugin> </plugins> </plugins> . < the groupId > org, apache maven plugins < / groupId > < artifactId > maven - surefire - plugin < / artifactId > < version > 3.0.0 - M3 < / version > <configuration> <includes> <include>none</include> </includes> </configuration> </plugin> <plugin> < the groupId > com. Vmlens < / groupId > < artifactId > interleave < / artifactId > < version > 1.0.4 < / version > <! -- start regressiontest -->
				<configuration>
					<trimStackTrace>false</trimStackTrace>
									<includes>
		<include>com.vmlens.examples.doNotCombine.TestCounter</include>
					</includes>
				</configuration>
           </plugin>
      </plugins>
  </build>
  </project>

Copy the code

Here’s the VMLens report:

From the diagram, we can see that “i++;” is executed on both threads simultaneously. When, both threads read the value of I “0” successively, then completed the calculation of “I +1” successively, and finally assigned the value of “1” to I successively, resulting in the failure of the test case execution.

Introducing this plug-in: “VMLens” is a tool for testing Java multithreading.

  1. All parts of an application that needs to test multiple threads accessing the same memory location or monitor. Vmlens shows multiple threads accessing the same memory location or all locations of the monitor.
  2. The VMLens inserts wait, notifies the instruction during the test and re-runs the test until all the test threads are interlaced. This, together with automatic detection of data contention and deadlocks, leads to system and repeatable tests.
  3. You can reduce the number of shared states by seeing how multiple threads access the same state.
  4. Less shared state means fewer synchronization monitors are required.

Here’s Thomas:

Hello!

Do you love to write bug-free software? Me too!

It always bothers me when I can not test something. That's why I created vmlens, a tool to test multithreaded java. Now 4 years and countless tests later vmlens enables you to test multi-threaded java systematic and reproducible. And like vmlens now let my completely test vmlens, vmlens let you test the multithreaded part of your application Enjoy writing concurrent software secured by tests. Cheers, ThomasCopy the code

Photo of the author:

~ FunTester together

Selected articles from previous issues

  1. Java one line of code to print a heart
  2. Linux performance monitoring software Netdata Chinese version
  3. Interface Test Code Coverage (JACOCO) solution sharing
  4. Performance testing framework
  5. How to perform performance tests on a Linux command line interface
  6. Graphic HTTP brain map
  7. Programming thinking for everyone
  8. JVM command brain map for testing

Map of official Account