Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7630523
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T06:07:37+00:00 2026-05-31T06:07:37+00:00

I am trying to find out about the performance difference between normal multithreading and

  • 0

I am trying to find out about the performance difference between normal multithreading and multithreading using executor (to maintain a thread pool).

The below are code examples for both.

Without Executor Code (with multithreading):

import java.lang.management.ManagementFactory;
import java.lang.management.MemoryPoolMXBean;
import java.lang.management.MemoryUsage;
import java.lang.management.ThreadMXBean;
import java.util.List;

public class Demo1 {
public static void main(String arg[]) {
    Demo1 demo = new Demo1();
    Thread t5  = new Thread(new Runnable() {
       public void run() {
              int count=0;
              // Thread.State;
              // System.out.println("ClientMsgReceiver started-----");
              Demo1.ChildDemo  obj = new Demo1.ChildDemo();
              while(true) {

                // System.out.println("Threadcount is"+Thread);
                // System.out.println("count is"+(count++));
                Thread t=new Thread(obj);
                t.start();
                ThreadMXBean tb = ManagementFactory.getThreadMXBean();
                List<MemoryPoolMXBean> pools = ManagementFactory.getMemoryPoolMXBeans();
                for (MemoryPoolMXBean pool : pools) {
                   MemoryUsage peak = pool.getPeakUsage();
                   System.out.format("Peak %s memory used: %,d%n",
                             pool.getName(), peak.getUsed());
                   System.out.format("Peak %s memory reserved: %,d%n",
                             pool.getName(), peak.getCommitted());
                } 

                System.out.println("Current Thread Count"+ tb.getThreadCount());
                System.out.println("Peak Thread Count"+ tb.getPeakThreadCount());
                System.out.println("Current_Thread_Cpu_Time " 
                         + tb.getCurrentThreadCpuTime());
                System.out.println("Daemon Thread Count" +tb.getDaemonThreadCount());
       }
       // ChatLogin = new ChatLogin();
     }
  });
  t5.start();
}

static class ChildDemo implements Runnable {
   public void run() {
        try {
        //  System.out.println("Thread Started with custom Run method");
            Thread.sleep(100000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        finally {
            System.out.println("A" +Thread.activeCount());
        }
    }
  }
}

With executor (multithreading):

import java.lang.management.ManagementFactory;
import java.lang.management.MemoryPoolMXBean;
import java.lang.management.MemoryUsage;
import java.lang.management.ThreadMXBean;
import java.util.List;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class Executor_Demo {
public static void main(String arg[]) {
   BlockingQueue<Runnable> queue = new ArrayBlockingQueue<Runnable>(10);
   ThreadPoolExecutor executor   = new ThreadPoolExecutor(
          10, 100, 10, TimeUnit.MICROSECONDS, queue);
   Executor_Demo demo = new Executor_Demo();

   executor.execute(new Runnable() {
       public void run() {
          int count=0;
          // System.out.println("ClientMsgReceiver started-----");
          Executor_Demo.Demo demo2 = new Executor_Demo.Demo();
          BlockingQueue<Runnable> queue1 = new ArrayBlockingQueue<Runnable>(1000);
          ThreadPoolExecutor executor1   = new ThreadPoolExecutor(
                  1000, 10000, 10, TimeUnit.MICROSECONDS, queue1);

          while(true) {
             // System.out.println("Threadcount is"+Thread);
             // System.out.println("count is"+(count++));
             Runnable command= new Demo();
             // executor1.execute(command);
             executor1.submit(command);         
             // Thread t=new Thread(demo2);
             // t.start();
             ThreadMXBean tb = ManagementFactory.getThreadMXBean();
             /* try {
                  executor1.awaitTermination(100, TimeUnit.MICROSECONDS);
                } catch (InterruptedException e) {
                   // TODO Auto-generated catch block
                   e.printStackTrace();
                } */
              List<MemoryPoolMXBean> pools = ManagementFactory.getMemoryPoolMXBeans();
              for (MemoryPoolMXBean pool : pools) {
                 MemoryUsage peak = pool.getPeakUsage();
                 System.out.format("Peak %s memory used: %,d%n",
                          pool.getName(), peak.getUsed());
                 System.out.format("Peak %s memory reserved: %,d%n",
                          pool.getName(), peak.getCommitted());
          }
              System.out.println("daemon threads"+tb.getDaemonThreadCount());
              System.out.println("All threads"+tb.getAllThreadIds());
              System.out.println("current thread CPU time "
                       + tb.getCurrentThreadCpuTime());
              System.out.println("current thread user time "
                       + tb.getCurrentThreadUserTime());
              System.out.println("Total started thread count " 
                       + tb.getTotalStartedThreadCount());
              System.out.println("Current Thread Count"+ tb.getThreadCount());
              System.out.println("Peak Thread Count"+ tb.getPeakThreadCount());
              System.out.println("Current_Thread_Cpu_Time " 
                       + tb.getCurrentThreadCpuTime());
              System.out.println("Daemon Thread Count"
                       + tb.getDaemonThreadCount());
              // executor1.shutdown();  
            }
            //ChatLogin = new ChatLogin();
          }
     });
     executor.shutdown();
}

static class Demo implements Runnable {
    public void run() {
      try {
        // System.out.println("Thread Started with custom Run method");
        Thread.sleep(100000);
      } catch (InterruptedException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
      }
      finally {
         System.out.println("A" +Thread.activeCount());
      }
   }
  }
}

Sample output Output

When I run both programs, it turns out the executor is more expensive than normal multithreading. why is this so?

And given this, what is the use of executor exactly? We use the executor to manage thread pools.

I would have expected the executor to give better results than normal multithreading.

Basically I’m doing this as I need to handle millions of clients using socket programming with multithreading.

Any suggestions will be helpful.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T06:07:38+00:00Added an answer on May 31, 2026 at 6:07 am

    To see how something scales, I would try to keep the cost of monitoring to a minimum and I would compare a small number to a large number.

    public class Executor_Demo {
        public static void main(String... arg) throws ExecutionException, InterruptedException {
            int nThreads = 5100;
            ExecutorService executor = Executors.newFixedThreadPool(nThreads, new DaemonThreadFactory());
    
            List<Future<Results>> futures = new ArrayList<Future<Results>>();
            for (int i = 0; i < nThreads; i++) {
                futures.add(executor.submit(new BackgroundCallable()));
            }
            Results result = new Results();
            for (Future<Results> future : futures) {
                result.merge(future.get());
            }
            executor.shutdown();
    
            result.print(System.out);
    
        }
    
        static class Results {
            private long cpuTime;
            private long userTime;
    
            Results() {
                final ThreadMXBean tb = ManagementFactory.getThreadMXBean();
                cpuTime = tb.getCurrentThreadCpuTime();
                userTime = tb.getCurrentThreadUserTime();
            }
    
    
            public void merge(Results results) {
                cpuTime += results.cpuTime;
                userTime += results.userTime;
            }
    
            public void print(PrintStream out) {
                ThreadMXBean tb = ManagementFactory.getThreadMXBean();
    
                List<MemoryPoolMXBean> pools = ManagementFactory.getMemoryPoolMXBeans();
                for (int i = 0, poolsSize = pools.size(); i < poolsSize; i++) {
                    MemoryPoolMXBean pool = pools.get(i);
                    MemoryUsage peak = pool.getPeakUsage();
                    out.format("Peak %s memory used:\t%,d%n", pool.getName(), peak.getUsed());
                    out.format("Peak %s memory reserved:\t%,d%n", pool.getName(), peak.getCommitted());
                }
    
                out.println("Total thread CPU time\t" + cpuTime);
                out.println("Total thread user time\t" + userTime);
                out.println("Total started thread count\t" + tb.getTotalStartedThreadCount());
                out.println("Current Thread Count\t" + tb.getThreadCount());
                out.println("Peak Thread Count\t" + tb.getPeakThreadCount());
                out.println("Daemon Thread Count\t" + tb.getDaemonThreadCount());
            }
        }
    
        static class DaemonThreadFactory implements ThreadFactory {
            @Override
            public Thread newThread(Runnable r) {
                Thread t = new Thread(r);
                t.setDaemon(true);
                return t;
            }
        }
    
        static class BackgroundCallable implements Callable<Results> {
            @Override
            public Results call() throws Exception {
                Thread.sleep(100);
                return new Results();
            }
        }
    }
    

    when tested with -XX:MaxNewSize=64m (this limits the size temporary memory spaces will increase)

    100 threads
    Peak Code Cache memory used:    386,880
    Peak Code Cache memory reserved:    2,555,904
    Peak PS Eden Space memory used: 41,280,984
    Peak PS Eden Space memory reserved: 50,331,648
    Peak PS Survivor Space memory used: 0
    Peak PS Survivor Space memory reserved: 8,388,608
    Peak PS Old Gen memory used:    0
    Peak PS Old Gen memory reserved:    192,675,840
    Peak PS Perm Gen memory used:   3,719,616
    Peak PS Perm Gen memory reserved:   21,757,952
    Total thread CPU time   20000000
    Total thread user time  20000000
    Total started thread count  105
    Current Thread Count    93
    Peak Thread Count   105
    Daemon Thread Count 92
    
    5100 threads
    Peak Code Cache memory used:    425,728
    Peak Code Cache memory reserved:    2,555,904
    Peak PS Eden Space memory used: 59,244,544
    Peak PS Eden Space memory reserved: 59,244,544
    Peak PS Survivor Space memory used: 2,949,152
    Peak PS Survivor Space memory reserved: 8,388,608
    Peak PS Old Gen memory used:    3,076,400
    Peak PS Old Gen memory reserved:    192,675,840
    Peak PS Perm Gen memory used:   3,787,096
    Peak PS Perm Gen memory reserved:   21,757,952
    Total thread CPU time   810000000
    Total thread user time  150000000
    Total started thread count  5105
    Current Thread Count    5105
    Peak Thread Count   5105
    Daemon Thread Count 5104
    

    The main increase is the increase in old gen used ~ 3 MB or about 6 KB per thread. and the CPU used by 956 ms or about 0.2 ms per thread.


    In your first example, you are creating one thread, in the second you are creating 1000.

    The output you are performing appears to be most of the work and you have much more output in the second case than the first.

    You need to be sure your testing and monitoring is far more light weight than want you are trying to monitor/measure.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to find out if anyone knows about an already existing jQuery plugin
I'm trying to find out whether I should be using business critical logic in
I'm trying to find out the most efficient (best performance) way to check date
I have been trying to find out more about Voldemort but it doesn't seem
I am trying to find out how long does it take each thread timeslice
I'm trying to find out about a Facebook user's interests based on pages they
I have spent about 2 Days trying to find out how to make 3d
I'm trying to find out about the Extras menu on windows phone. Extras.xml. Here
I am trying to find out if I am thinking about this correctly. My
Using the p4java library from Perforce (http://kb.perforce.com/article/1086/p4java-api), I'm trying to find out what files

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.