Forums Register Login

Needs to throw an Exception if a Method exection takes More time

+Pie Number of slices to send: Send
Hi

First of all sorry if this is not an appropiate forum to post this .

My question is

I have a method in my servlet . I dont want this method execution to take more than 25 ms .

For example if it takes 30 ms i want to throw an exception .

please let me know how can i do this ?? Thanks in advance .
+Pie Number of slices to send: Send
Look up Java threading. What have you tried so far?
+Pie Number of slices to send: Send
Actually I know this way only
Get the time using system.currenttimemillis , check it and throw the exception ??

how to do using Threads??
+Pie Number of slices to send: Send
What references are you using to learn about threads?
+Pie Number of slices to send: Send
 

Get the time using system.currenttimemillis , check it and throw the exception ??


There's a Thread.join(long timeout) method which you can use...
+Pie Number of slices to send: Send
Thank you very much
+Pie Number of slices to send: Send
About join method :

A thread invokes the join method on another Thread in order to wait for another Thread to complete its execution .

Please tell me how does it suits my requirement .

Thanks you .
+Pie Number of slices to send: Send
The method that Ankit pointed you to will only wait a specified amount of time for the other thread to complete. So if your other thread failed to complete in 25ms, your original thread will stop waiting. At that point your original thread can interrupt the worker thread, and throw your exception.
+Pie Number of slices to send: Send
Thread.isAlive would be another method that you can use to see if the other thread has finished or not, because when Thread.join returns, you cannot tell whether the join returned because the other thread completed or the timeout expired...
He's my best friend. Not yours. Mine. You can have this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 1298 times.
Similar Threads
Exception Doubt
Overload methods in subclass?
How do I throw an exception on purpose?
date format error
unlock a deleted record, how to?
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 10:57:26.