본문 바로가기
개발 공부 기록하기/- Kotlin & Java

ScheduleExecutorService 와 Timer를 이용한 일정시간 메소드 실행

by soulduse 2016. 1. 4.
반응형

ScheduleExecutorService 와 Timer를 이용한 일정시간 메소드 실행예제


Timer는 같은 이름으로 객체를 여러개 만들었을 경우 가장 마지막에 생성한

객체는 close()로 종료 할 수 있으나, 나머지 실행중인 객체들은 종료할 수 없다.


반면, ScheduleExecutorService의 경우 객체를 여러개 만들어 실행시키더라도 shutdownNow()를 사용하여 일괄종료 할 수 있다.


구글링 결과 Stack Overflow의 내용을 참조한 결과 확장성이나 여러가지로 ScheduleExecutorService가 

좋다고 한다.



 - ScheduleExecutorService


public class ScheduleExecutorServiceEx {
private static final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);

public static void main(String[] args) {
ScheduleExecutorServiceEx se = new ScheduleExecutorServiceEx();
se.timerInit();

try{
Thread.sleep(20000);
timerStop();
}catch (Exception e){
e.printStackTrace();
}
}

public static void timerInit() {
System.out.println("timerInit");
timerStart(1, 1);
timerStart(2, 5);
timerStart(3, 10);
}

public static void timerStart(final int condition, int time) {
final Runnable runnable = new Runnable() {
@Override
public void run() {
switch (condition){
case 1:{
System.out.println("A==========="+new Date());
break;
}
case 2:{
System.out.println("=====B======"+new Date());
break;
}
case 3:{
System.out.println("===========C"+new Date());
break;
}
}
}
};
scheduler.scheduleAtFixedRate(runnable, 1, time, TimeUnit.SECONDS);
}

public static void timerStop() {
System.out.println("=========================");
System.out.println("isShutdown :"+scheduler.isShutdown());
System.out.println("isTerminated :"+scheduler.isTerminated());
System.out.println("=========================");

scheduler.shutdownNow();

System.out.println("=========================");
System.out.println("isShutdown :"+scheduler.isShutdown());
System.out.println("isTerminated :"+scheduler.isTerminated());
System.out.println("=========================");

try{
System.out.println(scheduler.awaitTermination(2,TimeUnit.SECONDS));
}catch (Exception e){
e.printStackTrace();
}
}


}


 - Timer


public class PrintTimer {
static Timer jobScheduler = null;

public static void main(String[] args) {
timerInit();


try{
Thread.sleep(20000);
timerStop();
}catch (Exception e){
e.printStackTrace();
}
}

public static void timerInit(){
System.out.println("timerInit");
timerStart(1,1000);
timerStart(2,5000);
timerStart(3,10000);
}

public static void timerStart(int condition, int time){
ScheduleJob job = new ScheduleJob(condition);
timerObj(false);
jobScheduler.scheduleAtFixedRate(job, 1000, time);
}

public static void timerStop(){
timerObj(true);
jobScheduler.cancel();
}

public static void timerObj(boolean deamon){
jobScheduler = new Timer(deamon);
}
}

class ScheduleJob extends TimerTask{

int condition = -1;

public ScheduleJob(int condition){
this.condition = condition;
}

@Override
public void run() {
switch (condition){
case 1:{
System.out.println("A==========="+new Date());
break;
}
case 2:{
System.out.println("=====B======"+new Date());
break;
}
case 3:{
System.out.println("===========C"+new Date());
break;
}
}
}
}





반응형