How to implement message mechanism easily in JAVA eclipse

Source: Internet
Author: User
Tags try catch

Most of the time, we need to implement the message mechanism is nothing more than a class to get the data, we need to pass to a main interface to display, you can put this message in the class global variables, the main interface class with a thread timed scan, if the data is not empty, then the class refresh, then update the main interface of the data, When the update is complete, the data is emptied so that the next time the judgment is empty, it is not updated until a subclass refreshes the location.

But even so, it is not so simple, such as I in the main program, the dead loop continuously output an int type variable value, using two try Catch statements, the first will not die, in the log window can see the increase in data, and the second is not executed, the text box does not increase the data on a regular basis

?

We need to use Java handler to do this, first of all in the main program, declare handler type variables, and then oncreate the time to do this function to do things (can point to a new function, or directly write, note that the function format is dead, The message type variable is required to come in, but what function is actually executed internally is arbitrary, for example, I want to execute Updatatalk, which is to update the main interface with a string.

?

To implement the message mechanism of the problem, our main program once OnCreate started a thread, the threads continue to use the first in this article, to determine whether a global string is empty or null, if not the description of the new data, to perform the update. Note that the start of thread T1 executes the Run method, judging and refreshing the data every 100ms. Can try a direct update when not, must be handed over to handler to deal with.

?

Of course, after the update, you have to clear the data, or you will continue to prompt the same words

?

?

How to implement message mechanism easily in JAVA eclipse

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.