Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <blockquote> <p>I find that my PC starts taking a huge load as soon as the program I execute the program. And it continues showing load after 75% of the threads have completed their job.</p> </blockquote> <p>If 75% of the 500 threads have completed their job then that leaves 100+ threads that continue to run. 100 threads, if using a good amount of CPU, can more than swamp the processors on a box which I assume does not have 100s of cores. So your application may continue to show 100% CPU utilization until the number of running threads drops below the number of cores.</p> <p>You should consider using a fixed sized thread pool instead of creating 500 concurrent threads. You then submit 500 <em>tasks</em> to the thread pool. This allows you to choose an appropriate number of threads to be running concurrently. The appropriate number of threads is highly dependent on the tasks being submitted. More CPU bound tasks should use fewer threads while IO bound tasks can use more. Doing some test runs with your application while adjusting the number of threads is the best way to optimize the value. I tend to start with 2 times the number of cores and then optimize from there.</p> <pre><code>// create a thread pool with 10 workers ExecutorService threadPool = Executors.newFixedThreadPool(10); // define your jobs somehow for (MyRunnable job : jobsToDo) { threadPool.submit(job); } // once we have submitted all jobs to the thread pool, it should be shutdown threadPool.shutdown(); </code></pre> <p>For more details, check out the <a href="http://docs.oracle.com/javase/tutorial/essential/concurrency/exinter.html" rel="nofollow noreferrer"><code>ExecutorService</code> tutorial</a>.</p> <blockquote> <p>I want to know whether the threads whose work has finished were killed or not. And how does java deal with threads which have finished their job. Any help...</p> </blockquote> <p>The threads have most likely finished. After a thread leaves the <code>run()</code> method (either because it returns or throws an exception) it will no longer consume CPU and its underlying native thread can be reaped or reused. If there are no references to the <code>Thread</code> object, its memory will eventually be reaped by the garbage collector.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload