How to run background task which is guaranteed to be completed while the process returns in java?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
I want to do something like-
class doSomeTask
public static object do_method(object request)
//Perform some operation on request
response = doSomeOperation(request);
//Want to do this task in background
writeToDB(request, response);
return response;
Process should return immediately after the response is generated while the write to database operation should be done in background.
daemon thread can do this task in background but it does not guarantee task completion.
So if you know any other methods then let me know.
Thanks.
java multithreading background-process
add a comment |
I want to do something like-
class doSomeTask
public static object do_method(object request)
//Perform some operation on request
response = doSomeOperation(request);
//Want to do this task in background
writeToDB(request, response);
return response;
Process should return immediately after the response is generated while the write to database operation should be done in background.
daemon thread can do this task in background but it does not guarantee task completion.
So if you know any other methods then let me know.
Thanks.
java multithreading background-process
If you want to be sure the background task successfully completed you have to ask for that at some time. You could wrapwriteToDB(request, response);
in aCallable
and execute it in aFutureTask
orExecutorService
and invokefuture.get()
to provoke aExecutionException
– Meini
Nov 15 '18 at 9:29
Could you please explain me how to do the same
– Rutvik Shah
Nov 15 '18 at 9:36
add a comment |
I want to do something like-
class doSomeTask
public static object do_method(object request)
//Perform some operation on request
response = doSomeOperation(request);
//Want to do this task in background
writeToDB(request, response);
return response;
Process should return immediately after the response is generated while the write to database operation should be done in background.
daemon thread can do this task in background but it does not guarantee task completion.
So if you know any other methods then let me know.
Thanks.
java multithreading background-process
I want to do something like-
class doSomeTask
public static object do_method(object request)
//Perform some operation on request
response = doSomeOperation(request);
//Want to do this task in background
writeToDB(request, response);
return response;
Process should return immediately after the response is generated while the write to database operation should be done in background.
daemon thread can do this task in background but it does not guarantee task completion.
So if you know any other methods then let me know.
Thanks.
java multithreading background-process
java multithreading background-process
asked Nov 15 '18 at 9:19
Rutvik ShahRutvik Shah
6
6
If you want to be sure the background task successfully completed you have to ask for that at some time. You could wrapwriteToDB(request, response);
in aCallable
and execute it in aFutureTask
orExecutorService
and invokefuture.get()
to provoke aExecutionException
– Meini
Nov 15 '18 at 9:29
Could you please explain me how to do the same
– Rutvik Shah
Nov 15 '18 at 9:36
add a comment |
If you want to be sure the background task successfully completed you have to ask for that at some time. You could wrapwriteToDB(request, response);
in aCallable
and execute it in aFutureTask
orExecutorService
and invokefuture.get()
to provoke aExecutionException
– Meini
Nov 15 '18 at 9:29
Could you please explain me how to do the same
– Rutvik Shah
Nov 15 '18 at 9:36
If you want to be sure the background task successfully completed you have to ask for that at some time. You could wrap
writeToDB(request, response);
in a Callable
and execute it in a FutureTask
or ExecutorService
and invoke future.get()
to provoke a ExecutionException
– Meini
Nov 15 '18 at 9:29
If you want to be sure the background task successfully completed you have to ask for that at some time. You could wrap
writeToDB(request, response);
in a Callable
and execute it in a FutureTask
or ExecutorService
and invoke future.get()
to provoke a ExecutionException
– Meini
Nov 15 '18 at 9:29
Could you please explain me how to do the same
– Rutvik Shah
Nov 15 '18 at 9:36
Could you please explain me how to do the same
– Rutvik Shah
Nov 15 '18 at 9:36
add a comment |
1 Answer
1
active
oldest
votes
public class Test
private static FutureTask<Void> future;
public static Object do_method(Object request)
// Perform some operation on request
Object response = doSomeOperation(request);
// Want to do this task in background
future = new FutureTask<>(new Callable<Void>()
@Override
public Void call() throws Exception
writeToDB(request, response);
return null;
);
// Start background thread and execute Callable (FutureTask is also a runnable)
new Thread(future).start();
return response;
public static Object doSomeOperation(Object request)
return new Object();
public static void writeToDB(Object request, Object response) throws Exception
throw new Exception(); // Simulation failure on SQL insert
public static void main(String args)
Object request = new Object();
Object response = do_method(request);
// ... do some crazy stuff with response
try
future.get(); // Wait for succesfull execution of background thread
catch (Exception e)
// Catch ExecutionException if any uncaught Exception in the background thread is thrown
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%2f53316043%2fhow-to-run-background-task-which-is-guaranteed-to-be-completed-while-the-process%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
public class Test
private static FutureTask<Void> future;
public static Object do_method(Object request)
// Perform some operation on request
Object response = doSomeOperation(request);
// Want to do this task in background
future = new FutureTask<>(new Callable<Void>()
@Override
public Void call() throws Exception
writeToDB(request, response);
return null;
);
// Start background thread and execute Callable (FutureTask is also a runnable)
new Thread(future).start();
return response;
public static Object doSomeOperation(Object request)
return new Object();
public static void writeToDB(Object request, Object response) throws Exception
throw new Exception(); // Simulation failure on SQL insert
public static void main(String args)
Object request = new Object();
Object response = do_method(request);
// ... do some crazy stuff with response
try
future.get(); // Wait for succesfull execution of background thread
catch (Exception e)
// Catch ExecutionException if any uncaught Exception in the background thread is thrown
add a comment |
public class Test
private static FutureTask<Void> future;
public static Object do_method(Object request)
// Perform some operation on request
Object response = doSomeOperation(request);
// Want to do this task in background
future = new FutureTask<>(new Callable<Void>()
@Override
public Void call() throws Exception
writeToDB(request, response);
return null;
);
// Start background thread and execute Callable (FutureTask is also a runnable)
new Thread(future).start();
return response;
public static Object doSomeOperation(Object request)
return new Object();
public static void writeToDB(Object request, Object response) throws Exception
throw new Exception(); // Simulation failure on SQL insert
public static void main(String args)
Object request = new Object();
Object response = do_method(request);
// ... do some crazy stuff with response
try
future.get(); // Wait for succesfull execution of background thread
catch (Exception e)
// Catch ExecutionException if any uncaught Exception in the background thread is thrown
add a comment |
public class Test
private static FutureTask<Void> future;
public static Object do_method(Object request)
// Perform some operation on request
Object response = doSomeOperation(request);
// Want to do this task in background
future = new FutureTask<>(new Callable<Void>()
@Override
public Void call() throws Exception
writeToDB(request, response);
return null;
);
// Start background thread and execute Callable (FutureTask is also a runnable)
new Thread(future).start();
return response;
public static Object doSomeOperation(Object request)
return new Object();
public static void writeToDB(Object request, Object response) throws Exception
throw new Exception(); // Simulation failure on SQL insert
public static void main(String args)
Object request = new Object();
Object response = do_method(request);
// ... do some crazy stuff with response
try
future.get(); // Wait for succesfull execution of background thread
catch (Exception e)
// Catch ExecutionException if any uncaught Exception in the background thread is thrown
public class Test
private static FutureTask<Void> future;
public static Object do_method(Object request)
// Perform some operation on request
Object response = doSomeOperation(request);
// Want to do this task in background
future = new FutureTask<>(new Callable<Void>()
@Override
public Void call() throws Exception
writeToDB(request, response);
return null;
);
// Start background thread and execute Callable (FutureTask is also a runnable)
new Thread(future).start();
return response;
public static Object doSomeOperation(Object request)
return new Object();
public static void writeToDB(Object request, Object response) throws Exception
throw new Exception(); // Simulation failure on SQL insert
public static void main(String args)
Object request = new Object();
Object response = do_method(request);
// ... do some crazy stuff with response
try
future.get(); // Wait for succesfull execution of background thread
catch (Exception e)
// Catch ExecutionException if any uncaught Exception in the background thread is thrown
answered Nov 15 '18 at 10:07
MeiniMeini
414114
414114
add a comment |
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.
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%2f53316043%2fhow-to-run-background-task-which-is-guaranteed-to-be-completed-while-the-process%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
If you want to be sure the background task successfully completed you have to ask for that at some time. You could wrap
writeToDB(request, response);
in aCallable
and execute it in aFutureTask
orExecutorService
and invokefuture.get()
to provoke aExecutionException
– Meini
Nov 15 '18 at 9:29
Could you please explain me how to do the same
– Rutvik Shah
Nov 15 '18 at 9:36