wood burning stoves*
The moose likes Threads and Synchronization and the fly likes Running multiple Threads for a long time Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Running multiple Threads for a long time " Watch "Running multiple Threads for a long time " New topic
Author

Running multiple Threads for a long time

Varun Nambuthiri
Ranch Hand

Joined: Sep 20, 2010
Posts: 76
Hi

I was thinking how i could run multiple threads and keep them running till the program ends ..

I have created and started threads like this


All's well that Debugs well .
Krishna Kumar S
Greenhorn

Joined: Jan 31, 2012
Posts: 12
What do you mean by until program quits??? if you want to periodically execute some task have a look on " ScheduledExecutorService".

Example:

ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor();
scheduler.scheduleAtFixedRate(runnabletask, initial_delay, periodic_interval, Timeunit);
Krishna Kumar S
Greenhorn

Joined: Jan 31, 2012
Posts: 12
What do you mean by until program quits??? if you want to periodically execute some task have a look on " ScheduledExecutorService".

Example:

ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor();
scheduler.scheduleAtFixedRate(runnabletask, initial_delay, periodic_interval, Timeunit);
Varun Nambuthiri
Ranch Hand

Joined: Sep 20, 2010
Posts: 76
Thanks for the quick reply.

I think you will be able to give a better solution when you know what the problem is . See I have three threads on a server like appication
1) -which waits for an incoming connection and accepts it
2) -Then saving the client's log in details
3) -One for listening to a port for incoming pakcets .

So I need to be able to run then till the application quits
Thanks
Anayonkar Shivalkar
Bartender

Joined: Dec 08, 2010
Posts: 1510
    
    5

Hi Varun,

You can run those threads in infinite loop. When application quits, those threads will stop anyway.

Or even better, if you have a way to stop application(say a thread waiting for exit command etc.), then you can send message to those threads. The message can be as simple as a boolean variable. So once exit command is received, all threads (which are running in a loop and checking if exit command is received) will finish current task, make cleanup (close log file descriptor, close sockets etc.) and break the loop. Join those threads in main and exit.

I hope this helps.


Regards,
Anayonkar Shivalkar (SCJP, SCWCD, OCMJD, OCEEJBD)
Varun Nambuthiri
Ranch Hand

Joined: Sep 20, 2010
Posts: 76
Thanks

I will try that and let you know the results
Varun Nambuthiri
Ranch Hand

Joined: Sep 20, 2010
Posts: 76
Hi

Did that and it helped .. Thanks !!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Running multiple Threads for a long time