logo

שיטת Java Timer schedule()

שיטת התזמון (משימה TimerTask, Date time) של מחלקה טיימר משמשת לתזמון המשימה לביצוע בזמן הנתון. אם הזמן שניתן הוא בעבר, המשימה מתוכננת באותה תנועה לביצוע.

תחביר

 public void schedule(TimerTask task, Date time) 

פָּרָמֶטֶר

משימה - זו המשימה שיש לתזמן.

זמן - זה הזמן שבו המשימה אמורה להתבצע.

זורק

חריגה לא חוקית - זה זורק את החריג כאשר time.getTime() הוא שלילי.

חריגה לא חוקית - הוא זורק את החריג כאשר המשימה מתוזמנת או מבוטלת, או כאשר הטיימר מבוטל, או כאשר שרשור הטיימר מסתיים.

NullPointerException - זה זורק את החריג כאשר המשימה או הזמן הם בטלים.

דוגמה 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, תקופה ארוכה) הוא השיטה של ​​מחלקה טיימר. הוא משמש לתזמן את המשימה הנתונה שוב ושוב בביצוע זמן קבוע נתון.

תחביר

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

פָּרָמֶטֶר

משימה - זו המשימה שמתוכננת.

פעם ראשונה - זו הפעם הראשונה שבה משימה נתונה מבוצעת.

פרק זמן - זה הזמן לביצוע באלפיות שניות.

זורק

חריגה לא חוקית - הוא זורק את החריג כאשר firstTime.getTime() קטן מ-0, או נקודה גדולה או שווה ל-0

חריגה לא חוקית - הוא זורק את החריג כאשר המשימה כבר מתוזמנת או מבוטלת, או כאשר הטיימר מבוטל, או כאשר חוט הטיימר מסתיים.

NullPointerException - זה זורק את החריג כאשר המשימה או הזמן הם בטלים.

דוגמה 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++)>