E thấy Timer, TimmerTask và @Schedule đều dùng để tạo task được. Vậy có những điểm khác nhau nào ở nó ạ? Trước giờ e chỉ chạy job bằng @Schedule và set cron cho nó thôi ạ.
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Em đang nói đến TimerTask này nhỉ? Vậy thì sẽ khác nhau thế này:
1. TimmerTask là một interface thuộc java.util cung cấp cho lớp Timer. Nó là một interface cũ rồi và timer nó cũng chỉ cung cấp một số cách thức schedule đơn giản như theo delay hay theo period. Như vậy nếu em chỉ cần nghiệp vụ schedule theo deploy, period thì dùng thằng này cũng được. Hoặc em cũng có thể sử dụng ScheduledExecutorService là một interface mới hơn của java.
2. @Schedule: Là một annotation cho phép hàm được gọi theo định kỳ với expression đầu vào. Nó đa hạng hơn về các kiểu chạy, ví dụ như giờ nào, phút nào, năm nào, thứ mấy … Vậy nên nó phù hợp với những bài toán lập lịch phức tạp.
Nhìn chung nếu em chỉ cần nghiệp vụ schedule đơn giản chạy theo delay, period thì dùng ScheduledExecutorService hoặc TimmerTask là đủ. Còn nếu cao cấp hơn thì em dùng Schedule.