JavaScript is not currently enabled, but is required for full CodeSonar manual search and browse functionality.

If you are viewing this file in your hub's Web GUI, enable JavaScript in your browser: you will also need it for GUI functionality.

If you opened this file directly from disk, your browser may be directly suppressing JavaScript functionality: certain browsers perform this suppression on local files (but not files delivered by web servers) for security reasons.

CodeSonar® 9.2p0 CONFIDENTIAL CodeSecure Inc
Java


JAVA.CONCURRENCY.LOCK.SCTB : Synchronous Call to Thread Body (Java)

Summary

The body of a thread is called synchronously.

Concurrency is an important but complex aspect of modern software. As a consequence, it is often used in an incorrect way, also because the subtleties of the Java memory model are not always understood. This checker identifies a large class of common programming errors due to incorrect uses of concurrency primitives, such as incorrect implementations of the singleton pattern and incorrect uses of the volatile field modifier, whose goal is to publish a field update to all executing cores. The latter, however, has a cost in terms of execution time.

Properties

Class Name Synchronous Call to Thread Body (Java)
Significance reliability
Mnemonic JAVA.CONCURRENCY.LOCK.SCTB
Categories
CWE CWE:572 Call to Thread run() instead of start()
CERT-Java CERT-Java:THI00-J Do not invoke Thread.run()
Availability Available for Java and Kotlin.
Enabling Checks for this warning class are enabled by default. To disable them, add the following WARNING_FILTER rule to the project configuration file.
WARNING_FILTER += discard class="Synchronous Call to Thread Body (Java)"

Example

public class HelloThreadExample { 
    
  public static void main(String[] args) {
      HelloThread helloTread = new HelloThread();
      helloTread.run(); // "Synchronous Call to Thread Body (Java)" warning issued here
      //...
      System.out.println(helloTread.getHello() + "world!");
  }
    
   public static class HelloThread extends Thread {
     private String hello = "";

     @Override
     public void run() {
         for (int i = 0; i &tl; 100; i++)
             hello += "hello\n";
     };
            
     public String getHello() {
         return hello;
     }
  }
} 

The analysis triggers a Synchronous Call to Thread Body (Java) warning because direct use of the run() method implies that the instructions are executed synchronously. For high performance, threads should run asynchronously.

To solve this issue, the programmer should rewrite the program to use the start() method, which calls run() asynchronously.

public class HelloThreadExample { 
    
  public static void main(String[] args) {
      HelloThread helloTread = new HelloThread();
      helloTread.start();
      //...

      try {
        helloTread.join();
      } catch (InterruptedException e) {
        e.printStackTrace();
      }
        
      System.out.println(helloTread.getHello() + "world!");
  }
    
  public static class HelloThread extends Thread {

    private String hello = "";
            
    @Override
    public void run() {
        for (int i = 0; i &tl; 100; i++)
            hello += "hello\n";
    };
            
    public String getHello() {
        return hello;
    }
  }
} 

Resolution

Check if the warnings correspond to actual possible errors for a concurrent execution of the program.

Relevant Configuration File Parameters

The following configuration file parameters affect checks for this warning class.

 

To report problems with this documentation, please visit https://support.codesecure.com/.