O'Reilly logo

Quality Code: Software Testing Principles, Practices, and Patterns by Stephen Vance

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Synchronize through Common Lock

Existing explicit synchronization gives us one of the easiest seams to exploit. The most common form occurs when the code under test has a critical section that uses another class that is already thread-safe. In such cases, you can use the synchronization in the other class to suspend within the critical section. Consider the code in Listing 13-7.

Listing 13-7: An example for discussion on common lock synchronization

public final class HostInfoService { // Singleton  private static HostInfoService instance;  private HostInfoService() {   ...  }  public static synchronized HostInfoService getInstance() {    if (instance == null ) {      instance = new HostInfoService();    }    return ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required