Java Timer, TimerTask, Reminder Class Tutorial พร้อมตัวอย่าง
เผยแพร่แล้ว: 2021-02-04
java.util.Timer
เป็นคลาสยูทิลิตี้ที่สามารถใช้เพื่อกำหนดเวลาเธรดที่จะดำเนินการในเวลาที่แน่นอนในอนาคต คลาส Java Timer สามารถใช้เพื่อกำหนดเวลางานให้รันครั้งเดียวหรือรันในช่วงเวลาปกติ
java.util.TimerTask
เป็นคลาสนามธรรมที่ใช้อินเทอร์เฟซ Runnable และเราจำเป็นต้องขยายคลาสนี้เพื่อสร้าง TimerTask ของเราเองซึ่งสามารถกำหนดเวลาได้โดยใช้คลาส Java Timer
คลาสตัวจับเวลานั้นปลอดภัยสำหรับเธรดและหลายเธรดสามารถแชร์ออบเจ็กต์ Timer เดียวโดยไม่ต้องซิงโครไนซ์ภายนอก คลาสตัวจับเวลาใช้ java.util.TaskQueue เพื่อเพิ่มงานตามช่วงเวลาปกติและเมื่อใดก็ได้ อาจมีเธรดเดียวเท่านั้นที่รัน TimerTask ตัวอย่างเช่น หากคุณกำลังสร้างตัวจับเวลาให้ทำงานทุก 10 วินาที แต่การดำเนินการเธรดเดี่ยวจะใช้เวลา 20 วินาที จากนั้นวัตถุตัวจับเวลาจะเพิ่มงานไปยังคิวต่อไปและทันทีที่เธรดหนึ่งเสร็จสิ้น มันจะแจ้งคิวและเธรดอื่นจะเริ่มดำเนินการ

