This week's book giveaway is in the Design forum.
We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!
See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Thread runs for 1 second only.

 
Jigar Naik
Ranch Hand
Posts: 762
  • Mark post as helpful
  • send pies
  • 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
 
Stephan van Hulst
Bartender
Pie
Posts: 5572
53
  • Mark post as helpful
  • send pies
  • 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.
 
Henry Wong
author
Marshal
Pie
Posts: 20997
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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: 762
  • Mark post as helpful
  • send pies
  • 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
Bartender
Pie
Posts: 5572
53
  • Mark post as helpful
  • send pies
  • 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: 762
  • Mark post as helpful
  • send pies
  • 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: 762
  • Mark post as helpful
  • send pies
  • 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... !!!
 
Tom Reilly
Rancher
Posts: 618
  • Mark post as helpful
  • send pies
  • 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)?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic