

![]() | Start a set with this search |
![]() | Include this search in one of my sets |
![]() | Exclude this search from one of my sets |
![]() | Permalink to these results Paste this link in email or IM: |
| Atom feed for tracking future search results Paste this URL into your reader: |
3 messages in net.java.dev.jna.usersRe: [jna-users] Timeout on method call| From | Sent On | Attachments |
|---|---|---|
| Galex | Apr 14, 2008 7:01 am | |
| Albert Strasheim | Apr 14, 2008 7:22 am | |
| Galex | Apr 14, 2008 11:55 pm |

![]() | Permalink for this message Paste this link in email or IM: |
![]() | Permalink for this thread Paste this link in email or IM: |
| Atom feed for this thread Paste this URL into your reader: |
| Subject: | Re: [jna-users] Timeout on method call | Actions... |
|---|---|---|
| From: | Albert Strasheim (full...@gmail.com) | |
| Date: | Apr 14, 2008 7:22:09 am | |
| List: | net.java.dev.jna.users | |
Hello,
On Mon, Apr 14, 2008 at 4:02 PM, Galex <fire...@gmail.com> wrote:
Hello there,
I have a problem and I don't know if the JNA API can help or not. Basically what I would like to do, is to set a timeout on a method call.
Not really a job for JNA, I think.
In fact I have my library declaring some methods stored in the loaded DLL, and all is working just fine. But one method I'm calling can last up to many seconds. The problem is that I don't want this method to get stuck for a long time, stopping the execution of the rest of the java program.
I tried to make the call in a separate Thread but since it's only one method call (one line of code) and not a loop that will be executed many times, I can't really stop the Thread when the run method exceed the time I've fixed.
Various caveats might apply if too many calls to this method run for too long.
Notice that I can't touch to the DLL source code, it's not mine.
I searched through the JNA API but I did not find what i'm searching for (if there is).
If anybody can help me, it would be very nice :-).
Thanks in advance !
How about this:
import java.util.Random; import java.util.concurrent.Callable; import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Future; import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException;
public class TimeoutExample { private static final Random rnd = new Random(0);
public static void longMethod() throws InterruptedException { Thread.sleep((long) (500.0 + 1000.0 * rnd.nextDouble())); }
public static void main(final String[] args) { Callable<Object> callable = new Callable<Object>() { @Override public Object call() throws Exception { longMethod(); return new Object(); } }; ExecutorService executorService = Executors.newCachedThreadPool();
while (true) { Future<Object> future = executorService.submit(callable); try { Object result = future.get(1000L, TimeUnit.MILLISECONDS); System.out.println("got an object: " + result); } catch (ExecutionException e) { throw new RuntimeException(e); } catch (TimeoutException e) { System.out.println("timeout... trying again"); } catch (InterruptedException e) { System.out.println("interrupted"); } } } }
With this could you should see, on average, about half of the method calls completing within the alotted time and the other half timing out.
Cheers,
Albert







