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 executing thread only once Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "executing thread only once" Watch "executing thread only once" New topic

executing thread only once

Mario Gimenez
Ranch Hand

Joined: Mar 23, 2010
Posts: 38
Hi, this is my problem.

I have a class bo.
and two classes producer (runnable)
and consumer (runnable)

in bo I do producer.start() and consumer.start().
I need executing only once this operation via web.
I mean If the user clic a button then execute the bo and starts the threads.
but If I the user clic again I need to set a String like "running"

Thanks for your help!.
Sujoy Choudhury
Ranch Hand

Joined: Sep 17, 2008
Posts: 136

You can handle it in the client side itself.
When the user clicks for the first time change the caption to "Running..." and programatically don't make a call to the server side again.
You can easily do it I guess.

Thanks and Regards,
Sudarshan Devardekar

Joined: Dec 24, 2011
Posts: 4

Approach suggested by Sujoy Choudhury is best to use for web application.

Here is other way you can restrict thread execution number:
Use a ThreadGroup. Create your thread in a ThreadGroup. You get active thread count from a ThreadGroup, which you can use to check how many threads are present and can prevent creation of new thread.
I agree. Here's the link:
subject: executing thread only once
jQuery in Action, 3rd edition