(reprinted) communication between Android two sub-threads

Source: Internet
Author: User

 communication between Android two sub-threadsTags: classthreadandroid sub-threading communication2015-03-20 17:03 3239 People read comments (0) favorite reports Classification:Personal Gossip

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Android, believe that the main thread and the communication between the sub-threading everyone is not unfamiliar with it. In one interview experience, I was asked how two sub-threads communicated with each other. Hey, yo! This is a cover for me. Later went home to study the next, share to everyone.

In fact, the Android thread communication is nothing more than handler and Looper operation.

In general, communication between the main thread and the child thread is sent to the looper in the main thread through the message in the handler threads in the main course, or handler in the main thread sends a runnable through post to Looper. Looper is present in the main thread by default. What if there is no looper in the child thread? Very simply, we can bind looper to a child thread and create a handler. By sending a message through this handler in another thread, you can implement communication between the child threads!

[Java]View PlainCopy
  1. Public class Thread1 extends thread{
  2. private Handler handler1;
  3. Public Handler GetHandler () {//Note Oh, before run executes, the return is null
  4. return handler1;
  5. }
  6. @Override
  7. public Void Run () {
  8. Looper.prepare ();
  9. Handler1 = New Handler () {
  10. public void Handlemessage (Android.os.Message msg) {
  11. //handling messages here
  12. LOG.I ("Mthread", "received message:" +thread.currentthread (). GetName () +"----" +msg.obj);
  13. };
  14. };
  15. Looper.loop ();
  16. }
  17. }

[Java]View PlainCopy
  1. Public class Thread2 extends thread{
  2. @Override
  3. public Void Run () {
  4. For (int i=0; i<; i++) {
  5. Message msg = Message.obtain ();
  6. Msg.what = 1;
  7. Msg.obj = System.currenttimemillis () +"";
  8. Handler1.sendmessage (msg);
  9. LOG.I ("Mthread", Thread.CurrentThread (). GetName () +"----sent a message!  "+msg.obj);
  10. Systemclock.sleep (1000);
  11. }
  12. }
  13. }

Execution effect:

For full code download please see:

http://download.csdn.net/detail/kedaweiwei/8518701

(reprinted) communication between Android two sub-threads

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.