คลาสตัวจับเวลาใช้ Object wait และแจ้งเมธอดเพื่อกำหนดเวลางาน TimerTask เป็น abstract class
และเราสืบทอดมาเพื่อให้การนำไปใช้อย่างเป็นรูปธรรม คลาส TimerTask implements
interface
Runnable
ดังนั้นจึงเป็นเธรด ดังนั้นการใช้งาน TimerTask ของคุณจึงเป็นเธรดด้วย
ตัวอย่าง Timer and TimerTask ที่ง่ายมาก: งานจะดำเนินการเมื่อผ่านไป 5 seconds
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
package com . crunchify . tutorials ; import java . util . Timer ; import java . util . TimerTask ; /** * Author: Crunchify.com */ public class CrunchifyTimerTaskExample { Timer timer ; public CrunchifyTimerTaskExample ( int seconds ) { timer = new Timer ( ) ; timer . schedule ( new CrunchifyReminder ( ) , seconds * 1000 ) ; } class CrunchifyReminder extends TimerTask { public void run ( ) { System . out . format ( "Timer Task Finished..!%n" ) ; timer . cancel ( ) ; // Terminate the timer thread } } public static void main ( String args [ ] ) { new CrunchifyTimerTaskExample ( 5 ) ; System . out . format ( "Task scheduled.. Now wait for 5 sec to see next message..%n" ) ; } } |
ผลลัพธ์:
1 2 |
Task scheduled . . Now wait for 5 sec to see next message . . Time ' s up ! |
โปรแกรมอย่างง่ายนี้แสดงส่วนพื้นฐานต่อไปนี้ของการนำไปใช้และการจัดกำหนดการงานที่จะดำเนินการโดยเธรดตัวจับเวลา:
- ใช้คลาสย่อยที่กำหนดเองของ
TimerTask
วิธีการrun
ประกอบด้วยรหัสที่ทำงาน ในตัวอย่างนี้ คลาสย่อยชื่อ CrunchifyReminder - สร้างเธรดโดยสร้างอินสแตนซ์คลาส
Timer
- สร้างอินสแตนซ์ของวัตถุ
TimerTask
(new CrunchifyReminder()
) - กำหนดเวลางานตัวจับเวลาสำหรับการดำเนินการ
ฉันจะดำเนินการ TimerTask นี้ซ้ำ ๆ ได้อย่างไร
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
package crunchify . com . tutorials ; import java . util . Timer ; import java . util . TimerTask ; import java . awt . Toolkit ; /** * Author: Crunchify.com * Java Timer and TimerTask - Reminder Class Tutorials */ public class CrunchifyTimerTaskExample { // Toolkit This class is the abstract superclass of all actual implementations of the Abstract Window Toolkit. // Subclasses of the Toolkit class are used to bind the various components to particular native toolkit implementations. Toolkit toolkit ; // Timer A facility for threads to schedule tasks for future execution in a background thread. // Tasks may be scheduled for one-time execution, or for repeated execution at regular intervals. Timer timer ; public CrunchifyTimerTaskExample ( ) { toolkit = Toolkit . getDefaultToolkit ( ) ; timer = new Timer ( ) ; // schedule() Schedules the specified task for repeated fixed-delay execution, beginning after the specified delay. // Subsequent executions take place at approximately regular intervals separated by the specified period. timer . schedule ( new CrunchifyReminder ( ) , 0 , // initial delay 1 * 1000 ) ; // subsequent rate } // TimerTask A task that can be scheduled for one-time or repeated execution by a Timer. // A timer task is not reusable. Once a task has been scheduled for execution on a Timer or cancelled, // subsequent attempts to schedule it for execution will throw IllegalStateException. class CrunchifyReminder extends TimerTask { int loop = 5 ; public void run ( ) { if ( loop > 0 ) { // beep() Emits an audio beep depending on native system settings and hardware capabilities. toolkit . beep ( ) ; System . out . format ( "Knock Knock..!\n" ) ; loop -- ; } else { toolkit . beep ( ) ; System . out . format ( "\nThat's it.. Done..!" ) ; // cancel() Terminates this timer, discarding any currently scheduled tasks. Does not interfere with a currently executing task (if it exists). Once a timer has been terminated, // its execution thread terminates gracefully, and no more tasks may be scheduled on it. timer . cancel ( ) ; } } } public static void main ( String args [ ] ) { new CrunchifyTimerTaskExample ( ) ; System . out . format ( "Task scheduled..!%n \n" ) ; } } |
ผลลัพธ์:
1 2 3 4 5 6 7 8 9 |
Task scheduled . . ! Knock Knock . . ! Knock Knock . . ! Knock Knock . . ! Knock Knock . . ! Knock Knock . . ! That ' s it . . Done . . ! |
ตามค่าเริ่มต้น โปรแกรมจะทำงานต่อไปตราบเท่าที่เธรดตัวจับเวลายังทำงานอยู่ คุณสามารถยุติเธรดตัวจับเวลาได้สี่วิธี:

- เรียกใช้การ
cancel
บนตัวจับเวลา คุณสามารถทำสิ่งนี้ได้จากทุกที่ในโปรแกรม เช่น จากวิธีการrun
ของงานตัวจับเวลา - ทำให้เธรดของตัวจับเวลาเป็น “daemon” โดยสร้างตัวจับเวลาดังนี้:
new Timer(true)
หากเธรดเดียวที่เหลืออยู่ในโปรแกรมคือเธรด daemon โปรแกรมจะออก - หลังจากที่งานตามกำหนดเวลาของตัวจับเวลาดำเนินการเสร็จสิ้นแล้ว ให้ลบการอ้างอิงทั้งหมดไปยังวัตถุตัว
Timer
ในที่สุด เธรดของตัวจับเวลาจะสิ้นสุดลง - เรียกใช้เมธอด
System.exit
ซึ่งจะทำให้โปรแกรมทั้งหมด (และเธรดทั้งหมด) ออกจากโปรแกรม