File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Threads and Synchronization and the fly likes Control threads execution Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Control threads execution" Watch "Control threads execution" New topic

Control threads execution

iamiqbal.m Mohiuddin

Joined: May 11, 2006
Posts: 12
We have a simple jsp/struts UI.
Using the UI we can start a batch process by clicking on Run button.
The batch process would fetch records from DB and write to MQ.
We have a threshold value of 30000 records.
When the program retrieves 30000 records we spawn a new thread and pass that list of 30000 records to that thread which does
writing to MQ and the retrieval continues. This process repeats everytime counter reaches 30000 mark.
Now on the UI we are going to have Stop button. When user clicks on stop button the execution of that batch should be stopped. (All the threads that might have spawned should be stopped on clicking of stop).

We have layered architecture with UI (jsp and forms / actions (struts)) and jdbc framework in separate packages.

Any pointers would be highly apprecited.

Thanks in advance,
Nitesh Kant

Joined: Feb 25, 2007
Posts: 1638

Have you thought of using threadpools for your work? Using them you do not have to take the overhead of creating a new thread and you also get cancel(shutdown) functionality for free!
With jdk5 threadpools come along with the jdk. For version before that you can use the backport of the same.

apigee, a better way to API!
iamiqbal.m Mohiuddin

Joined: May 11, 2006
Posts: 12
Thanks. I appreciate it.

We dont know the number of threads that are going to be created.
Its dynamic based on number os records returned from the database.

I shall explore more on ThreadPool option.
I agree. Here's the link:
subject: Control threads execution
It's not a secret anymore!