File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Threads and Synchronization and the fly likes ThreadPoolExecutor getting stuck Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "ThreadPoolExecutor getting stuck" Watch "ThreadPoolExecutor getting stuck" New topic
Author

ThreadPoolExecutor getting stuck

Ravi K Rao
Greenhorn

Joined: Jun 16, 2010
Posts: 2
Hi,

I'm working on some application and using ThreadPoolExecutor for handling various tasks. ThreadPoolExecutor is getting stuck after some duration. To simulate this in a simpler environment, I've written a simple code where I'm able to simulate the issue.



Try executing this code a few times. It normally print outs the number on console and when all threads end, it exists. But at times, it finished all task and then is not getting terminated. The thread dump is as follows:


In my actual application,ThreadPoolExecutor threads go in this state and then it stops responding.

PS: I'm new to JavaRanch. Please let me know if I've not followed any rules.

Regards,
Ravi Rao
Peter Taucher
Ranch Hand

Joined: Nov 18, 2006
Posts: 174
I think you need to shutdown the pool sometime. Try adding this code at the end of your main method:


Censorship is the younger of two shameful sisters, the older one bears the name inquisition.
-- Johann Nepomuk Nestroy
Ravi K Rao
Greenhorn

Joined: Jun 16, 2010
Posts: 2
Hi Peter,

First of all, Thank you for quick response.

This is not the actual program. It is just a simple code which I've written to simulate a similar condition in my actual code. I wont be able to post the actual code here.

The problem is once it gets into this condition, it keeps on waiting and after some point new tasks are not executed at all.

Regards,
Ravi Rao
Peter Taucher
Ranch Hand

Joined: Nov 18, 2006
Posts: 174
And does that change the need to shutdown the pool at some time? At least take look into the API docs (especially under 'Finalization').
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3003
    
    9
Peter Taucher wrote:I think you need to shutdown the pool sometime. Try adding this code at the end of your main method:


That seems unnecessarily complex - shutdown() will already wait until all current tasks are completed, so I'd just call it directly.

The problem is, it sounds like Ravi wants to keep this pool around awhile to await future tasks, and sometimes all the threads get suspended somehow.

I can't seem to recreate the problem on my machine using the code shown. I'm running a Mac, so the different JVM internals may be why I can't recreate it.

When I do a thread dump, I get virtually the same results as Ravi - with the critical difference that all his threads say "(Suspended)", while mine do not. So the question is, what causes the threads to become suspended?

One thing to try is to run this in an IDE, and set a break point on the suspend() method in the Thread class. If something in the system is calling suspend(), this should let you know what it is.

Anyone else got any ideas on what might cause the threads to become suspended?
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: ThreadPoolExecutor getting stuck
 
Similar Threads
How to correctly use a fixed size thread pool?
stopping a runnable in ThreadPoolExecutor
Stopping a thread in threadPoolExecutor
ThreadpoolExecutor error
ExecutorService is causing the program to hang for few minutes