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 7575131
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T16:36:36+00:00 2026-05-30T16:36:36+00:00

I learned about multiprocessing from Python and I’m having a bit of trouble understanding

  • 0

I learned about multiprocessing from Python and I’m having a bit of trouble understanding Java’s approach. In Python, I can say I want a pool of 4 processes and then send a bunch of work to my program and it’ll work on 4 items at a time. I realized, with Java, I need to use threads to achieve this same task and it seems to be working really really well so far.

But.. unlike in Python, my cpu(s) aren’t getting 100% utilization (they are about 70-80%) and I suspect it’s the way I’m creating threads (code is the same between Python/Java and processes are independent). In Java, I’m not sure how to create one thread so I create a thread for every item in a list I want to process, like this:

    for (int i = 0; i < 500; i++) {
        Runnable task = new MyRunnable(10000000L + i);
        Thread worker = new Thread(task);
        // We can set the name of the thread
        worker.setName(String.valueOf(i));
        // Start the thread, never call method run() direct
        worker.start();
        // Remember the thread for later usage
        threads.add(worker);
    }

I took it from here. My question is this the correct way to launch threads or is there a way to have Java itself manage the number of threads so it’s optimal? I want my code to run as fast as possible and I’m trying to understand how to tell and resolve any issues that maybe arising from too many threads being created.

This is not a major issue, just curious to how it works under the Java hood.

  • 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-30T16:36:38+00:00Added an answer on May 30, 2026 at 4:36 pm

    Take a look at the Java Executor API. See this article, for example.

    Although creating Threads is much ‘cheaper’ than it used to be, creating large numbers of threads (one per runnable as in your example) isn’t the way to go – there’s still an overhead in creating them, and you’ll end up with too much context switching.

    The Executor API allows you to create various types of thread pool for executing Runnable tasks, so you can reuse threads, flexibly manage the number that are created, and avoid the overhead of thread-per-runnable.

    The Java threading model and the Python threading model (not multiprocessing) are really quite similar, incidentally. There isn’t a Global Interpreter Lock as in Python, so there’s usually less need to fork off multiple processes.

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

Sidebar

Related Questions

I recently learned about sockets in Java and sending information back and forth from
I have recently learned about continuous integration. From my understanding, its a way to
I just learned about how the Java Collections Framework implements data structures in linked
I learned about how to browse my local App Engine Datastore from this handy
I just recently learned about Scope Guard C++ idiom. Unfortunately I can't find any
I just learned about .delegate today, and I know I can be used for
I just learned about Java's Scanner class and now I'm wondering how it compares/competes
After I learned about reading unicode files in Python 3.0 web script, now it's
I just learned about java.sql package . It uses Class.forName() to dynamically load the
I just learned about Google App Engine, and I must say it is fantastic.

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.