In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "Timer how to achieve custom time interval chain explosions". In daily operation, I believe many people have doubts about how Timer realizes custom time interval chain bombings. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the questions of "Timer how to achieve custom time interval chain explosions". Next, please follow the editor to study!
In traditional threading technology, there is a timer. The class of timer is Timer. The purpose of using timer is to assign it a task so that it can complete the task at a specified time. So first take a look at the methods in the Timer class (mainly look at the commonly used TimerTask () method):
The return value method name method describes that voidschedule (TimerTasktask,longdelay) is scheduled to execute the specified task after the specified delay. Voidschedule (TimerTasktask,longdelay,longperiod) schedules the specified task to be executed repeatedly with a fixed delay after the specified delay. Voidschedule (TimerTasktask,Datetime) schedules the execution of specified tasks at the specified time voidschedule (TimerTasktask,DatefirstTime,longperiod) schedules the specified tasks to be executed with repetitive fixed delays starting at the specified time.
The first two are executed or repeated after a specified delay, and the latter two are executed or repeated at a specified time. Let's take the previous two examples to study the use of timers. First write a simple demo that uses a timer, and then slowly extend it.
Public class TraditionalTimer {public static void main (String [] args) {/ / demo new Timer () .schedule of a simple timer (new TimerTask () {@ Override public void run () {/ / actually throws an object in Here we can manipulate all the methods of this object System.out.println ("--boom--") While (true) {System.out.println (new Date (). GetSeconds ()); try {Thread.sleep (1000);} catch (Exception e) {e.printStackTrace () }
We define a TimerTask object with an anonymous inner class, need to override the run () method, and then run the program to see that it prints 2 seconds after the first time-- every 3 seconds after the boom--,.
We can also implement the above repeated execution by ourselves, we use the "serial set" of the timer! That is, another timer is set in the timer, a timer task is completed, and a timer is installed at the end of the task. Then we need to define our own timer task, install a timer in our own timer task, and throw the custom timer task into it. Then when we start the timer, we can throw in the timer task that we define. As follows:
Public class TraditionalTimer {public static void main (String [] args) {/ / customize a timer task class MyTimerTask extends TimerTask {@ Override public void run () {System.out.println ("--boom--") / / install a timer after the task is executed, and throw it into the custom timer task new Timer (). Schedule (new MyTimerTask (), 3000);}} new Timer () .schedule (new MyTimerTask (), 2000) / / start the timer while (true) {/ / print for a second and output System.out.println (new Date (). GetSeconds ()) once a second; try {Thread.sleep (1000);} catch (Exception e) {e.printStackTrace ();}
In this way, we can easily achieve a series of explosions through the "serial set" of the timer. But now the problem is, in the method provided above, the execution is repeated at fixed intervals. If I want to execute every 2 seconds, then every 4 seconds, then every 2 seconds, then every 4 seconds. How can this be achieved?
You can do this. We define a global private member variable to record the number of explosions. If the number of explosions is odd, the number of explosions is 2 seconds, if the number of explosions is even, 4 seconds, or vice versa. Modify as follows:
Public class TraditionalTimer {private static int count = 0; / / record the number of explosions public static void main (String [] args) {class MyTimerTask extends TimerTask {@ Override public void run () {count = (count + 1)% 2; / / the result is only 0 and 1 System.out.println ("--boom--") / / set a new timing time new Timer () .schedule (new MyTimerTask (), 2000+2000*count);}} new Timer () .schedule (new MyTimerTask (), 2000) according to the count result; while (true) {/ / print seconds, and output System.out.println (new Date () .getSeconds ()) once a second Try {Thread.sleep (1000);} catch (Exception e) {e.printStackTrace ();}
In that case, we have implemented a custom explosion interval. The above is achieved by defining a global private variable, but we can do the same: aren't we going to achieve a chain attack with two different time intervals? I can define two timer tasks An and B, start the timer after An is executed, load the B task, and B start the timer to install A, which is OK. As follows:
Public class TraditionalTimer {public static void main (String [] args) {new Timer (). Schedule (new MyTimerTaskA (), 2000); / / An and B open any while (true) {/ / print seconds, and output System.out.println (new Date (). GetSeconds ()) once a second; try {Thread.sleep (1000) } catch (Exception e) {e.printStackTrace ();} / / Custom two timer task classes, inheriting TimerTask to class MyTimerTaskA extends TimerTask {@ Override public void run () {System.out.println ("--boomA--"); new Timer () .schedule (new MyTimerTaskB (), 4000) }} class MyTimerTaskB extends TimerTask {@ Override public void run () {System.out.println ("--boomB--"); new Timer () .schedule (new MyTimerTaskA (), 2000);} at this point, the study on "how Timer implements chain bombings with custom time intervals" is over, hoping to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.