Javatpoint Logo
Javatpoint Logo

Java Timer scheduleAtFixedRate() Method

The scheduleAtFixedRate(TimerTask task, long delay, long period) is the method of Timer class. It is used to schedule the given task again and again in fixed rate of execution. It will begin after the specified delay.

Syntax

Parameter

task: It is the task that is to be scheduled.

delay: It is the time at which a task is executed, it is in the millisecond.

period: It is the time between the successive task of execution, it is in milliseconds.

Throws

IllegalArgumentException - It throws the exception when the delay is less than zero or delay plus System.currentTimeMillis() less than zero or period is less than or equal to zero.

IllegalStateException - - It throws the exception when the task is scheduled or canceled, or when the timer is canceled, or when timer thread is terminated.

NullPointerException - - It throws the exception when task or time is null.

Example 1

Test it Now

Output:

Task Timer on Fixed Rate
Task Timer on Fixed Rate
Task Timer on Fixed Rate
Task Timer on Fixed Rate
Task Timer on Fixed Rate
.
.
.
.
.

Example 2

Test it Now

Output:

Task Timer on Fixed Rate
Task Timer on Fixed Rate
Task Timer on Fixed Rate
Task Timer on Fixed Rate
.
.
.
..

Java Timer scheduleAtFixedRate(TimerTask task,Date firstTime,long period) Method

The scheduleAtFixedRate (TimerTask task, Date firstTime, long period) is the method of Timer class. It is used to schedule the given task again and again in a fixed rate of execution. It began at a given time.

Syntax

Parameter

task - It is the task that is to be scheduled.

firstTime - It is the time at which a task is executed.

period - It is the time between the successive task of execution. It is in the milliseconds.

Throws

IllegalArgumentException - It throws the exception when the firstTime.getTime() is less than zero or period is less than or equal to zero.

IllegalStateException - It throws the exception when the task is scheduled or canceled, or when the timer is canceled, or when timer thread is terminated.

NullPointerException - It throws the exception when task or firstTime is null.

Example 3

Test it Now

Output:

Task Timer on Fixed Rate
Task Timer on Fixed Rate
Task Timer on Fixed Rate
Task Timer on Fixed Rate
Task Timer on Fixed Rate
Task Timer on Fixed Rate
.
.
.
.
.
h3 class="h3">Example 4
Test it Now

Output:

Task Timer on Fixed Rate
Task Timer on Fixed Rate
Task Timer on Fixed Rate
Task Timer on Fixed Rate
.
.
.
..

Next TopicJava Timer




Help Others, Please Share

facebook twitter pinterest