Android multithreaded Research (2)--timers

Source: Internet
Author: User

Take a look at the code first:

public static void Main (string[] args) {new Timer (). Schedule (new TimerTask () {@Overridepublic void run () {System.out.prin TLN ("sun Cockroach");}, 0;while (True) {System.out.println (i++); try {thread.sleep ()} catch ( Interruptedexception e) {e.printstacktrace ();}}}

Modify the above code as follows:

New Timer (). Schedule (new TimerTask () {@Overridepublic void run () {System.out.println ("sun Cockroach");}}, 2000, 3000);
At this point the timer timer will execute the Run method after 2000ms, and repeat every 3000ms.

If there is a need, 1s, 2s, 1s, 2s ... Execute the code in the timer separately (with even seconds and odd seconds executing different timers), how we implement (s for seconds)

Implementation method One:

public static void Main (string[] args) {new Timer (). Schedule (new TimerTask () {@Overridepublic void run () {System.out.prin TLN ("Sunshine cockroach 2 Seconds"); new Timer (). Schedule (new TimerTask () {@Overridepublic void run () {System.out.println ("Sun cockroach 4 Seconds");}, 1000 }}, (+), int i = 0;while (true) {System.out.println (i++); try {thread.sleep (+);} catch (Interruptedexception E ) {e.printstacktrace ();}}}
Implementation method Two:

static int Count  = 0;public static void Main (string[] args) {class Mytimertask extends timertask{@Overridepublic void Run () {count = (count + 1)% 2; System.out.println ("Sun Xiao Qiang" + (2 + 2 * count) + "seconds"), new Timer (). Schedule (New Mytimertask (), + + * count);}} New Timer (). Schedule (New Mytimertask (), n), int i = 0;while (true) {System.out.println (i++); try {thread.sleep (1000);} catch (Interruptedexception e) {e.printstacktrace ();}}}
There is also a situation is the timing of e-mail, we can use the following way


Import Java.util.calendar;import java.util.date;import Java.util.timer;import Java.util.timertask;public class Testtimer {//interval private static final long Period_day = $ * $ * 1000;public static void Main (string[] args) {Calen Dar Calendar = calendar.getinstance ();/*** custom Daily 2:00 execution method ***/calendar.set (Calendar.hour_of_day, 2); Calendar.set ( Calendar.minute, 0); Calendar.set (Calendar.second, 0);D ate Date = Calendar.gettime (); Time of the first execution of a scheduled task//If the time of the first execution of the scheduled task is less than the current time//The time of the first execution of the scheduled task is added to the day so that the task executes at the next point in time. If one day is not added, the task executes immediately. if (Date.before (new Date ())) {date = Addday (date, 1);} New timer (). Schedule (new TimerTask () {@Overridepublic void run () {//todo to send mail, or other scheduled tasks}, date, period_day);} Increase or decrease the number of days public static date Addday (date date, int num) {Calendar STARTDT = calendar.getinstance (); Startdt.settime (date); Startdt.add (Calendar.day_of_month, num); return startdt.gettime ();}}








Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.