多线程学习Demo

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity {
 
 private static final String TAG = "MainThread";

 private Handler mMainHandler, mChildHandler;

 private TextView info;

 private Button msgBtn;

 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);

  info = (TextView) findViewById(R.id.textView1);
  msgBtn = (Button) findViewById(R.id.button1);

  mMainHandler = new Handler() {
   @Override
   public void handleMessage(Message msg) {
    Log.i(TAG+"4444444444444", "Got an incoming message from the child thread - "
      + (String) msg.obj);
    // 接收子线程的消息
    info.setText((String) msg.obj);
   }
  };

  new ChildThread().start();

  msgBtn.setOnClickListener(new OnClickListener() {

   @Override
   public void onClick(View v) {
    
    
    // 发送消息给子线程
    if (mChildHandler != null) {
     Message childMsg = mChildHandler.obtainMessage();
     childMsg.obj = mMainHandler.getLooper().getThread()
       .getName()
       + "says hello!";
     Log.i(TAG+"11111111111111", "Send a message to the child thread - "
       + (String) childMsg.obj);
     mChildHandler.sendMessage(childMsg);
     

    }
   }
  });

 }

 @Override
 protected void onDestroy() {
  super.onDestroy();
  Log.i(TAG+"5555555555", "Stop looping the child thread's message queue");
  mChildHandler.getLooper().quit();

 }

 class ChildThread extends Thread {
  private static final String CHILD_TAG = "ChildThread";

  @Override
  public void run() {
   this.setName("ChildThread");
   // 初始化消息循环队列,需要在Handler创建之前
   Looper.prepare();
   Log.i(CHILD_TAG+"?????????", "????????");
   mChildHandler = new Handler() {
    
    @Override
    public void handleMessage(Message msg) {
     Log.i(CHILD_TAG+"22222222222222",
       "Got an incoming message from the main thread - "
         + (String) msg.obj);

     try {
      sleep(100);
      Message toMain = mMainHandler.obtainMessage();
      toMain.obj = "This is "
        + this.getLooper().getThread().getName()
        + ".  Did you send me \"" + (String) msg.obj
        + "\"?";
      mMainHandler.sendMessage(toMain);

      Log.i(CHILD_TAG+"333333333333", "Send a message to the main thread - "
        + (String) toMain.obj);

     } catch (InterruptedException e) {
      e.printStackTrace();
     }

    }
   };
   Log.i(CHILD_TAG+"000000000000", "Child handler is bound to - "
     + mChildHandler.getLooper().getThread().getName());
   // 启动子线程消息循环队列
   Looper.loop();

  }
 }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值