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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T04:38:08+00:00 2026-06-09T04:38:08+00:00

I am trying to use a single function in a class that will start

  • 0

I am trying to use a single function in a class that will start and stop timers which are linked to int values using a boolean. So for example if I started a timer with a int of 0 then that would be timer0 and if it was 3 then timer3 and so on.

The problem I am having is that the timers seem to start ok but when I send them a false bool to stop them they will keep running so I need to know how I can stop them correctly.

In the Class.java the code is:

public void Event(final int value, boolean run, int time){

    if(run){
        System.out.println(run);

        Timer timer = new Timer();

        timer.schedule( new TimerTask() {
            public void run() {
                // The needed code will go here
                System.out.println(value + " Event run");
            }
         }, 0, time); // Every second
    } else {
    }

} 

Then for my Main.java the code is:

System.out.println("Start Timer 0 Event");
r.Event(0, true, 1000);

System.out.println("Start Timer 1 Event");
r.Event(1, true, 250);

System.out.println("Start Timer 2 Event");
r.Event(2, true, 250);

r.Event(0, false, 1000); // Not Working as i need
System.out.println("Stop Timer 0 Event");

Basically I just want to have sets of events get repeated every set amount amount of time until I stop them and there could be many run together. If timers are not the best way to do this then a alternative would be fine but it would need to work the same way as described however.


On request here is the runnable code for my timer.

MyClass.java:

package com.z;

import java.awt.*;
import java.util.*;
import java.util.TimerTask;

public class MyClass {

////////////////////////////////////////////////////////////
//Name: Event (BROKEN)
////////////////////////////////////////////////////////////
public void Event(final int value, boolean run, int time){

    Timer timer = new Timer("" + value, true);

    if(run){
        System.out.println(run);

        timer.schedule( new TimerTask() {
            public void run() {
                // Code here
                System.out.println(value + " Event run");
            }
         }, 0, time); // Every second
    } 

    if (!run) {
        timer.cancel();
    }
}

}

Example.java:

package com.z;

import java.awt.*;
import java.awt.event.*;

public class Example {

    public static void main(String[] args) {

    MyClass r = new MyClass();

    ////////////////////////////////////////////////////////////
    // Event (BROKEN)
    ////////////////////////////////////////////////////////////
    System.out.println("Start Timer 0 Event");
    r.Event(0, true, 1000);

    System.out.println("Start Timer 1 Event");
    r.Event(1, true, 250);

    r.Event(0, false, 1000);
    System.out.println("Stop Timer 0 Event");

    }
}
  • 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-09T04:38:10+00:00Added an answer on June 9, 2026 at 4:38 am

    The timer.schedule methods that take 3 args repeat execution, so if run is true, the timer will start executing tasks.

    If you want to stop the timer, you can always call timer.cancel, but you need to save a reference to the timer outside the Event method.

    Reading the Timer javadocs should help here http://docs.oracle.com/javase/6/docs/api/java/util/Timer.html

    EDIT: Here is an example of how this might work

    Timer startTimer(final int value, final long time) {
       Timer timer = new Timer("Timer" + value);
       timer.schedule( new TimerTask() {
                public void run() {
                    // Code here
                    System.out.println(value + " Event run");
                }
             }, 0, time); // Every second
       return timer;
    }
    
    Timer t0 = startTimer(0,1000);
    Timer t1 = startTimer(1,1000);
    
    // stop t0
    t0.cancel();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to find a single regular expression that I can use to parse
I am trying to code a class that makes use of Push and Pop
I am trying to use a single RouteURL to route to different pages dependent
I'm trying to replace multiple spaces with a single space. When I use ereg_replace
I have a single .jar file that I create by using the runnable .jar
I am trying to make a jquery similar tabs. since its for single use
I'm trying to single source a form page which can be in edit mode
I am trying to write a script that will submit all the individual forms
I'm writing something that will examine a function and rewrite that function in another
I am trying to create a function with a variant record-type parameter that allows

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.