Thread safety is an unavoidable obstacle in Java performance testing. To test it, you must have some understanding of it. I’ve written about thread safety and thread synchronization in performance testing before:

  • Double-checked locking for server performance optimization
  • Java Concurrency BUG Basics
  • Java concurrency BUG enhancement
  • How do I keep threads safe in anonymous Thread subclasses
  • Two common concurrency errors on the Java server
  • Thread-safe classes are used in performance testing

But in terms of performance, lock synchronization will bring more performance consumption, some of the gain is not worth the loss. In some complicated scenario lock synchronization is not the only solution to the problem of the thread safe there are two kinds, one kind is based on CAS alternatives, I have previous articles thread-safe classes in performance testing is used in the application of the scheme, including performance testing framework is used multiple times in the third edition of this scheme, interested students can take a look back at the two articles.

Here is another way to avoid synchronization: One way to avoid synchronization is to use different objects in each thread so that there is no competition for accessing objects. To ensure thread-safety, many Java objects are synchronized, but they do not necessarily need to be shared. On the other hand, many Java objects are expensive to create or consume a lot of memory. The java.lang.ThreadLocal method is a good solution to this problem. Each time a thread accesses the object, it gets a new object, eliminating the need for threads to compete for the same object and thus eliminating the need for synchronization, which can greatly improve performance.

Here’s a Demo:

package com.fun; import com.fun.frame.SourceCode; public class AR extends SourceCode { static ThreadLocal<String> local = new ThreadLocal<String>() { public String initialValue() { String s = new String(getNanoMark() + EMPTY); output(s.hashCode()); return s; }}; public static void main(String[] args) throws InterruptedException { AR ar = new AR(); ar.ss(); ar.ss(); Thread thread = new Thread(() -> ar.ss()); Thread thread2 = new Thread(() -> ar.ss()); thread.start(); thread2.start(); thread.join(); thread2.join(); } public void ss() { local.get(); }}Copy the code

The console prints the following:

INFO-> Current user: fV, IP: 192.168.0.104, working directory: / Users/fv/Documents/workspace/fun/system coding format: utf-8, Mac OS X system version: 10.15.3 INFO - > 851397249 INFO - > 1851572413 INFO - > 1875674350 Process finished with exit code 0Copy the code

  • Solemnly declare: the article was first published in the public number “FunTester”, prohibit third parties (except Tencent cloud) reprint, published.

Technical articles selected

  • Linux performance monitoring software Netdata Chinese version
  • Performance Testing Framework third edition
  • How to perform performance tests on a Linux command line interface
  • Graphic HTTP brain map
  • Automatically turn the Swagger document into test code
  • Selenium Python Use Tips (1)
  • Selenium Python Use Tips (2)
  • Selenium Python Use tips (3)

Selected non-technical articles

  • Programming thinking for everyone
  • 7 Skills to become an automated test
  • Summary of Automatic Test failures on the Web
  • Testers often use excuses
  • API Automation Test Guide
  • Future QA test engineer
  • JSON based
  • 2020 Tester self-improvement