Java timeout function is not working for my below codes
I tried to set a 1-second time limit for my SQL query in Java, using the methods:
How to timeout a thread
public class App
public static void main(String args) throws Exception
ExecutorService executor = Executors.newSingleThreadExecutor();
Future<String> future = executor.submit(new Task());
try
System.out.println("Started..");
System.out.println(future.get(1, TimeUnit.SECONDS));
System.out.println("Finished!");
catch (TimeoutException e)
future.cancel(true);
System.out.println("Terminated!");
executor.shutdownNow();
class Task implements Callable<String>
@Override
public String call() throws Exception
try
// some codes to do query via SQL Server JDBC, assuming it takes 10 seconds.
ResultSet result = statement.executeQuery();
// some codes to print the query result
return "Done";
catch (Exception e)
System.out.println();
e.printStackTrace();
However, I found that though it prints 'Terminated' after 1 second, the program keeps running and prints the query result after 10 seconds. What's the reason why it doesn't work and how to fix it?
java
|
show 1 more comment
I tried to set a 1-second time limit for my SQL query in Java, using the methods:
How to timeout a thread
public class App
public static void main(String args) throws Exception
ExecutorService executor = Executors.newSingleThreadExecutor();
Future<String> future = executor.submit(new Task());
try
System.out.println("Started..");
System.out.println(future.get(1, TimeUnit.SECONDS));
System.out.println("Finished!");
catch (TimeoutException e)
future.cancel(true);
System.out.println("Terminated!");
executor.shutdownNow();
class Task implements Callable<String>
@Override
public String call() throws Exception
try
// some codes to do query via SQL Server JDBC, assuming it takes 10 seconds.
ResultSet result = statement.executeQuery();
// some codes to print the query result
return "Done";
catch (Exception e)
System.out.println();
e.printStackTrace();
However, I found that though it prints 'Terminated' after 1 second, the program keeps running and prints the query result after 10 seconds. What's the reason why it doesn't work and how to fix it?
java
As mentioned in the question you linked, you have to checkThread.interrupted()
inside yourcall()
method. If it is set totrue
you return the method immediately and don't print/use the result anymore.
– Progman
Nov 12 '18 at 22:45
@Progman Thanks. But how would I do this. It's just a single line of code runs about 10s. (If I'm running a for loop that may run for a long time, I may check interrupted for each iteration, but this is not the case).
– MTANG
Nov 13 '18 at 14:48
If the method you are calling does not check theThread.interrupted()
call itself you have to wait for it to finish. But you can still checkThread.interrupted()
after the long running call and decide not to do you actions you normally would do (like printing the result set).
– Progman
Nov 13 '18 at 14:54
@Progman Actually we want to raise an exception if there is a time out. (Rather than 'wait until finish and decide what to do'
– MTANG
Nov 13 '18 at 15:02
As mentioned by "Joe C" you can usesetQueryTimeout()
to set the timeout before callingexecuteQuery()
. But since you said it didn't work for you, you might need to edit your question to include the full source code you have (with thesetQueryTimeout()
call) and how exactly it "isn't working".
– Progman
Nov 13 '18 at 15:14
|
show 1 more comment
I tried to set a 1-second time limit for my SQL query in Java, using the methods:
How to timeout a thread
public class App
public static void main(String args) throws Exception
ExecutorService executor = Executors.newSingleThreadExecutor();
Future<String> future = executor.submit(new Task());
try
System.out.println("Started..");
System.out.println(future.get(1, TimeUnit.SECONDS));
System.out.println("Finished!");
catch (TimeoutException e)
future.cancel(true);
System.out.println("Terminated!");
executor.shutdownNow();
class Task implements Callable<String>
@Override
public String call() throws Exception
try
// some codes to do query via SQL Server JDBC, assuming it takes 10 seconds.
ResultSet result = statement.executeQuery();
// some codes to print the query result
return "Done";
catch (Exception e)
System.out.println();
e.printStackTrace();
However, I found that though it prints 'Terminated' after 1 second, the program keeps running and prints the query result after 10 seconds. What's the reason why it doesn't work and how to fix it?
java
I tried to set a 1-second time limit for my SQL query in Java, using the methods:
How to timeout a thread
public class App
public static void main(String args) throws Exception
ExecutorService executor = Executors.newSingleThreadExecutor();
Future<String> future = executor.submit(new Task());
try
System.out.println("Started..");
System.out.println(future.get(1, TimeUnit.SECONDS));
System.out.println("Finished!");
catch (TimeoutException e)
future.cancel(true);
System.out.println("Terminated!");
executor.shutdownNow();
class Task implements Callable<String>
@Override
public String call() throws Exception
try
// some codes to do query via SQL Server JDBC, assuming it takes 10 seconds.
ResultSet result = statement.executeQuery();
// some codes to print the query result
return "Done";
catch (Exception e)
System.out.println();
e.printStackTrace();
However, I found that though it prints 'Terminated' after 1 second, the program keeps running and prints the query result after 10 seconds. What's the reason why it doesn't work and how to fix it?
java
java
asked Nov 12 '18 at 22:29
MTANG
1368
1368
As mentioned in the question you linked, you have to checkThread.interrupted()
inside yourcall()
method. If it is set totrue
you return the method immediately and don't print/use the result anymore.
– Progman
Nov 12 '18 at 22:45
@Progman Thanks. But how would I do this. It's just a single line of code runs about 10s. (If I'm running a for loop that may run for a long time, I may check interrupted for each iteration, but this is not the case).
– MTANG
Nov 13 '18 at 14:48
If the method you are calling does not check theThread.interrupted()
call itself you have to wait for it to finish. But you can still checkThread.interrupted()
after the long running call and decide not to do you actions you normally would do (like printing the result set).
– Progman
Nov 13 '18 at 14:54
@Progman Actually we want to raise an exception if there is a time out. (Rather than 'wait until finish and decide what to do'
– MTANG
Nov 13 '18 at 15:02
As mentioned by "Joe C" you can usesetQueryTimeout()
to set the timeout before callingexecuteQuery()
. But since you said it didn't work for you, you might need to edit your question to include the full source code you have (with thesetQueryTimeout()
call) and how exactly it "isn't working".
– Progman
Nov 13 '18 at 15:14
|
show 1 more comment
As mentioned in the question you linked, you have to checkThread.interrupted()
inside yourcall()
method. If it is set totrue
you return the method immediately and don't print/use the result anymore.
– Progman
Nov 12 '18 at 22:45
@Progman Thanks. But how would I do this. It's just a single line of code runs about 10s. (If I'm running a for loop that may run for a long time, I may check interrupted for each iteration, but this is not the case).
– MTANG
Nov 13 '18 at 14:48
If the method you are calling does not check theThread.interrupted()
call itself you have to wait for it to finish. But you can still checkThread.interrupted()
after the long running call and decide not to do you actions you normally would do (like printing the result set).
– Progman
Nov 13 '18 at 14:54
@Progman Actually we want to raise an exception if there is a time out. (Rather than 'wait until finish and decide what to do'
– MTANG
Nov 13 '18 at 15:02
As mentioned by "Joe C" you can usesetQueryTimeout()
to set the timeout before callingexecuteQuery()
. But since you said it didn't work for you, you might need to edit your question to include the full source code you have (with thesetQueryTimeout()
call) and how exactly it "isn't working".
– Progman
Nov 13 '18 at 15:14
As mentioned in the question you linked, you have to check
Thread.interrupted()
inside your call()
method. If it is set to true
you return the method immediately and don't print/use the result anymore.– Progman
Nov 12 '18 at 22:45
As mentioned in the question you linked, you have to check
Thread.interrupted()
inside your call()
method. If it is set to true
you return the method immediately and don't print/use the result anymore.– Progman
Nov 12 '18 at 22:45
@Progman Thanks. But how would I do this. It's just a single line of code runs about 10s. (If I'm running a for loop that may run for a long time, I may check interrupted for each iteration, but this is not the case).
– MTANG
Nov 13 '18 at 14:48
@Progman Thanks. But how would I do this. It's just a single line of code runs about 10s. (If I'm running a for loop that may run for a long time, I may check interrupted for each iteration, but this is not the case).
– MTANG
Nov 13 '18 at 14:48
If the method you are calling does not check the
Thread.interrupted()
call itself you have to wait for it to finish. But you can still check Thread.interrupted()
after the long running call and decide not to do you actions you normally would do (like printing the result set).– Progman
Nov 13 '18 at 14:54
If the method you are calling does not check the
Thread.interrupted()
call itself you have to wait for it to finish. But you can still check Thread.interrupted()
after the long running call and decide not to do you actions you normally would do (like printing the result set).– Progman
Nov 13 '18 at 14:54
@Progman Actually we want to raise an exception if there is a time out. (Rather than 'wait until finish and decide what to do'
– MTANG
Nov 13 '18 at 15:02
@Progman Actually we want to raise an exception if there is a time out. (Rather than 'wait until finish and decide what to do'
– MTANG
Nov 13 '18 at 15:02
As mentioned by "Joe C" you can use
setQueryTimeout()
to set the timeout before calling executeQuery()
. But since you said it didn't work for you, you might need to edit your question to include the full source code you have (with the setQueryTimeout()
call) and how exactly it "isn't working".– Progman
Nov 13 '18 at 15:14
As mentioned by "Joe C" you can use
setQueryTimeout()
to set the timeout before calling executeQuery()
. But since you said it didn't work for you, you might need to edit your question to include the full source code you have (with the setQueryTimeout()
call) and how exactly it "isn't working".– Progman
Nov 13 '18 at 15:14
|
show 1 more comment
3 Answers
3
active
oldest
votes
shutdownNow
doesn't actually stop a thread, it merely sends a signal (an interrupt) that the Thread can act upon. Stopping a Thread in Java is tricky because while you can just kil the thread (with Thread.stop
), you really shouldn't because you have no idea what state the Thread is in and what it will leave behind.
You can find more information in the documentation.
How could I do that with thread.stop(). Sorry that I'm new to Java. Also, will GC still work to collect the gabbage if I force to kill a thread.
– MTANG
Nov 13 '18 at 14:49
You do not want to dothread.stop()
. Ever.
– Joe C
Nov 13 '18 at 20:59
add a comment |
Calling cancel
on a future does not guarantee that the job will be cancelled. It depends on the method checking periodically for interrupts, and then aborting if an interrupt is detected. Statement.execute()
does not do that.
In your case, given you are executing a SQL statement, there is a method in the Statement
class (setQueryTimeout
) which achieves what you appear to be after without over-engineering timeouts by other means.
Thanks. that's very helpful.
– MTANG
Nov 13 '18 at 14:46
You're welcome. Don't forget to mark a best answer.
– Joe C
Nov 13 '18 at 20:59
add a comment |
Another way you can approach this is by using the thread.sleep() method. I often use it when I want my program to simply pause for a short or long period of time. In the parameters, you put values in thousands that correspond to seconds. For example:
public static void main(String args) throws InterruptedException // Required for thread.sleep()
System.out.println("Hi there.");
Thread.sleep(2000); // Wait two seconds before running the next line of code
System.out.println("Goodbye.");
This is quite basic, but can be used for more than just strings. Hope this helps.
You are attempting to solve a different problem to the one the OP is asking about. The OP doesn't want to pause his program, but to stop an operation if it hasn't completed within a certain length of time.
– Joe C
Nov 13 '18 at 21:01
@JoeC Oh I see, sorry for the mistake. I'm trying to understand and teach to help myself learn at the same time.
– ShaheerL
Dec 15 '18 at 21:23
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53271025%2fjava-timeout-function-is-not-working-for-my-below-codes%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
shutdownNow
doesn't actually stop a thread, it merely sends a signal (an interrupt) that the Thread can act upon. Stopping a Thread in Java is tricky because while you can just kil the thread (with Thread.stop
), you really shouldn't because you have no idea what state the Thread is in and what it will leave behind.
You can find more information in the documentation.
How could I do that with thread.stop(). Sorry that I'm new to Java. Also, will GC still work to collect the gabbage if I force to kill a thread.
– MTANG
Nov 13 '18 at 14:49
You do not want to dothread.stop()
. Ever.
– Joe C
Nov 13 '18 at 20:59
add a comment |
shutdownNow
doesn't actually stop a thread, it merely sends a signal (an interrupt) that the Thread can act upon. Stopping a Thread in Java is tricky because while you can just kil the thread (with Thread.stop
), you really shouldn't because you have no idea what state the Thread is in and what it will leave behind.
You can find more information in the documentation.
How could I do that with thread.stop(). Sorry that I'm new to Java. Also, will GC still work to collect the gabbage if I force to kill a thread.
– MTANG
Nov 13 '18 at 14:49
You do not want to dothread.stop()
. Ever.
– Joe C
Nov 13 '18 at 20:59
add a comment |
shutdownNow
doesn't actually stop a thread, it merely sends a signal (an interrupt) that the Thread can act upon. Stopping a Thread in Java is tricky because while you can just kil the thread (with Thread.stop
), you really shouldn't because you have no idea what state the Thread is in and what it will leave behind.
You can find more information in the documentation.
shutdownNow
doesn't actually stop a thread, it merely sends a signal (an interrupt) that the Thread can act upon. Stopping a Thread in Java is tricky because while you can just kil the thread (with Thread.stop
), you really shouldn't because you have no idea what state the Thread is in and what it will leave behind.
You can find more information in the documentation.
answered Nov 12 '18 at 22:40
Jakg
405311
405311
How could I do that with thread.stop(). Sorry that I'm new to Java. Also, will GC still work to collect the gabbage if I force to kill a thread.
– MTANG
Nov 13 '18 at 14:49
You do not want to dothread.stop()
. Ever.
– Joe C
Nov 13 '18 at 20:59
add a comment |
How could I do that with thread.stop(). Sorry that I'm new to Java. Also, will GC still work to collect the gabbage if I force to kill a thread.
– MTANG
Nov 13 '18 at 14:49
You do not want to dothread.stop()
. Ever.
– Joe C
Nov 13 '18 at 20:59
How could I do that with thread.stop(). Sorry that I'm new to Java. Also, will GC still work to collect the gabbage if I force to kill a thread.
– MTANG
Nov 13 '18 at 14:49
How could I do that with thread.stop(). Sorry that I'm new to Java. Also, will GC still work to collect the gabbage if I force to kill a thread.
– MTANG
Nov 13 '18 at 14:49
You do not want to do
thread.stop()
. Ever.– Joe C
Nov 13 '18 at 20:59
You do not want to do
thread.stop()
. Ever.– Joe C
Nov 13 '18 at 20:59
add a comment |
Calling cancel
on a future does not guarantee that the job will be cancelled. It depends on the method checking periodically for interrupts, and then aborting if an interrupt is detected. Statement.execute()
does not do that.
In your case, given you are executing a SQL statement, there is a method in the Statement
class (setQueryTimeout
) which achieves what you appear to be after without over-engineering timeouts by other means.
Thanks. that's very helpful.
– MTANG
Nov 13 '18 at 14:46
You're welcome. Don't forget to mark a best answer.
– Joe C
Nov 13 '18 at 20:59
add a comment |
Calling cancel
on a future does not guarantee that the job will be cancelled. It depends on the method checking periodically for interrupts, and then aborting if an interrupt is detected. Statement.execute()
does not do that.
In your case, given you are executing a SQL statement, there is a method in the Statement
class (setQueryTimeout
) which achieves what you appear to be after without over-engineering timeouts by other means.
Thanks. that's very helpful.
– MTANG
Nov 13 '18 at 14:46
You're welcome. Don't forget to mark a best answer.
– Joe C
Nov 13 '18 at 20:59
add a comment |
Calling cancel
on a future does not guarantee that the job will be cancelled. It depends on the method checking periodically for interrupts, and then aborting if an interrupt is detected. Statement.execute()
does not do that.
In your case, given you are executing a SQL statement, there is a method in the Statement
class (setQueryTimeout
) which achieves what you appear to be after without over-engineering timeouts by other means.
Calling cancel
on a future does not guarantee that the job will be cancelled. It depends on the method checking periodically for interrupts, and then aborting if an interrupt is detected. Statement.execute()
does not do that.
In your case, given you are executing a SQL statement, there is a method in the Statement
class (setQueryTimeout
) which achieves what you appear to be after without over-engineering timeouts by other means.
answered Nov 12 '18 at 22:46
Joe C
10.6k62341
10.6k62341
Thanks. that's very helpful.
– MTANG
Nov 13 '18 at 14:46
You're welcome. Don't forget to mark a best answer.
– Joe C
Nov 13 '18 at 20:59
add a comment |
Thanks. that's very helpful.
– MTANG
Nov 13 '18 at 14:46
You're welcome. Don't forget to mark a best answer.
– Joe C
Nov 13 '18 at 20:59
Thanks. that's very helpful.
– MTANG
Nov 13 '18 at 14:46
Thanks. that's very helpful.
– MTANG
Nov 13 '18 at 14:46
You're welcome. Don't forget to mark a best answer.
– Joe C
Nov 13 '18 at 20:59
You're welcome. Don't forget to mark a best answer.
– Joe C
Nov 13 '18 at 20:59
add a comment |
Another way you can approach this is by using the thread.sleep() method. I often use it when I want my program to simply pause for a short or long period of time. In the parameters, you put values in thousands that correspond to seconds. For example:
public static void main(String args) throws InterruptedException // Required for thread.sleep()
System.out.println("Hi there.");
Thread.sleep(2000); // Wait two seconds before running the next line of code
System.out.println("Goodbye.");
This is quite basic, but can be used for more than just strings. Hope this helps.
You are attempting to solve a different problem to the one the OP is asking about. The OP doesn't want to pause his program, but to stop an operation if it hasn't completed within a certain length of time.
– Joe C
Nov 13 '18 at 21:01
@JoeC Oh I see, sorry for the mistake. I'm trying to understand and teach to help myself learn at the same time.
– ShaheerL
Dec 15 '18 at 21:23
add a comment |
Another way you can approach this is by using the thread.sleep() method. I often use it when I want my program to simply pause for a short or long period of time. In the parameters, you put values in thousands that correspond to seconds. For example:
public static void main(String args) throws InterruptedException // Required for thread.sleep()
System.out.println("Hi there.");
Thread.sleep(2000); // Wait two seconds before running the next line of code
System.out.println("Goodbye.");
This is quite basic, but can be used for more than just strings. Hope this helps.
You are attempting to solve a different problem to the one the OP is asking about. The OP doesn't want to pause his program, but to stop an operation if it hasn't completed within a certain length of time.
– Joe C
Nov 13 '18 at 21:01
@JoeC Oh I see, sorry for the mistake. I'm trying to understand and teach to help myself learn at the same time.
– ShaheerL
Dec 15 '18 at 21:23
add a comment |
Another way you can approach this is by using the thread.sleep() method. I often use it when I want my program to simply pause for a short or long period of time. In the parameters, you put values in thousands that correspond to seconds. For example:
public static void main(String args) throws InterruptedException // Required for thread.sleep()
System.out.println("Hi there.");
Thread.sleep(2000); // Wait two seconds before running the next line of code
System.out.println("Goodbye.");
This is quite basic, but can be used for more than just strings. Hope this helps.
Another way you can approach this is by using the thread.sleep() method. I often use it when I want my program to simply pause for a short or long period of time. In the parameters, you put values in thousands that correspond to seconds. For example:
public static void main(String args) throws InterruptedException // Required for thread.sleep()
System.out.println("Hi there.");
Thread.sleep(2000); // Wait two seconds before running the next line of code
System.out.println("Goodbye.");
This is quite basic, but can be used for more than just strings. Hope this helps.
answered Nov 12 '18 at 23:36
ShaheerL
14
14
You are attempting to solve a different problem to the one the OP is asking about. The OP doesn't want to pause his program, but to stop an operation if it hasn't completed within a certain length of time.
– Joe C
Nov 13 '18 at 21:01
@JoeC Oh I see, sorry for the mistake. I'm trying to understand and teach to help myself learn at the same time.
– ShaheerL
Dec 15 '18 at 21:23
add a comment |
You are attempting to solve a different problem to the one the OP is asking about. The OP doesn't want to pause his program, but to stop an operation if it hasn't completed within a certain length of time.
– Joe C
Nov 13 '18 at 21:01
@JoeC Oh I see, sorry for the mistake. I'm trying to understand and teach to help myself learn at the same time.
– ShaheerL
Dec 15 '18 at 21:23
You are attempting to solve a different problem to the one the OP is asking about. The OP doesn't want to pause his program, but to stop an operation if it hasn't completed within a certain length of time.
– Joe C
Nov 13 '18 at 21:01
You are attempting to solve a different problem to the one the OP is asking about. The OP doesn't want to pause his program, but to stop an operation if it hasn't completed within a certain length of time.
– Joe C
Nov 13 '18 at 21:01
@JoeC Oh I see, sorry for the mistake. I'm trying to understand and teach to help myself learn at the same time.
– ShaheerL
Dec 15 '18 at 21:23
@JoeC Oh I see, sorry for the mistake. I'm trying to understand and teach to help myself learn at the same time.
– ShaheerL
Dec 15 '18 at 21:23
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53271025%2fjava-timeout-function-is-not-working-for-my-below-codes%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
As mentioned in the question you linked, you have to check
Thread.interrupted()
inside yourcall()
method. If it is set totrue
you return the method immediately and don't print/use the result anymore.– Progman
Nov 12 '18 at 22:45
@Progman Thanks. But how would I do this. It's just a single line of code runs about 10s. (If I'm running a for loop that may run for a long time, I may check interrupted for each iteration, but this is not the case).
– MTANG
Nov 13 '18 at 14:48
If the method you are calling does not check the
Thread.interrupted()
call itself you have to wait for it to finish. But you can still checkThread.interrupted()
after the long running call and decide not to do you actions you normally would do (like printing the result set).– Progman
Nov 13 '18 at 14:54
@Progman Actually we want to raise an exception if there is a time out. (Rather than 'wait until finish and decide what to do'
– MTANG
Nov 13 '18 at 15:02
As mentioned by "Joe C" you can use
setQueryTimeout()
to set the timeout before callingexecuteQuery()
. But since you said it didn't work for you, you might need to edit your question to include the full source code you have (with thesetQueryTimeout()
call) and how exactly it "isn't working".– Progman
Nov 13 '18 at 15:14