java.util.Timer sample
Countdown을 설정하는 예제
Countdown.java
import java.util.Timer;
import java.util.TimerTask;
public class Countdown {
public static void main(final String args[]) {
if (args.length != 1) {
System.err.println("Usage: java Countdown <time in secs>");
System.exit(1);
}
final Timer timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
int i = Integer.parseInt(args[0]);
public void run() {
System.out.println(i--);
if (i< 0)
timer.cancel();
}
}, 0, 1000);
}
}
C:\javaExample\util>javac Countdown.java
C:\javaExample\util>java Countdown 3
3
2
1
0
=========================================================
cancel()을 이용해 Timer 스레드 종료하기
TimerTest.java
import java.util.Timer;
import java.util.TimerTask;
public class TimerTest {
Timer timer;
public TimerTest(int seconds) {
timer = new Timer();
timer.schedule(new TimerTaskTest(), seconds*1000);
}
class TimerTaskTest extends TimerTask {
public void run() {
System.out.println("Time's up!");
timer.cancel(); //timer thread 종료
}
}
public static void main(String args[]) {
System.out.println("About to schedule task.");
new TimerTest(3);
System.out.println("Task scheduled.");
}
}
C:\javaExample\util>javac TimerTest.java
C:\javaExample\util>java TimerTest
About to schedule task.
Task scheduled.
Time's up!
프로그램을 실행시키면 3초 후에 자동으로 정지된다.
=========================================================
System.exit()을 이용해 Timer 스레드 종료하기 2
TimerTest1.java
import java.util.Timer;
import java.util.TimerTask;
import java.awt.Toolkit;
public class TimerTest1 {
Toolkit toolkit;
Timer timer;
public TimerTest1(int seconds) {
toolkit = Toolkit.getDefaultToolkit();
timer = new Timer();
timer.schedule(new TimerTaskTest1(), seconds*1000);
}
class TimerTaskTest1 extends TimerTask {
public void run() {
System.out.println("Time's up!");
toolkit.beep();
//timer.cancel();
//System.exit()을 불러오기때문에에 필요없음
System.exit(0); //AWT thread 정지
}
}
public static void main(String args[]) {
System.out.println("About to schedule task.");
new TimerTest1(3);
System.out.println("Task scheduled.");
}
}
C:\javaExample\util>javac TimerTest1.java
C:\javaExample\util>java TimerTest1
About to schedule task.
Task scheduled.
Time's up!
프로그램을 실행시키면 3초 후에 소리를 내며 종료된다.
from:
http://ddoobb.com/programing/java.jsp
Countdown.java
import java.util.Timer;
import java.util.TimerTask;
public class Countdown {
public static void main(final String args[]) {
if (args.length != 1) {
System.err.println("Usage: java Countdown <time in secs>");
System.exit(1);
}
final Timer timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
int i = Integer.parseInt(args[0]);
public void run() {
System.out.println(i--);
if (i< 0)
timer.cancel();
}
}, 0, 1000);
}
}
C:\javaExample\util>javac Countdown.java
C:\javaExample\util>java Countdown 3
3
2
1
0
=========================================================
cancel()을 이용해 Timer 스레드 종료하기
TimerTest.java
import java.util.Timer;
import java.util.TimerTask;
public class TimerTest {
Timer timer;
public TimerTest(int seconds) {
timer = new Timer();
timer.schedule(new TimerTaskTest(), seconds*1000);
}
class TimerTaskTest extends TimerTask {
public void run() {
System.out.println("Time's up!");
timer.cancel(); //timer thread 종료
}
}
public static void main(String args[]) {
System.out.println("About to schedule task.");
new TimerTest(3);
System.out.println("Task scheduled.");
}
}
C:\javaExample\util>javac TimerTest.java
C:\javaExample\util>java TimerTest
About to schedule task.
Task scheduled.
Time's up!
프로그램을 실행시키면 3초 후에 자동으로 정지된다.
=========================================================
System.exit()을 이용해 Timer 스레드 종료하기 2
TimerTest1.java
import java.util.Timer;
import java.util.TimerTask;
import java.awt.Toolkit;
public class TimerTest1 {
Toolkit toolkit;
Timer timer;
public TimerTest1(int seconds) {
toolkit = Toolkit.getDefaultToolkit();
timer = new Timer();
timer.schedule(new TimerTaskTest1(), seconds*1000);
}
class TimerTaskTest1 extends TimerTask {
public void run() {
System.out.println("Time's up!");
toolkit.beep();
//timer.cancel();
//System.exit()을 불러오기때문에에 필요없음
System.exit(0); //AWT thread 정지
}
}
public static void main(String args[]) {
System.out.println("About to schedule task.");
new TimerTest1(3);
System.out.println("Task scheduled.");
}
}
C:\javaExample\util>javac TimerTest1.java
C:\javaExample\util>java TimerTest1
About to schedule task.
Task scheduled.
Time's up!
프로그램을 실행시키면 3초 후에 소리를 내며 종료된다.
from:
http://ddoobb.com/programing/java.jsp