File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Threads and Synchronization and the fly likes Thread runs for 1 second only. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Thread runs for 1 second only." Watch "Thread runs for 1 second only." New topic
Author

Thread runs for 1 second only.

Jigar Naik
Ranch Hand

Joined: Dec 12, 2006
Posts: 759
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


Jigar Naik


Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3649
    
  17

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
Sheriff

Joined: Sep 28, 2004
Posts: 19073
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Jigar Naik
Ranch Hand

Joined: Dec 12, 2006
Posts: 759
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

Joined: Sep 20, 2010
Posts: 3649
    
  17

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

Joined: Dec 12, 2006
Posts: 759
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

Joined: Dec 12, 2006
Posts: 759
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

Joined: Jun 01, 2010
Posts: 618
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)?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Thread runs for 1 second only.