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

  • SEARCH
  • Home
  • 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 6182773
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T01:15:22+00:00 2026-05-24T01:15:22+00:00

I am doing a hello world on threads, i have created a simple thread

  • 0

I am doing a hello world on threads, i have created a simple thread with using the run() call (which it is just a normal method call) and a duplicate thread with using the start() call which spawns another thread to process, however, the time taken for the start() calls is more than that taken for the run() calls, which are not threaded calls, why is it so?

Start call time taken: 00:00:08:300

    long time = System.currentTimeMillis();

    for (int i = 0; i < 100000; i++) {
        Thread thread = new Thread(new Car());
        thread.setName(Integer.toString(i));
        thread.start();
    }

    long completedIn = System.currentTimeMillis() - time;

    System.out.println(DurationFormatUtils.formatDuration(completedIn,
            "HH:mm:ss:SS"));

Run call time taken: 00:00:01:366

    long time = System.currentTimeMillis();

    for (int i = 0; i < 100000; i++) {
        Thread thread = new Thread(new Car());
        thread.setName(Integer.toString(i));
        thread.run();
    }

    long completedIn = System.currentTimeMillis() - time;

    System.out.println(DurationFormatUtils.formatDuration(completedIn,
            "HH:mm:ss:SS"));
  • 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-24T01:15:22+00:00Added an answer on May 24, 2026 at 1:15 am

    From comment to accepted response: "Would you have any advise on this?"

    Yes, do not use threads directly. Since java 5, we have the java.util.concurrent framework that allows to simplify thread and task management.

    Creating a thread is costly.

    That is even before the thread will run the task you want, it will have to be created, a really long process. For that very reason, we have the concept of thread pool.

    Instead of creating new threads each time you want to perform a concurrent task, you first create the threads and then you send them tasks when you need too.

    A thread pool as a second advantage. When the task is finished, the thread is not destroyed, but kept active to run the next task, so the thread creation cost occurs only one time, at initialization time.

    So how do you use theses concepts?

    First create an executor with a thread pool. To stay simple we create a thread pool with 100 threads (as you want to simulate load of 100 concurrent calls):

    ExecutorService pool = Executors.newFixedThreadPool(100);
    

    Then you’ill submit your tasks:

    long time = System.currentTimeMillis();
    for (int i=0; i<100000; i++) {
      pool.execute(new Car());
    }
    

    And important, you’ll wait for all tasks to finish before stopping the program !!!

    pool.shutdown(); //Do no longer accept new tasks.
    pool.awaitTermination(1, TimeUnit.HOURS); //Wait for up to one hour for all tasks to finish.
    long completedIn = System.currentTimeMillis() - time;
    System.out.println(DurationFormatUtils.formatDuration(completedIn,
                "HH:mm:ss:SS"));
    

    In your thread code you didn’t wait for the threads to finish their job, in fact you measured the thread creation time, not the tasks running time.

    What the code does?

    The executor.execute method execute the provided task inside a thread. Here it take one of the 100 thread, and let it execute the task.

    What happens if there is more than 100 tasks?

    With 100 threads, you can’t run more than 100 concurrent tasks. Other tasks will be queued untill one task is finished so one thread is available to execute it. This is a good way to ensure you don’t create too much thread and OutOfMemory or other nasty things don’t happens.

    How many thread should you use?

    This depend of the kind of tasks you want to execute.

    If it is like a web server, you are mostly IO bound waiting for the database to get data and then for the network to send the response, your thread will be mostly waiting. So even one CPU will benefit from dozen, even hundred of threads. And even if more thread start to slow down the whole application, it allow to process the user request instead of making it wait, or just deny to respond.

    If your task are CPU bound you’ll want something like one task per CPU core so to maximize usage of your hardware but limit the overhead of context switches. With a 4 core hyper-threading CPU, you can go up to 8 concurrent threads.

    This response is just a short introduction… You’ll learn more by looking at java.util.concurrent package and reading some tutorials.

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

Sidebar

Related Questions

I have simple Hello World C++ program (main.cpp): #include <iostream> using namespace std; int
I am using System.Windows.Forms.RichTextBox and doing something like RichTextBox1.Text = Hello World; But after
I'm just starting to learn Android doing the Hello World app, and some of
I'm just starting out with Cocoa development in Xcode, doing the hello world example.
I have no experience with threads, unfortunately. I have been doing a couple of
I've implemented a simple Observer pattern in a hello world style app. I've got
Hello: I have an app where in a thread hierarchy (persisted entity) is modelled
I am trying to run a hello world program written in javascript in a
Direct Question: How do I create a simple hello world CUDA project within visual
When doing a simple performance measurement, I was astonished to see that calling String.IndexOf(char)

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.