Timer TimerTask Countdown Timer API

Source: Internet
Author: User
Tags time in milliseconds

Timer timers
A tool that the thread uses to schedule later in theBackground ThreadThe tasks that are performed in the. You can schedule a task to execute once, or repeat it periodically. corresponding to each Timer object is theSingle Background thread, which is used to perform all timer tasks sequentially.Timer Taskshould be done quickly. If the time to complete a timer task is too long, it will "monopolize" the task execution thread of the timer. As a result, this can delay the execution of subsequent tasks, which can be "stacked together" and can be executed quickly and continuously when the unfriendly task is finally completed.
After the last reference to the Timer object is complete, and after all the unhandled tasks have been completed, the timer's task execution line routinesNormal termination(and becomes the object of garbage collection). But it may take a long time before it happens. By default, the task execution thread does not run as a daemon, so it can prevent the application from terminating. If the caller wants to quickly terminate the task execution thread of the timer, the caller should invoke the timer's Cancel method. If the task execution thread of the timer is unexpectedly terminated, for example by calling its Stop method, all subsequent attempts to schedule the task will result in IllegalStateException, as if the Cancel method of the timer was invoked.
This class isThread Safety: Multiple threads can share a single Timer object without having to synchronize externally. This class does not provide a real-time guarantee: It uses the object.wait (long) method to schedule tasks. Implementation considerations: This class can be extended to a large number of simultaneous scheduled tasks (there are thousands of of them without problems). Internally, it uses a binary heap to represent its task queue, so the overhead of scheduling a task is O (log n), where n is the number of simultaneous tasks scheduled. Implementation considerations: All construction methods start the timer thread.
Construction method
    • Timer () to create a new timer.
    • The timer (String name) creates a new timer whose associated thread has the specified name .
    • The timer (Boolean Isdaemon) creates a new timer that specifies its associated thread to run as a daemon.
    • The timer (String name, Boolean Isdaemon) creates a new timer whose associated thread has the specified name and can be specified to run as a daemon.

Public methods
  • void cancel ()     terminates this timer, discarding all currently scheduled tasks.
      • This does not interfere when
      • Once the timer is terminated, Then its execution thread terminates, and there is no way to schedule more tasks on it.
      • Note that this timer is called by the Timer task Call this method within the Run method
      • This method can be called repeatedly, However, the second and subsequent calls are not valid.
  • int purge ()     Removes all canceled tasks from the task queue from this timer.
  • void schedule (timertask task, long delay)       t arranged in Specify delay after < Span data-wiz-span= "Data-wiz-span" to perform the specified task. Ask-the task to be scheduled, delay-the time in milliseconds before the task is executed.
  • void schedule (timertask task, Date time) < Span data-wiz-span= "Data-wiz-span" >     arranged in specified time < Span data-wiz-span= "Data-wiz-span" to perform the specified task. If this time has passed, schedule the task to execute immediately.
  • void schedule (timertask task, long delay, long period)     Schedule the specified task from the specified delay after fixed time interval (separated by a specified period) for subsequent execution.
      • in fixed delay in execution, according to to schedule each execution. . in the long run, the frequency of execution is generally slightly slower than the reciprocal of the specified period (assuming that the system clock relied on by object.wait (long) is accurate).
      • Fixed-deferred execution applies to repetitive activities that require smooth running. In other words, it short-term run in Keep the frequency accurate for activities that are more important than in the long run . This includes most animation tasks, such as fixed-time interval flashing cursor. This also includes fixed activities that are performed in response to human activity, such as automatically repeating the input characters while holding down the key.
  • voidSchedule (timertask task, Date Firsttime, long period) schedules the specified task at the specified time Begins a repetitive, fixed-delay execution.
  • void scheduleatfixedrate (timertask task, long delay, long period) schedules the specified task to begin repeating after a defined delay Fixed rate execution. with approximateFixed time interval(separated by a specified period) for subsequent execution.
    • In a fixed rate execution, each execution is scheduled according to the scheduled initial execution time . If an execution is delayed for any reason, such as a garbage collection or other background activity, two or more executions will occur quickly and continuously, allowing subsequent executions to " catch up ". In the long run, the frequency of execution will be exactly the reciprocal of the specified period (assuming that the system clock relied on by object.wait (long) is accurate).
    • fixed rate execution applies to those , such as hourly on time to play the clock, Or in the daily at a specific time run scheduled maintenance activities. It also applies to the total time that a fixed number of executions are completed Countdown timer, tick once per second, total 10 seconds. Finally, fixed rate execution is appropriate for scheduling multiple recurring timer tasks that must be synchronized with each other.
  • voidscheduleatfixedrate (timertask task, Date Firsttime, long period) schedules the specified task to The specified time begins to repeat at a fixed rate of execution.

TimerTask all implemented interfaces for the timing task:RunnableA task that is scheduled to be performed or performed repeatedly by a Timer.

Api
  • Construction Method protected   timertask () creates a new timer task.
  • A Booleancancel() cancels this timer task.
      • If the task is scheduled to execute once and is not yet running, or has not been scheduled, it will never run. If the task is scheduled to execute repeatedly, it will never run again . (If the task is running when this call occurs, the task will run out, but will never run again .) )
      • Note that calling this method from the run method of the repeating timer task absolutely guarantees that the timer task will no longer run.
      • This method can be called repeatedly, and the second and subsequent invocations are invalid.
      • Returns true if the task is scheduled to execute once and has not been run, or if the task is scheduled to be repeated. Returns False if the task is scheduled to execute once and is already running, or if the task has not been scheduled, or if the task has been canceled. (In general, returns True if this method does not allow one or more scheduled executions.) )
  • abstract voidRun () the action to be performed by this timer task.
  • Longscheduledexecutiontime () Returns the scheduled execution time of the most recent actual execution of this task.

Countdowntimer Countdown Timer https://developer.android.google.cn/reference/android/os/CountDownTimer.html
Schedule arrangement, moment, List a countdownuntilA time in the future, along with regular regular notifications on intervals interval along the way, process. Example of showing a second countdown in a text field:
@BindView(R.id.send) Button send;//发送验证码CountDownTimer timer = new CountDownTimer(60000, 1000) {@Overridepublic void onTick(long millisUntilFinished) {    send.setText(time + "S");}@Overridepublic void onFinish() {send.setEnabled(true);send.setText("重新发送");}};
The calls to OnTick (long) is synchronized to this object soOne call to OnTick (long) won ' t ever occur before the previous callback are complete. this was onlyrelevant make sense.When the implementation of OnTick (long) takesan amount of a large number of Time toExecute Execution That'ssignificantcompared to the countdown interval.
Api
  • Countdowntimer (Long millisinfuture, long Countdowninterval)
      • millisinfuture : The number of Millis in the future from the call to start () until the countdown was done and OnFinish () is C alled.
      • Countdowninterval:the interval along the to receive OnTick (long) callbacks.
  • final voidCancel ():Cancel the countdown.
  • abstract voidonfinish ():Callback fired when the time was up.
  • abstract voidonTick (Long millisuntilfinished):Callback fired on regular interval.
      • Millisuntilfinished:the amount of time until finished.
  • Final Countdowntimerstart ():start the countdown.


From for notes (Wiz)

Timer TimerTask Countdown Timer API

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.