Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What happens to a thread as soon as it has completed its assigned task in java?

I have been working on a project in which my program creates around 500 threads during the execution. 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. 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...

like image 234
Krishna Avatar asked Nov 13 '13 17:11

Krishna


People also ask

What happens to a given thread after it finishes its task?

Each thread uses the default stack size, runs at the default priority, and is in the multithreaded apartment. Once a thread in the thread pool completes its task, it's returned to a queue of waiting threads. From this moment it can be reused.

What happens to a thread when it finishes Java?

Finishing Threads So when does a thread finish? It happens in one of two cases: all instructions in the Runnable are executed. an uncaught exception is thrown from the run method.

How do you know when a thread is finished?

In JavaSW, a thread is considered to be alive when its start() method has been called. After the run() method finishes, the thread is considered to not be alive anymore. We can determine if a thread is currently alive or not by calling a Thread instance's isAlive() method. The getState() method can also be useful.


2 Answers

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.

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.

You should consider using a fixed sized thread pool instead of creating 500 concurrent threads. You then submit 500 tasks 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.

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

For more details, check out the ExecutorService tutorial.

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...

The threads have most likely finished. After a thread leaves the run() 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 Thread object, its memory will eventually be reaped by the garbage collector.

like image 52
Gray Avatar answered Nov 14 '22 23:11

Gray


The JVM will garbage collect the Thread object, as long as there are no references to it, and as long as its run method returns. Thread is dead itself after its run method returns. It might still be in the heap, but it will not have its own stack anymore, and not do anything.

The only possible way that your threads have not been killed is that they still do something or you forgot to clean up references to your thread objects - but this is memory related.

If you allocated your threads through the thread pool, they are returned to the pool after the execution of the task. I this case, they might not be released after the completion of the task.

like image 32
Artem Moskalev Avatar answered Nov 15 '22 00:11

Artem Moskalev