"Java" timers, threads, and anonymous inner classes

Source: Internet
Author: User

The internal anonymous class is written in some Java books very abstract, in fact, this thing, if it is taken out alone, it is really difficult to make sense, I would have thought so abstract concept, and nothing to use. It never occurred to me that this thing is especially used in Android-programmed listeners. Anonymous inner class is not difficult at all, we have been in use, but have been not aware of their use, was asked this concept is ignorant. If you have used jquery, you must have seen the following structure:

$ ("#组件id"). Click (function () {    
The Listener function () {} where the component ID is clicked is an anonymous intrinsic.

Well, if you've ever used jquery, I'll illustrate the problem with a small example like the one below.


Define a timer to add I to every 1 seconds. Then define a thread that checks I for more than 3 every 5 seconds, and if I exceeds 3, stop the timer and die naturally.

The use of timers in the "Java" using timer and TimerTask timed tasks (click to open the link) has said that the use of threads in the "Java" thread concurrency, mutual exclusion and synchronization (click the Open link) discussed.

Originally if you want to use a timer in Java, the thread to write a longer code, the first new timer or thread, and then open an inheritance timer, thread abstract interface class, rewrite the method inside.

However, if you use anonymous internal classes, the code of the program evolves to this:

Import java.util.*;p Ublic class Timertheardtest {private static int i = 0;private static Timer timer;public static void Ma In (string[] args) {timer = new timer (); Timer.schedule (new TimerTask () {@Override//If an int i=2 appears here, this I will act on all methods of this anonymous inner class// The above private static int i=0; fail//Note that there is an int i=2 at this time, there is no syntax error! public void Run () {System.out.println (i++);}}, 0, +), new Thread (new Runnable () {@Override//in Runnable, if you want to keep the thread running on its own , you must define the while structure yourself//If this run () method is finished, the entire thread dies naturally public void run () {//defines a thread abort flag Boolean iscontinue = True;while (iscontinue) { try {thread.sleep (//java); Hibernate of the thread, must be in the Try-catch structure} catch (Interruptedexception e) {e.printstacktrace ();} if (i > 3) {timer.cancel ();//Abort Timer iscontinue = false;//jump out of this dead loop, let the thread die naturally}}}). Start ();//The default thread does not start, you must start ()}}

The program first defines two global variables covering the entire main class, one self-increment I, and one timer. Self-increment i to be the timer, the thread simultaneously operation, therefore becomes the global variable is certain. Some may wonder why timer timers are also global variables. That's because there is a way to abort the timer in the thread. If you define a timer in the main function, its scope cannot be overridden in the thread runnable this anonymous inner class.

After that, it is very clear to see that the first parameter of the timer must fill in the class of inheriting timertask, in order to match, we would have to open another class, at the same time named for this class, and then put here, but because the use of anonymous inner class, can now be directly reproduced. The same is true for threads. The structure of the jquery, ExtJS JavaScript front-end framework is very much wanted here. Android's component listeners are also heavily using anonymous internal classes.

In fact, the previous Java program in the "Java" collections Sort method Comparator (click to open link) I also used anonymous internal classes, but I did not notice the concept.

"Java" timers, threads, and anonymous inner classes

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.