logo

자바 타이머 Schedule() 메소드

Timer 클래스의 Schedule(TimerTask 작업, 날짜 시간) 메서드는 지정된 시간에 작업이 실행되도록 예약하는 데 사용됩니다. 주어진 시간이 과거인 경우 작업은 해당 이동에서 실행되도록 예약됩니다.

통사론

 public void schedule(TimerTask task, Date time) 

매개변수

일 - 예정된 작업입니다.

시간 - 작업이 실행되는 시간입니다.

던지기

IllegalArgumentException - time.getTime()이 음수이면 예외가 발생합니다.

IllegalStateException - 작업이 예약되거나 취소될 때, 타이머가 취소될 때, 타이머 스레드가 종료될 때 예외가 발생합니다.

NullPointerException - 작업이나 시간이 null인 경우 예외가 발생합니다.

실시예 1

 import java.util.*; public class JavaTimerScheduleExample1 { public static void main(String[] args) { // creating timer task, timer Timer t = new Timer(); TimerTask tt = new TimerTask() { @Override public void run() { System.out.println('Task is on'); }; }; t.schedule(tt, new Date()); } } 
지금 테스트해보세요

산출:

 Task is on 

실시예 2

 import java.util.*; public class JavaTimerScheduleExample2{ public static void main(String[] args) { // creating timer task, timer Timer t = new Timer(); TimerTask tt = new TimerTask() { @Override public void run() { for(int i=1; i<=10;i++) { system.out.println('working on'); } }; t.schedule(tt, new date()); < pre> <span> Test it Now </span> <p> <strong>Output:</strong> </p> <pre> working on working on working on working on working on working on working on working on working on working on </pre> <p>The schedule (TimerTask task, Date firstTime, long period) is the method of Timer class. It is used to schedule the given task again and again in given fixed time execution.</p> <h2>Syntax</h2> <pre> public void schedule(TimerTask task, Date firstTime, long period) </pre> <h2>Parameter</h2> <p> <strong>task -</strong> It is the task that is scheduled.</p> <p> <strong>firstTime -</strong> It is the First time in which given task is executed.</p> <p> <strong>period -</strong> It is the time in milliseconds execution.</p> <h2>Throws</h2> <p> <strong>IllegalArgumentException -</strong> It throws the exception when firstTime.getTime() is less than 0, or period is greater than or equal to 0</p> <p> <strong>IllegalStateException -</strong> It throws the exception when the task is already scheduled or canceled, or when the timer is canceled, or when timer thread is terminated.</p> <p> <strong>NullPointerException -</strong> It throws the exception when task or time is null.</p> <h3>Example 3</h3> <pre> import java.util.*; public class JavaTimerScheduleExample3 { public static void main(String[] args) { // creating timer task, timer Timer t = new Timer(); TimerTask tt = new TimerTask() { @Override public void run() { System.out.println(&apos;working on&apos;); }; }; t.schedule(tt, new Date(),1000); ; } } </pre> <span> Test it Now </span> <p> <strong>Output:</strong> </p> <pre> working on working on working on working on . . . . . </pre> <h3>Example 4</h3> <pre> import java.util.*; public class JavaTimerScheduleExample4{ public static void main(String[] args) { // creating timer task, timer Timer t = new Timer(); TimerTask tt = new TimerTask() { @Override public void run() { for(int i=1; i<=10;i++) { system.out.println('working on'); } }; t.schedule(tt, new date(),1000); < pre> <span> Test it Now </span> <p> <strong>Output:</strong> </p> <pre> working on working on working on working on . . . . . </pre> <br></=10;i++)></pre></=10;i++)>

일정(TimerTask 작업, Date firstTime, 장기 기간)은 Timer 클래스의 메서드입니다. 주어진 고정 시간 실행에서 주어진 작업을 반복해서 예약하는 데 사용됩니다.

통사론

 public void schedule(TimerTask task, Date firstTime, long period) 

매개변수

일 - 예정된 작업입니다.

처음으로 - 주어진 작업이 처음으로 실행되는 시간입니다.

기간 - 밀리초 단위의 실행 시간입니다.

던지기

IllegalArgumentException - firstTime.getTime()이 0보다 작거나 기간이 0보다 크거나 같으면 예외가 발생합니다.

IllegalStateException - 작업이 이미 예약되었거나 취소된 경우, 타이머가 취소된 경우 또는 타이머 스레드가 종료된 경우 예외가 발생합니다.

NullPointerException - 작업이나 시간이 null인 경우 예외가 발생합니다.

실시예 3

 import java.util.*; public class JavaTimerScheduleExample3 { public static void main(String[] args) { // creating timer task, timer Timer t = new Timer(); TimerTask tt = new TimerTask() { @Override public void run() { System.out.println(&apos;working on&apos;); }; }; t.schedule(tt, new Date(),1000); ; } } 
지금 테스트해보세요

산출:

 working on working on working on working on . . . . . 

실시예 4

 import java.util.*; public class JavaTimerScheduleExample4{ public static void main(String[] args) { // creating timer task, timer Timer t = new Timer(); TimerTask tt = new TimerTask() { @Override public void run() { for(int i=1; i<=10;i++) { system.out.println(\'working on\'); } }; t.schedule(tt, new date(),1000); < pre> <span> Test it Now </span> <p> <strong>Output:</strong> </p> <pre> working on working on working on working on . . . . . </pre> <br></=10;i++)>