Basic knowledge in Java grooming "Timers for multithreading"-----March 15, 2015

Source: Internet
Author: User
Tags terminates

Timer Introduction:

(1) Basic method:

Public Timer(): Creates a new timer. The associated thread is not running as a daemon.

public void Schedule(timertask task, long delay, long period)

Schedules the specified task to start repeating fixed deferred execution from the specified delay. Follow-up is performed at approximately fixed intervals (delimited by the specified period).

public void Schedule(timertask task, Date time)

Schedules the specified task to execute at the specified time. If this time has passed, schedule the task to execute immediately.

Parameters:

task-The tasks to be scheduled.

time-Time to perform the task.

public void Cancel()

Terminates this timer, discarding all currently scheduled tasks. This does not interfere with the currently executing task, if it exists. Once the timer is terminated, its execution thread terminates, and no more tasks can be scheduled on it.

TimerTask Introduction:

public abstract void Run()

The action to be performed by this timer task.

Public boolean cancel()

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.) )

How the timer implements Multithreading:

The timer object and the TimerTask object binding use the Schedule method in the Timer object to trigger the run () method in the TimerTask class to implement multithreading.

Instance:

 Packagetest1;Importjava.util.Date;Importjava.util.IllegalFormatCodePointException;ImportJava.util.Timer;ImportJava.util.TimerTask;classTaskextendstimertask{Private  intA=1; Private intSum=0;  Public voidrun () {sum+=A; A++; System.out.println ("HELLO"); if(a>3) {System.out.println (sum);  This. Cancel ();//the Cancel () method here is called by the TimerTask object, where the timer and timer classes all contain the Cancel () method        }        }} Public classTimerTask1 { Public Static voidMain (string[] args) {Timer time=NewTimer (); Task T=NewTask (); Time.schedule (T,NewDate (), 1); }}

Basic knowledge in Java grooming "Timers for multithreading"-----March 15, 2015

Related Article

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.