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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T21:25:30+00:00 2026-06-14T21:25:30+00:00

I have two timers to manage input(en-queue) and output (dequeue) from a FIFO queue

  • 0

I have two timers to manage input(en-queue) and output (dequeue) from a FIFO queue but I keep getting a exception for the dequeueing java.lang.IllegalStateException: Timer already cancelled. I can’t place a stop to debug line where the error is claimed to occur line 83. I don’t know what I’m missing so any help would be appreciated.

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

/**
 * RunSim
 */
public class RunSim {
    private double arrivalRate = 600;
    private double y;
    private Timer t;
    private Timer t2;
    private Queue fifoQueue;
    private long xy;
    private long fact = 10;
    private int count;
    private int pId;

    public RunSim() {
        Random r = new Random();
        long n = System.currentTimeMillis();
        r.setSeed(n);
        double i = r.nextDouble();
        y = ((1 / arrivalRate) * (Math.log(i)));
        xy = (long) y;
        t = new Timer();
        t2 = new Timer();
        fifoQueue = new Queue();
        count = 0;
        pId = 0;

    }

    public static void main() {
        RunSim rs = new RunSim();
        rs.start();
    }

    public void start() {
        class sendPacket extends TimerTask {
            public void run() {
                Packet p = new Packet();
                p.setId(pId);
                fifoQueue.insert(p);
                p.setArrivalTime();
                System.out.println("ID: " + p.getId() + " Arrival Time: "
                        + p.getArrivalTime() / fact);
                pId++;

            }
        }

        class removePacket extends TimerTask {
            public void run() {
                fifoQueue.first().setDepartureTime();
                System.out.println("ID: " + fifoQueue.first().getId()
                        + " Departure Time: "
                        + fifoQueue.first().getDepartureTime() / fact);
                fifoQueue.remove();
            }
        }

        while (count < 1000) {
            long v = fact * (1 + Math.abs(xy));
            t.schedule(new sendPacket(), 0, v);
            count++;
            t2.schedule(new removePacket(), 5, 5);

        }
    }
}
  • 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-14T21:25:32+00:00Added an answer on June 14, 2026 at 9:25 pm

    Immediately after scheduling all the timers, you cancel them. This doesn’t work like the ExecutorService where you can schedule all you need and then call shutdown—this actually cancels the timer and all scheduled tasks.

    Another problem with your code is that you call System.exit right away, not giving any chance to the scheduled tasks to actually run.

    Apart from those problems, you may get a Timer already canceled exception if a previous task threw an exception. The exception will not be seen anywhere, but it will cancel the timer. Be sure to wrap your timer tasks into a catch-all try-statement.

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

Sidebar

Related Questions

I am trying to get a timer to count two times but I have
I have two classes (MVC view model) which inherits from one abstract base class.
I have two virtual machines with seemingly identical configuration (created from the same VM
I have to define a function called zeros which takes input of two lists
I have a Qt application that launches two threads from the main thread at
I have two times in PHP and I would like to determine the elapsed
I want to compare two times in VB.net: I have 1:42:21 PM and I
In above figure the element Chandru is repeated as two times. so i have
Currently I have a timestamp for example say, 2012-06-23 14:24:07.975 and two times -
I have an application in which I am starting a timer for two task:

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.