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 6220453
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T07:57:33+00:00 2026-05-24T07:57:33+00:00

I have written the following code: import java.util.Calendar; import java.util.concurrent.ScheduledThreadPoolExecutor; import java.util.concurrent.TimeUnit; class Voter

  • 0

I have written the following code:

import java.util.Calendar;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

class Voter {   
public static void main(String[] args) {
    ScheduledThreadPoolExecutor stpe = new ScheduledThreadPoolExecutor(2);
    stpe.scheduleAtFixedRate(new Shoot(), 0, 1, TimeUnit.SECONDS);
}
}

class Shoot implements Runnable {
Calendar deadline;
long endTime,currentTime;

public Shoot() {
    deadline = Calendar.getInstance();
    deadline.set(2011,6,21,12,18,00);
    endTime = deadline.getTime().getTime();
}

public void work() {
    currentTime = System.currentTimeMillis();

    if (currentTime >= endTime) {
        System.out.println("Got it!");
        func();
    } 
}

public void run() {
    work();
}

public void func() {
    // function called when time matches
}
}

I would like to stop the ScheduledThreadPoolExecutor when the func() is called. There is no need for it to work futher! I think i should put the function func() inside Voter class, and than create some kind of callback. But maybe I can do it from within the Shoot class.

How can I solve it properly?

  • 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-05-24T07:57:33+00:00Added an answer on May 24, 2026 at 7:57 am

    The ScheduledThreadPoolExecutor allows you to execute your task right away or schedule it to be executed later (you can set periodical executions also).

    So, if you will use this class to stop your task execution keep in mind this:

    1. There is no way to guarantee that one thread will stop his execution. Check the Thread.interrupt() documentation.
    2. The method ScheduledThreadPoolExecutor.shutdown() will set as canceled your task, and it will not try to interrupt your threads. With this method you actually avoid the execution of newer tasks, and the execution of scheduled but not started tasks.
    3. The method ScheduledThreadPoolExecutor.shutdownNow() will interrupt the threads, but as I said in the first point of this list …

    When you want to stop the scheduler you must do something like this:

        //Cancel scheduled but not started task, and avoid new ones
        myScheduler.shutdown();
    
        //Wait for the running tasks 
        myScheduler.awaitTermination(30, TimeUnit.SECONDS);
    
        //Interrupt the threads and shutdown the scheduler
        myScheduler.shutdownNow();
    

    But what if you need to stop only one task?

    The method ScheduledThreadPoolExecutor.schedule(...) returns a ScheduleFuture that is a representation of your already scheduled task. So, you can call the ScheduleFuture.cancel(boolean mayInterruptIfRunning) method to cancel your task and try to interrupt it if you need to.

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

Sidebar

Related Questions

I have the following Java code: import java.util.Arrays; import java.util.Collections; public class Test {
I have written the following IronPython code: import clr clr.AddReference(System.Drawing) from System import *
In physics library written in C# I have the following code: (in ContactManager.cs) public
I have the following in a program (written in VB.NET): Imports Microsoft.Office.Interop.Excel Public Class
I have written a util class in Java for webservice call. My util class
I have written following code for the client of RMI. But getting java.rmi.ConnectException: Connection
I have written the following Python code: #!/usr/bin/python # -*- coding: utf-8 -*- import
I have written a following code to get just the file name without extension
I have written the following code choice /m Do you want to add another
I have written the following code. But it is removing only &nbsp; not <br>

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.