• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Ravi Kiran Va
Ranch Hand
Posts: 2234
Eclipse IDE Firefox Browser Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 .
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look up Java threading. What have you tried so far?
 
Ravi Kiran Va
Ranch Hand
Posts: 2234
Eclipse IDE Firefox Browser Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually I know this way only
Get the time using system.currenttimemillis , check it and throw the exception ??

how to do using Threads??
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What references are you using to learn about threads?
 
Ankit Garg
Sheriff
Posts: 9529
33
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Get the time using system.currenttimemillis , check it and throw the exception ??

There's a Thread.join(long timeout) method which you can use...
 
Ravi Kiran Va
Ranch Hand
Posts: 2234
Eclipse IDE Firefox Browser Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much
 
Ravi Kiran Va
Ranch Hand
Posts: 2234
Eclipse IDE Firefox Browser Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 .
 
Andrew Monkhouse
author and jackaroo
Marshal Commander
Pie
Posts: 11945
212
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ankit Garg
Sheriff
Posts: 9529
33
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic