Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

TimerTask change period at runtime

Posted on 2003-03-27
6
Medium Priority
?
1,600 Views
Last Modified: 2012-08-14
Is there a simple possibility to change the period of TimerTask at runtime?
I know that it can certainly be realised using a thread (sleep).
First I made it with threads and everything worked fine in the
Appletviewer. But when I tested it in a browser the applet was always
stopped with a messag from Security Manager that I have to modify thread and so on.
I tried everything in order to make it run with threads (Priority, ...) but it didn't work.
That's why I want it to do with TimerTask this time and it works so far...
0
Comment
Question by:borg81
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 92

Expert Comment

by:objects
ID: 8221470
There is no way to directly change the period of a task.
What you would do is cancel the task and start a new one with the desired period.
0
 

Expert Comment

by:keithfry
ID: 8222597
The sad thing with Timer is that you cannot use the same TimerTask object. Below are two code snippets from the implementation of Timer.java & TimerTask.java (from JDK 1.3). When you call TimerTask.cancel(), you do indeed cancel the TimerTask. However you cannot use that task in any subsequent schedule operations as Timer.sched() will throw an IllegalStateException when the TimerTask is not a virgin. So if you take this approach, make sure that you create a NEW TimerTask object to set the period.

IMHO the Timer/TimerTask classes should be changed in the future to support:
* Timer: listing scheduled tasks by priority.
* Both: cooperate to allow cancelled tasks to be rescheduled.
* Both: allow adjusting period dynamically. This may include options for allowing the next scheduled run to occur or be pre-empted.

borg81: if you want to take a stab at it, you can always get a copy of the Java source, apply these changes as you see fit, TEST TEST TEST, and then triumphantly return with your improved Timer class. :-)



-----------
Timer.java
-----------
    private void sched(TimerTask task, long time, long period) {
        if (time < 0)
            throw new IllegalArgumentException("Illegal execution time.");
       
        synchronized(queue) {
            if (!thread.newTasksMayBeScheduled)
                throw new IllegalStateException("Timer already cancelled.");

            synchronized(task.lock) {
                if (task.state != TimerTask.VIRGIN)
                    throw new IllegalStateException(
                        "Task already scheduled or cancelled");
                task.nextExecutionTime = time;
                task.period = period;
                task.state = TimerTask.SCHEDULED;
            }

            queue.add(task);
            if (queue.getMin() == task)
                queue.notify();
        }
    }
-------------
TimerTask.java
-------------
    public boolean cancel() {
        synchronized(lock) {
            boolean result = (state == SCHEDULED);
            state = CANCELLED;
            return result;
        }
    }
--------------
0
 
LVL 92

Accepted Solution

by:
objects earned 60 total points
ID: 8222621
> make sure that you create a NEW TimerTask object to set the period.

Is that such a big drama?
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

by:borg81
ID: 8222909
Thousand Thanks
0
 
LVL 92

Expert Comment

by:objects
ID: 8222918
no probs.
0
 

Expert Comment

by:keithfry
ID: 8225233
No drama, I just ran into that situation once and was confused with the error message.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

721 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question