On the relationship between timer, TimerTask and thread

1. Timer is a timer that can execute a fixed task TimerTask according to the specified time and specified execution cycle. Examples are as follows:

The relationship between timer and thread can be found in timer source code as follows:

1. The following is the default constructor of timer, which calls one of its own parameterized constructors:

2. The code of the parameterized constructor is as follows:

3. The parameter constructor sets the name of the current thread and starts the thread. The thread code is as follows:

4. The source code of timerthread class is as follows:

From the above code, we can see that timer is actually a subclass of thread class.

Relationship between TimerTask and thread:

1. The following is the source code of TimerTask constructor:

2. The following is the definition of TimerTask class:

As can be seen from the above code, TimerTask is a thread that implements the runnable interface. There is an abstract run method in this class, as follows:

So we must implement this run method in a subclass of TimerTask.

Summary:

The above relationship between timer and TimerTask is similar to the following thread relationship:

The above discussion on the relationship between timer and TimerTask and thread is all the content shared by Xiaobian. I hope it can give you a reference and support more programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>