How to implement alarm clock functionality to alarm sound at 7 am on week days and 10 am on weekends using java schedular
Steps to schedule a alarm clock during the week days or week ends
Step1. Write an abstract class wich implements Runnable and has the method to implement the logic to alarm sound.
package com.vels.test;
import java.util.TimerTask;
/**
* @author velmurugan pousel
* @project test
* created on Feb 1, 2011
*/
public abstract class SchedulerTask implements Runnable {
final Object lock = new Object();
int state = VIRGIN;
static final int VIRGIN = 0;
static final int SCHEDULED = 1;
static final int CANCELLED = 2;
TimerTask timerTask;
protected SchedulerTask() {
}
public abstract void run();
public boolean cancel() {
synchronized(lock) {
if (timerTask != null) {
timerTask.cancel();
}
boolean result = (state == SCHEDULED);
state = CANCELLED;
return result;
}
}
public long scheduledExecutionTime() {
synchronized(lock) {
return timerTask == null ? 0 : timerTask.scheduledExecutionTime();
}
}
Steps to schedule a alarm clock during the week days or week ends
Step1. Write an abstract class wich implements Runnable and has the method to implement the logic to alarm sound.
package com.vels.test;
import java.util.TimerTask;
/**
* @author velmurugan pousel
* @project test
* created on Feb 1, 2011
*/
public abstract class SchedulerTask implements Runnable {
final Object lock = new Object();
int state = VIRGIN;
static final int VIRGIN = 0;
static final int SCHEDULED = 1;
static final int CANCELLED = 2;
TimerTask timerTask;
protected SchedulerTask() {
}
public abstract void run();
public boolean cancel() {
synchronized(lock) {
if (timerTask != null) {
timerTask.cancel();
}
boolean result = (state == SCHEDULED);
state = CANCELLED;
return result;
}
}
public long scheduledExecutionTime() {
synchronized(lock) {
return timerTask == null ? 0 : timerTask.scheduledExecutionTime();
}
}
Wake up! It's 02 Mar 2011 12:48:00.011
.....
Reference : http://www.ibm.com/
http://peruserpaid.com/?ref=142327
.....
Reference : http://www.ibm.com/
http://peruserpaid.com/?ref=142327
No comments:
Post a Comment