Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8002027
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T16:14:57+00:00 2026-06-04T16:14:57+00:00

I am trying to implement a code where I want to call a function

  • 0

I am trying to implement a code where I want to call a function from JNI which should have a timeout. If it exceeds the timeout, I want to terminate the native task. I am posting a piece of code to show as an example.

void myFunction(timeOutInSeconds)
{
    if(timeOutInSeconds > 0)
    {
        ExecutorService executor = Executors.newCachedThreadPool();
        Callable<Integer> task = new Callable<Integer>() {
            public Integer call() {
                System.out.println("Calling JNI Task");
                JNI_Task();
                System.out.println("Finished JNI Task");
                return 0;                              
            }
        };

        Future<Integer> future = executor.submit(task);
        try 
        {
            @SuppressWarnings("unused")
            Integer result = future.get(timeOutInSeconds, TimeUnit.SECONDS); 
        } 
        catch (TimeoutException ex)
        {
            // handle the timeout               
            kill_task_in_JNI();     

            // future.cancel(true);
            return TIMEOUT;

        } catch (InterruptedException e) {
            // handle the interrupts
        } catch (ExecutionException e) {
            // handle other exceptions
        } 
        finally 
        {
            // future.cancel(true);
            executor.shutdown();
        }
    }
    else
        JNI_Task();
}

There are several questions –

  • Where should I exactly put future.cancel(). There are 2 locations which are commented.
  • If I run this function with timeOutInSeconds = 0, it runs perfectly.
    However Irrespective of the value of timeOutInSeconds, the task gets stuck up and
    the JNI task does not get called. I check this by putting printf’s in the JNI
    code. The task takes 1 second to execute and I gave 30 seconds, 5 minutes etc. still it
    is stuck up.

Is there any problem with such approach?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T16:14:59+00:00Added an answer on June 4, 2026 at 4:14 pm

    You can (and in this case should) call future.cancel() only in the finally block. http://docs.oracle.com/javase/tutorial/essential/exceptions/finally.html.

    About the 2nd question, its not clear for me if the problem also occur when timeOutInSeconds=0. Is this the case? Can you provide the content of the JNI_TASK() method?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to implement delegation for a class which should call it's delegate (if
I'm trying to implement webgl shadow mapping from an example code that already implements
I have a simple WCF service that I call server side from code behind
I'm trying to implement a program which runs a function limited by a fixed
I am trying to implement some code on my web page to auto-scroll after
I am trying to implement a simple code tester : a very basic version
Iam trying to implement a simple JMS(traditional not using springs) code in eclipse using
I'm trying to implement autocomplete for a textbox using the following code, but it's
I'm trying to implement http://github.com/bengottlieb/Twitter-OAuth-iPhone/tree/master into my code. I'm following the Demo provided and
Hello i'm trying to implement an AsynController, here is my code: [NoCache] public class

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.