This week's book giveaway is in the Cloud/Virtualization forum.
We're giving away four copies of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds and have James Denton on-line!
See this thread for details.
Win a copy of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds this week in the Cloud/Virtualization forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

Running multiple Threads for a long time  RSS feed

 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Bartender
Posts: 1558
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Varun Nambuthiri
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks

I will try that and let you know the results
 
Varun Nambuthiri
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

Did that and it helped .. Thanks !!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!