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;








-1















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.










share|improve this question






















  • 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

















-1















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.










share|improve this question






















  • 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













-1












-1








-1








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.










share|improve this question














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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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 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

















  • 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
















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












1 Answer
1






active

oldest

votes


















0














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








share|improve this answer























    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
    );



    );













    draft saved

    draft discarded


















    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









    0














    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








    share|improve this answer



























      0














      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








      share|improve this answer

























        0












        0








        0







        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








        share|improve this answer













        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









        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 15 '18 at 10:07









        MeiniMeini

        414114




        414114





























            draft saved

            draft discarded
















































            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.




            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            Use pre created SQLite database for Android project in kotlin

            Darth Vader #20

            Ondo