How to run tasks at a scheduled rate that DON'T wait for the task before it?












1















Currently, I have some code that I need to run every (for example) 33 milliseconds. However, the operation that I am calling requires around 270ms. Is there a way to schedule my tasks so that they run regardless of the task before them?



I have tried implementing a ScheduledExecutorService variable and running the task at a "ScheduledFixedRate" but that currently waits for the task before it.



Runnable imageCapture = new Runnable() {

public void run() {
// code that takes approximately 270ms
}
};

executor = Executors.newScheduledThreadPool(4);
executor.scheduleAtFixedRate(imageCapture, 0, 33, TimeUnit.MILLISECONDS);









share|improve this question























  • If you want it to run that frequently, you'll need more threads.

    – Andy Turner
    Dec 31 '18 at 12:40











  • Is there like a formula to use, I kind of just guessed on the number. Do you think adding more threads will solve my issue? @AndyTurner

    – Tyler
    Dec 31 '18 at 12:45








  • 1





    Multiply the time for one action to complete by the frequency. Equivalently, divide the time for one action by the time between two actions starting. 270/33=8 and a bit. So you'd need at least 9 threads.

    – Andy Turner
    Dec 31 '18 at 12:54











  • But that won't work with the code you have anyway, since the javadoc says "If any execution of this task takes longer than its period, then subsequent executions may start late, but will not concurrently execute (emphasis mine).

    – JB Nizet
    Dec 31 '18 at 13:05











  • @AndyTurner not only 9 threads, but also 9 processors (cores).

    – Alexei Kaigorodov
    Dec 31 '18 at 19:47
















1















Currently, I have some code that I need to run every (for example) 33 milliseconds. However, the operation that I am calling requires around 270ms. Is there a way to schedule my tasks so that they run regardless of the task before them?



I have tried implementing a ScheduledExecutorService variable and running the task at a "ScheduledFixedRate" but that currently waits for the task before it.



Runnable imageCapture = new Runnable() {

public void run() {
// code that takes approximately 270ms
}
};

executor = Executors.newScheduledThreadPool(4);
executor.scheduleAtFixedRate(imageCapture, 0, 33, TimeUnit.MILLISECONDS);









share|improve this question























  • If you want it to run that frequently, you'll need more threads.

    – Andy Turner
    Dec 31 '18 at 12:40











  • Is there like a formula to use, I kind of just guessed on the number. Do you think adding more threads will solve my issue? @AndyTurner

    – Tyler
    Dec 31 '18 at 12:45








  • 1





    Multiply the time for one action to complete by the frequency. Equivalently, divide the time for one action by the time between two actions starting. 270/33=8 and a bit. So you'd need at least 9 threads.

    – Andy Turner
    Dec 31 '18 at 12:54











  • But that won't work with the code you have anyway, since the javadoc says "If any execution of this task takes longer than its period, then subsequent executions may start late, but will not concurrently execute (emphasis mine).

    – JB Nizet
    Dec 31 '18 at 13:05











  • @AndyTurner not only 9 threads, but also 9 processors (cores).

    – Alexei Kaigorodov
    Dec 31 '18 at 19:47














1












1








1








Currently, I have some code that I need to run every (for example) 33 milliseconds. However, the operation that I am calling requires around 270ms. Is there a way to schedule my tasks so that they run regardless of the task before them?



I have tried implementing a ScheduledExecutorService variable and running the task at a "ScheduledFixedRate" but that currently waits for the task before it.



Runnable imageCapture = new Runnable() {

public void run() {
// code that takes approximately 270ms
}
};

executor = Executors.newScheduledThreadPool(4);
executor.scheduleAtFixedRate(imageCapture, 0, 33, TimeUnit.MILLISECONDS);









share|improve this question














Currently, I have some code that I need to run every (for example) 33 milliseconds. However, the operation that I am calling requires around 270ms. Is there a way to schedule my tasks so that they run regardless of the task before them?



I have tried implementing a ScheduledExecutorService variable and running the task at a "ScheduledFixedRate" but that currently waits for the task before it.



Runnable imageCapture = new Runnable() {

public void run() {
// code that takes approximately 270ms
}
};

executor = Executors.newScheduledThreadPool(4);
executor.scheduleAtFixedRate(imageCapture, 0, 33, TimeUnit.MILLISECONDS);






java concurrency scheduledexecutorservice






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Dec 31 '18 at 12:37









TylerTyler

133




133













  • If you want it to run that frequently, you'll need more threads.

    – Andy Turner
    Dec 31 '18 at 12:40











  • Is there like a formula to use, I kind of just guessed on the number. Do you think adding more threads will solve my issue? @AndyTurner

    – Tyler
    Dec 31 '18 at 12:45








  • 1





    Multiply the time for one action to complete by the frequency. Equivalently, divide the time for one action by the time between two actions starting. 270/33=8 and a bit. So you'd need at least 9 threads.

    – Andy Turner
    Dec 31 '18 at 12:54











  • But that won't work with the code you have anyway, since the javadoc says "If any execution of this task takes longer than its period, then subsequent executions may start late, but will not concurrently execute (emphasis mine).

    – JB Nizet
    Dec 31 '18 at 13:05











  • @AndyTurner not only 9 threads, but also 9 processors (cores).

    – Alexei Kaigorodov
    Dec 31 '18 at 19:47



















  • If you want it to run that frequently, you'll need more threads.

    – Andy Turner
    Dec 31 '18 at 12:40











  • Is there like a formula to use, I kind of just guessed on the number. Do you think adding more threads will solve my issue? @AndyTurner

    – Tyler
    Dec 31 '18 at 12:45








  • 1





    Multiply the time for one action to complete by the frequency. Equivalently, divide the time for one action by the time between two actions starting. 270/33=8 and a bit. So you'd need at least 9 threads.

    – Andy Turner
    Dec 31 '18 at 12:54











  • But that won't work with the code you have anyway, since the javadoc says "If any execution of this task takes longer than its period, then subsequent executions may start late, but will not concurrently execute (emphasis mine).

    – JB Nizet
    Dec 31 '18 at 13:05











  • @AndyTurner not only 9 threads, but also 9 processors (cores).

    – Alexei Kaigorodov
    Dec 31 '18 at 19:47

















