'이클립스'에 해당되는 글 17건
- 2017.09.08 개발자 101 가이드
- 2009.08.04 이클립스 코드 템플릿
- 2009.08.03 Maven 을 이용한 이클립스 프로젝트 만들기
- 2009.07.24 이클립스 단축키
- 2009.07.20 Java Decompiler(JD-GUI, JD-Eclipse)
- 2009.05.21 Listen, backlog 관계
- 2009.05.20 jMock 을 이용한 DAO 구현없이 테스트하기
- 2009.05.18 SIP 관련
- 2009.04.21 클래스 로더 관련
- 2009.04.17 자바:Timer
http://vicki.tistory.com/524
Ctrl + H : 검색(문자열, 메소드..)
Ctrl + / : 주석달기(여러줄을 선택한 후에 왼쪽 단축키를 입력)
Ctrl + Shift + f : 소스 깔끔 정리
Ctrl + F11 : Run ( 마지막에 실행했던 RUN 실행)
Ctrl + / : 주석달기(여러줄을 선택한 후에 왼쪽 단축키를 입력)
Ctrl + Shift + f : 소스 깔끔 정리
Ctrl + F11 : Run ( 마지막에 실행했던 RUN 실행)
원문:
Timer timer = new Timer(); // 타이머 생성
timer.scheduleAtFixedRate(timerTask, new Date(), 10*1000 ); // 현재시간부터 10초마다 timerTask 실행
timer.cancel(); //타이머 중지
이런 식으로 할 수 있다.
위에서 timerTask 는 java.util.TimerTask 라는 추상 객체를 상속받아 만들 수 있다.
public class MyTimerTask extends TimerTask {
public void run() {
System.out.println("Hello!");
}
}
위는 아주 간단한 사용법이고...
내가 응용해서 사용하는 방법을 하나 소개한다.
내가 하고자 하는 것은 일정 시간마다 무엇인가를 돌려야 하는데...
그 무엇인가는 동적으로 추가 또는 제거가 될 수 있다.
Timer 는 무한 쓰레드이기 때문에 시작되었으면 누군가 반드시 cancel() 메쏘드로 종료해야 한다.
즉, 누군가가 타이머의 시작과 종료를 관리를 해주어야 하는데...
나는 그 무엇인가가 들어오면 타이머가 시작되고, 무엇인가가 아무것도 없으면 타이머가 중지되도록 하고 싶다.
그 이슈로 해서 만든 소스는 다음과 같다...
public class MyTimer extends TimerTask {
private static MyTimer SINGLETON;
public synchronized static MyTimer getInstance() {
if ( SINGLETON == null ) SINGLETON = new MyTimer();
return SINGLETON;
}
private Timer timer;
private Vector taskList;
private long delay;
private MyTimer() {
this.taskList = new Vector();
this.timer = null;
this.delay = 10*1000; // 10초
}
public void insertTask(Runnable task,int idx) {
this.taskList.insertElementAt(task,idx);
if ( this.timer == null ) startTimer();
}
public void appendTask(Runnable task) {
this.taskList.insertElementAt(task,idx);
if ( this.timer == null ) startTimer();
}
public void removeTask(Runnable task) {
taskList.removeelement(task);
if ( taskList.size() == 0 ) stopTimer();
}
public boolean containsTask(Runnable task) {
return taskList.contains(task);
}
public void run() {
if ( taskList.size()>0 ) {
for(int i=0; i<taskList.size(); i++) {
Runnable task = (Runnable) taskList.elementAt(i);
task.run();
// 앞의 Task가 를 종료되고, 다음 Task를 실행하는 방식이라 new Thread(task).start() 하지 않았다.
// 용도에 맞추어 입맛대로...
}
} else {
stopTimer();
}
}
private void startTimer() {
if ( this.timer != null ) stopTimer();
this.timer = new Timer();
this.timer.scheduleAtFixedRate(this,new Date(),delay);
}
private void stopTimer() {
if ( this.timer != null ) {
this.timer.cancel();
this.timer = null;
}
}
// 테스트 해볼까..?
public static void main(String[] args) {
try {
MyTimer timer = MyTimer.getInstance();
Runnable task = new Runnable() {
public void run() {
System.out.println("Hello!");
}
}
timer.appendTask(task);
Thread.currentThread().sleep(60 * 1000); // 1분 동안 timer 가 도는 걸 확인하고...
timer.removeTask(task);
} catch(Throwable t) {
t.printStackTrace();
}
}
}
Timer timer = new Timer(); // 타이머 생성
timer.scheduleAtFixedRate(timerTask, new Date(), 10*1000 ); // 현재시간부터 10초마다 timerTask 실행
timer.cancel(); //타이머 중지
이런 식으로 할 수 있다.
위에서 timerTask 는 java.util.TimerTask 라는 추상 객체를 상속받아 만들 수 있다.
public class MyTimerTask extends TimerTask {
public void run() {
System.out.println("Hello!");
}
}
위는 아주 간단한 사용법이고...
내가 응용해서 사용하는 방법을 하나 소개한다.
내가 하고자 하는 것은 일정 시간마다 무엇인가를 돌려야 하는데...
그 무엇인가는 동적으로 추가 또는 제거가 될 수 있다.
Timer 는 무한 쓰레드이기 때문에 시작되었으면 누군가 반드시 cancel() 메쏘드로 종료해야 한다.
즉, 누군가가 타이머의 시작과 종료를 관리를 해주어야 하는데...
나는 그 무엇인가가 들어오면 타이머가 시작되고, 무엇인가가 아무것도 없으면 타이머가 중지되도록 하고 싶다.
그 이슈로 해서 만든 소스는 다음과 같다...
public class MyTimer extends TimerTask {
private static MyTimer SINGLETON;
public synchronized static MyTimer getInstance() {
if ( SINGLETON == null ) SINGLETON = new MyTimer();
return SINGLETON;
}
private Timer timer;
private Vector taskList;
private long delay;
private MyTimer() {
this.taskList = new Vector();
this.timer = null;
this.delay = 10*1000; // 10초
}
public void insertTask(Runnable task,int idx) {
this.taskList.insertElementAt(task,idx);
if ( this.timer == null ) startTimer();
}
public void appendTask(Runnable task) {
this.taskList.insertElementAt(task,idx);
if ( this.timer == null ) startTimer();
}
public void removeTask(Runnable task) {
taskList.removeelement(task);
if ( taskList.size() == 0 ) stopTimer();
}
public boolean containsTask(Runnable task) {
return taskList.contains(task);
}
public void run() {
if ( taskList.size()>0 ) {
for(int i=0; i<taskList.size(); i++) {
Runnable task = (Runnable) taskList.elementAt(i);
task.run();
// 앞의 Task가 를 종료되고, 다음 Task를 실행하는 방식이라 new Thread(task).start() 하지 않았다.
// 용도에 맞추어 입맛대로...
}
} else {
stopTimer();
}
}
private void startTimer() {
if ( this.timer != null ) stopTimer();
this.timer = new Timer();
this.timer.scheduleAtFixedRate(this,new Date(),delay);
}
private void stopTimer() {
if ( this.timer != null ) {
this.timer.cancel();
this.timer = null;
}
}
// 테스트 해볼까..?
public static void main(String[] args) {
try {
MyTimer timer = MyTimer.getInstance();
Runnable task = new Runnable() {
public void run() {
System.out.println("Hello!");
}
}
timer.appendTask(task);
Thread.currentThread().sleep(60 * 1000); // 1분 동안 timer 가 도는 걸 확인하고...
timer.removeTask(task);
} catch(Throwable t) {
t.printStackTrace();
}
}
}