Uploaded image for project: 'Rules Repository'
  1. Rules Repository
  2. RSPEC-3014

"ThreadGroup" should not be used

    XMLWordPrintable

    Details

    • Message:
      Remove this use of "ThreadGroup". Prefer the use of "ThreadPoolExecutor".
    • Default Severity:
      Blocker
    • Impact:
      High
    • Likelihood:
      High
    • Default Quality Profiles:
      Sonar way
    • Covered Languages:
      Java
    • Remediation Function:
      Constant/Issue
    • Constant Cost:
      45min
    • CERT:
      THI01-J.
    • PMD:
      AvoidThreadGroup

      Description

      There is little valid reason to use the methods of the ThreadGroup class. Some are deprecated (allowThreadSuspension(), resume(), stop(), and suspend()), some are obsolete, others aren't thread-safe, and still others are insecure (activeCount(), enumerate()) . For these reasons, any use of ThreadGroup is suspicious and should be avoided.

      Compliant Solution

      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();
      

      See

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              Unassigned Unassigned
              Reporter:
              ann.campbell.2 Ann Campbell
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

                Dates

                Created:
                Updated: