Standard Analyses

SemmleCode Professional comes with a wide range of pre-packaged analyses, from architectural properties and metrics, to statement-level checks for likely bugs and violations of best practice.


Sleep with Lock Held

This query finds code that calls Thread.sleep with a lock held. Other threads may be waiting to acquire this lock, leading to poor performance or even a deadlock.

How to Interpret the Query Results

The query flags such code and also displays the list of detected occurrences in the result view.

How to Address the Query Results

Instead, wait should be called on the lock.

Source Code
import default

from MethodAccess ma, Method sleep
where ma.getMethod() = sleep and
      sleep.hasName("sleep") and
      sleep.getDeclaringType().hasName("Thread") and
      (   ma.getEnclosingStmt().getParent*() instanceof SynchronizedStmt
       or ma.getEnclosingCallable().hasModifier("synchronized"))
select ma, "sleep() with lock held"
References