Skip to content

Thread Suspend Resume

"Thread.Resume" and "Thread.Suspend" should not be used.

What does this mean ?

Thread.Suspend and Thread.Resume might provide unexpected consequences, therefore both are deprecated.

What can happen ?

If Thread.Suspend is not used with caution, a thread can be suspended while still holding a lock, resulting in a deadlock.

Recommendation

Other, more secure synchronization techniques, such as Monitor, Mutex, and Semaphore, should be utilized.

Sample Code

Vulnerable :

static void Main(string[] args)
{
  // ...
  Thread.CurrentThread.Suspend(); // Noncompliant
  Thread.CurrentThread.Resume(); // Noncompliant
}

Non Vulnerable :

ThreadFactory threadFactory = Executors.defaultThreadFactory();
ThreadPoolExecutor executorPool = new ThreadPoolExecutor(3, 10, 5, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(2), threadFactory);

for (int i = 0; i < 10; i++) {
  executorPool.execute(new JobThread("Job: " + i));
}

System.out.println(executorPool.getActiveCount()); // Compliant
executorPool.shutdown();

References