• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Thread runs for 1 second only.

 
Ranch Hand
Posts: 763
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I am writing a small application in which computer A sends message to Computer B using socket. Where i want to prevent the number of message sent/second.

So i want to create a message senderThread which will send only N number of message within 1 second. If 1 second is too short for sending N number of messages, rest of the messages should not be sent.

Lets say for example N = 10

so my thread should sent only 10 messages/second. if my thread finishes sending 10 messages in 100 ms rest of the 900 ms thread should be idle.

in the case where N = 10000

and my thread is able to send 6000 messages in 1 second. rest of the 4000 messages shouldn't be sent.

How do i do that ? Do i have to use threading for handling the scenario of sending N messages/second or there is some other way to handle this scenario ?

I don't have much experience in Thread programming

Thanks & Regards,
Jigar
 
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, you just save the time when you start sending, and after every message you check how much later it is. If there's enough time, send another message. If your second is over, you don't send another message.
You can do all of this in a single thread.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:No, you just save the time when you start sending, and after every message you check how much later it is. If there's enough time, send another message. If your second is over, you don't send another message.
You can do all of this in a single thread.



Agreed. This isn't really a thread related question -- except you want this done by a thread.


As for Stephan's solution, I also agree.... Except, you should also keep a message count, and when the count is exceeded too, you also don't sent any more messages either.

Henry
 
Jigar Naik
Ranch Hand
Posts: 763
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
but if we consider performance which one is th best solution ? Thread or time check ?

does checking time every time before sending message wont eat my MS or NS ?
 
Stephan van Hulst
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Threads have nothing to do with your problem. Threads don't magically add performance. You have to be able to separate your task into pieces that can be performed by different threads at the same time. There's no way to split your task up sensibly in this case.

Whatever your solution, you will have to check time, even if there *was* a way to use multiple threads. Yes, this will eat CPU cycles, but this is a small price to pay for a program that actually works, yes?

Henry Wong wrote:As for Stephan's solution, I also agree.... Except, you should also keep a message count, and when the count is exceeded too, you also don't sent any more messages either.


D'oh
 
Jigar Naik
Ranch Hand
Posts: 763
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes you are right. but somebody is going to judge me based on this small program, and i will just get to know the result either pass or fail. So that's why even though it's a small program i wanted to know the best way.

currently i have used TimerTask to keep my program running.

 
Jigar Naik
Ranch Hand
Posts: 763
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i tried by checking time... the application stops working after 2 -3 iteration... noting comes on console



output



can anybody help me out solving this issue... !!!
 
Rancher
Posts: 618
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My guess is that since you are only catching IOException (starting at line 13):another exception is being thrown and that is being caught (and ignored) in another piece of code that you have not posted. Try catching Exception in that try-catch block and print the stack trace.

BTW, did you intend to recursively call pitcherTask (see last line of your code that you posted)?
 
please buy this thing and then I get a fat cut of the action:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic