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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T01:29:43+00:00 2026-06-01T01:29:43+00:00

The Timer ( java.util.Timer ) doc describes the cancel method as one that affects

  • 0

The Timer (java.util.Timer) doc describes the cancel method as one that affects the Timer and it states that the timer cannot be used after cancellation. So I instantiate a new Timer. Why will it not let me re-use the argument task0 in this example? I’m not even invoking purge which is described as making tasks GC-eligible. Until it might be explained to be otherwise, I claim Timer class should not affect a TimerTask object that is merely an argument to it.

import java.util.Timer;
import java.util.TimerTask;

public class Tester {

    public static void main(String[] args) throws InterruptedException {
        long delay = 3000L;

        Timer timer0 = new Timer();
        Task task0 = new Task();
        timer0.schedule(task0, delay);
        timer0.cancel();

        Timer timer1 = new Timer();
        timer1.schedule(task0, delay); // throws an exception if we use task0

        Thread.sleep(5000);
        timer1.cancel();
    }
}

class Task extends TimerTask {
    Task() {
    }
    @Override
    public void run() {
        System.out.println("task was invoked");
    }
}
  • 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-01T01:29:45+00:00Added an answer on June 1, 2026 at 1:29 am

    Allowing this would be error prone, since task0 could still be running when scheduled again by another timer. (Note that cancel() does not terminate the task.)

    Note that if task0 is managed by a single Timer, the same task will never be executed concurrently with itself (regardless if it is executed with fixed-delay or with fixed-rate).

    If you really want such behavior, the work around would be to let task0 and a task1 wrap a common object:

    class Task extends TimerTask {
        Runnable runnable;
        Task(Runnable runnable) {
            this.runnable = runnable;
        }
        @Override
        public void run() {
            runnable.run();
        }
    }
    

    And then execute it like this:

    // "Wrapped" (and thus shared) by task0 and task1 below.
    Runnable runnable = new Runnable() {
        @Override
        public void run() {
            System.out.println("task was invoked");
        }
    }
    
    
    
    Timer timer0 = new Timer();
    Task task0 = new Task(runnable);
    timer0.schedule(task0, delay);
    timer0.cancel();
    
    
    Task task1 = new Task(runnable);
    Timer timer1 = new Timer();
    timer1.schedule(task1, delay); // throws an exception if we use task0
    
    Thread.sleep(5000);
    timer1.cancel();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using java.util.Timer class and I am using its schedule method to perform
I want to execute a job everyday 2PM. Which method of java.util.Timer I can
I'm using java.util.Timer to schedule a periodic task. At one point, I'd like to
Comparing javax.swing.Timer , java.util.Timer and Thread , which method is the best CPU utilization
I use java.util.Timer to trigger jobs in my app, but I found that it
http://docs.oracle.com/javase/1.5.0/docs/api/java/util/Timer.html The reason for this question is that I have a timer running at
I tried everything. This one too How to stop the task scheduled in Java.util.Timer
I am using void java.util.Timer.scheduleAtFixedRate(TimerTask task, long delay, long period) to execute a task
I have code where I schedule a task using java.util.Timer . I was looking
i have a question , i have this timer code in java that when

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.