Skip to content

Instantly share code, notes, and snippets.

@googya
Created April 26, 2011 08:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save googya/941992 to your computer and use it in GitHub Desktop.
Save googya/941992 to your computer and use it in GitHub Desktop.
Java定时器timer 字符与数字计算的时候,自动转换成数字
package timer;
import java.io.IOException;
import java.util.Timer;
import java.util.TimerTask;
/**
* Created by IntelliJ IDEA.
* User: Hoodoo
* Date: 11-4-26
* Time: 下午2:21
* To change this template use File | Settings | File Templates.
*/
public class TimerTest {
public static void main(String[] args) {
Timer timer = new Timer();
timer.schedule(new MyTask(), 1000, 2000);
while (true) {
try {
int ch = System.in.read();
if (ch - 'c' == 0) {
if (timer != null) {
timer.cancel();
timer = null;
System.out.println("定时任务已经取消");
} else
System.out.println("任务已经取消了,不能再次取消;要启动请按'r'");
}
if (ch - 'r' == 0) {
if (timer == null) {
System.out.println("定时任务再次启动");
timer = new Timer();
timer.schedule(new MyTask(), 1000, 2000);
} else
System.out.println("定时任务已经启动了,不需要再次启动;要取消请按'c'");
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
class MyTask extends TimerTask {
public void run() {
System.out.println("*******************************@@@@@@@@*********************************");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment