---
title: "Handler和线程"
author: "AndLi"
date: "2012-04-18T23:17:11Z"
tags: []
categories: ["移动开发"]
slug: "handler"
draft: false
lastmod: "2012-04-18T23:17:11Z"
id: 1489932
url: "https://andli.iteye.com/blog/1489932"
---

![](http://dl.iteye.com/upload/attachment/0066/8171/f00db039-35dd-312d-a645-1c1618b322c0.jpg)

![](http://dl.iteye.com/upload/attachment/0066/8173/a63365b1-ceec-32d4-8085-88d9a96455c1.jpg)  
 package com.andli.handlerandthread;

```
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;

public class HandlerAndThreadActivity extends Activity {
	private String tag = "andli";

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		Log.i(tag, "---------------Main------------------");
		Log.i(tag, "Thread_ID=" + Thread.currentThread().getId());
		Log.i(tag, "Thread_Name=" + Thread.currentThread().getName());

		Handler handler = new Handler();
		Log.i(tag, "--------------Handler_Thread----------");
		handler.post(runnable);

		Log.i(tag, "---------------Thread------------------");
		Thread thread = new Thread(runnable);
		thread.start();

	}

	Runnable runnable = new Runnable() {
		public void run() {
			Log.i(tag, "Thread_ID=" + Thread.currentThread().getId());
			Log.i(tag, "Thread_Name=" + Thread.currentThread().getName());
		}
	};
}
```