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

  • SEARCH
  • Home
  • 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 8773207
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T18:14:07+00:00 2026-06-13T18:14:07+00:00

I use ScheduledExecutorService and I want it to do some calculation every 10 seconds

  • 0

I use ScheduledExecutorService and I want it to do some calculation every 10 seconds for a minute and after that minute to return me the new value.How do I do that?

Example:
So it receives 5 it adds +1 six times then it should return me after a minute the value of 11.

What i have so far but is not working is:

package com.example.TaxiCabs;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import static java.util.concurrent.TimeUnit.*;


public class WorkingWithTimeActivity {
public int myNr;
public WorkingWithTimeActivity(int nr){
    myNr = nr;
}
private final ScheduledExecutorService scheduler =
        Executors.newScheduledThreadPool(1);

public int doMathForAMinute() {
    final Runnable math = new Runnable() {
        public void run() {
            myNr++;
        }
    };
    final ScheduledFuture<?> mathHandle =
            scheduler.scheduleAtFixedRate(math, 10, 10, SECONDS);
    scheduler.schedule(
            new Runnable() {
                public void run() {
                    mathHandle.cancel(true);
                }
            }, 60, SECONDS);
    return myNr;
}

}

and in my main activity and i want it after 1 minute to change my txtview text into 11;

WorkingWithTimeActivity test = new WorkingWithTimeActivity(5);
txtview.setText(String.valueOf(test.doMathForAMinute()));
  • 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-13T18:14:08+00:00Added an answer on June 13, 2026 at 6:14 pm

    You should use Callable which can return value rather than Runnable

    The Callable interface is similar to Runnable, in that both are designed for classes whose instances are potentially executed by another thread. A Runnable, however, does not return a result and cannot throw a checked exception.

    public class ScheduledPrinter implements Callable<String> {
        public String call() throws Exception {
            return "somethhing";
        }
    }
    

    Then use it like below

        ScheduledExecutorService scheduler = Executors
                .newScheduledThreadPool(1);
        ScheduledFuture<String> future = scheduler.schedule(
                new ScheduledPrinter(), 10, TimeUnit.SECONDS);
        System.out.println(future.get());
    

    This is one shot schedule so it will execute only one time you will need to schedule it again once the get call is returned.


    However in your case it will be easy to use a simple AtomicInteger and call addAndGet compare the returned value once your condition arrive cancel the scheduling by calling cancel.

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

Sidebar

Related Questions

I use ScheduledExecutorService to schedule some tasks which need to run periodically. I want
use case example I have a servlet that is receiving login requests. If a
use case is simple: I want to run some boiler plate code before each
I use ScheduledExecutorService in my application. I need to use it from time to
use C#,want to upload excel file on google doc. bellow syntax use to upload
use Text::Table; my $tb = Text::Table->new(Planet,Radius\nkm,Density\ng/cm^3); $tb->load( [ Mercury,2360,3.7], [ Mercury,2360,3.7], [ Mercury,2360,3.7], );
use Modern::Perl; use Algorithm::Permute; use List::AllUtils qw/uniq/; find_perms(1151); sub find_perms { my ($value) =
AKA doing something at set intervals. For example, let's say I want to scan
Is it possible to use ScheduledExecutorService with a webdyno or heroku shutdown the java
When I use a ScheduledExecutor with a TimeUnit.MINUTES and value 3, the job gets

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.