If you want it to run that frequently, you'll need more threads.

– Andy Turner
Dec 31 '18 at 12:40





If you want it to run that frequently, you'll need more threads.

– Andy Turner
Dec 31 '18 at 12:40













Is there like a formula to use, I kind of just guessed on the number. Do you think adding more threads will solve my issue? @AndyTurner

– Tyler
Dec 31 '18 at 12:45







Is there like a formula to use, I kind of just guessed on the number. Do you think adding more threads will solve my issue? @AndyTurner

– Tyler
Dec 31 '18 at 12:45






1




1





Multiply the time for one action to complete by the frequency. Equivalently, divide the time for one action by the time between two actions starting. 270/33=8 and a bit. So you'd need at least 9 threads.

– Andy Turner
Dec 31 '18 at 12:54





Multiply the time for one action to complete by the frequency. Equivalently, divide the time for one action by the time between two actions starting. 270/33=8 and a bit. So you'd need at least 9 threads.

– Andy Turner
Dec 31 '18 at 12:54













But that won't work with the code you have anyway, since the javadoc says "If any execution of this task takes longer than its period, then subsequent executions may start late, but will not concurrently execute (emphasis mine).

– JB Nizet
Dec 31 '18 at 13:05





But that won't work with the code you have anyway, since the javadoc says "If any execution of this task takes longer than its period, then subsequent executions may start late, but will not concurrently execute (emphasis mine).

– JB Nizet
Dec 31 '18 at 13:05













@AndyTurner not only 9 threads, but also 9 processors (cores).

– Alexei Kaigorodov
Dec 31 '18 at 19:47





@AndyTurner not only 9 threads, but also 9 processors (cores).

– Alexei Kaigorodov
Dec 31 '18 at 19:47












1 Answer
1






active

oldest

votes


















0














Split the task in two: one makes actual computations and another is executed periodically and starts the first one:



executor = Executors.newScheduledThreadPool(4);

Runnable imageCapture = new Runnable() {

public void run() {
// code that takes approximately 270ms
}
};

Runnable launcher = new Runnable() {

public void run() {
executor.execute(imageCapture);
}
};

executor.scheduleAtFixedRate(launcher, 0, 33, TimeUnit.MILLISECONDS);





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%2f53987629%2fhow-to-run-tasks-at-a-scheduled-rate-that-dont-wait-for-the-task-before-it%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














    Split the task in two: one makes actual computations and another is executed periodically and starts the first one:



    executor = Executors.newScheduledThreadPool(4);

    Runnable imageCapture = new Runnable() {

    public void run() {
    // code that takes approximately 270ms
    }
    };

    Runnable launcher = new Runnable() {

    public void run() {
    executor.execute(imageCapture);
    }
    };

    executor.scheduleAtFixedRate(launcher, 0, 33, TimeUnit.MILLISECONDS);





    share|improve this answer






























      0














      Split the task in two: one makes actual computations and another is executed periodically and starts the first one:



      executor = Executors.newScheduledThreadPool(4);

      Runnable imageCapture = new Runnable() {

      public void run() {
      // code that takes approximately 270ms
      }
      };

      Runnable launcher = new Runnable() {

      public void run() {
      executor.execute(imageCapture);
      }
      };

      executor.scheduleAtFixedRate(launcher, 0, 33, TimeUnit.MILLISECONDS);





      share|improve this answer




























        0












        0








        0







        Split the task in two: one makes actual computations and another is executed periodically and starts the first one:



        executor = Executors.newScheduledThreadPool(4);

        Runnable imageCapture = new Runnable() {

        public void run() {
        // code that takes approximately 270ms
        }
        };

        Runnable launcher = new Runnable() {

        public void run() {
        executor.execute(imageCapture);
        }
        };

        executor.scheduleAtFixedRate(launcher, 0, 33, TimeUnit.MILLISECONDS);





        share|improve this answer















        Split the task in two: one makes actual computations and another is executed periodically and starts the first one:



        executor = Executors.newScheduledThreadPool(4);

        Runnable imageCapture = new Runnable() {

        public void run() {
        // code that takes approximately 270ms
        }
        };

        Runnable launcher = new Runnable() {

        public void run() {
        executor.execute(imageCapture);
        }
        };

        executor.scheduleAtFixedRate(launcher, 0, 33, TimeUnit.MILLISECONDS);






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Dec 31 '18 at 19:44

























        answered Dec 31 '18 at 15:14









        Alexei KaigorodovAlexei Kaigorodov

        10.1k11029




        10.1k11029
































            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%2f53987629%2fhow-to-run-tasks-at-a-scheduled-rate-that-dont-wait-for-the-task-before-it%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

            Monofisismo

            Angular Downloading a file using contenturl with Basic Authentication

            Olmecas