Android线程的创建与销毁

Android线程的创建与销毁 原文:http://angrycode.cn/archives/447

在Android中经常会使用到线程,一想到线程,很多同学就立即使用

1  new Thread(){...}.start();

这样的方式。这样如果在一个Activity中多次调用上面的代码,那么将创建多个匿名线程,程序运行的越久可能会越来越慢。因此,需要一个Handler来启动一个线程,以及删除一个线程。
保证线程不会重复的创建。

1、创建Handler的一般方式
一般会使用

1  HandlerThread thread = new HandlerThread("MyHandlerThread");
2  thread.start();
3  mHandler = new Handler(thread.getLooper());
4  mHandler.post(mBackgroundRunnable);

创建HandlerThread时要把它启动了,即调用start()方法。然后创建Handler时将HandlerThread中的looper对象传入。那么这个mHandler对象就是与HandlerThread这个线程绑定了(这时
就不再是与UI线程绑定了,这样它处理耗时操作将不会阻塞UI)。
最后把实现耗时操作的线程post到mHandler的消息队列里面。注意的是,mBackgroundRunnable这个线程并没有启动,因为没有调用start()方法。

3、完整的angrycode

 1 public class MainActivity extends Activity implements OnClickListener{
 2         public static final String TAG = "MainActivity";
 3          
 4         private Handler mHandler;
 5          
 6         private boolean mRunning = false;
 7          
 8         private Button mBtn;
 9         @Override
10         protected void onCreate(Bundle savedInstanceState) {
11                 super.onCreate(savedInstanceState);
12                 setContentView(R.layout.activity_main);
13                 HandlerThread thread = new HandlerThread("MyHandlerThread");
14                 thread.start();//创建一个HandlerThread并启动它
15                 mHandler = new Handler(thread.getLooper());//使用HandlerThread的looper对象创建Handler,如果使用默认的构造方法,很有可能阻塞UI线程
16                 mHandler.post(mBackgroundRunnable);//将线程post到Handler中
17                  
18                 mBtn = (Button)findViewById(R.id.button);
19                 mBtn.setOnClickListener(this);
20         }
21         @Override
22         protected void onResume() {
23                 super.onResume();
24                 mRunning = true;
25         }
26         @Override
27         protected void onStop() {
28                 super.onStop();
29                 mRunning = false;
30         }
31         @Override
32         public boolean onCreateOptionsMenu(Menu menu) {
33                 // Inflate the menu; this adds items to the action bar if it is present.
34                 getMenuInflater().inflate(R.menu.main, menu);
35                 return true;
36         }
37         //实现耗时操作的线程
38         Runnable mBackgroundRunnable = new Runnable() {
39                  
40                 @Override
41                 public void run() {
42                         //----------模拟耗时的操作,开始---------------
43                         while(mRunning){
44  
45                                 Log.i(TAG, "thread running!");
46                                  
47                                 try {
48                                         Thread.sleep(200);
49                                 } catch (InterruptedException e) {
50                                         e.printStackTrace();
51                                 }
52                         }
53                         //----------模拟耗时的操作,结束---------------
54                 }
55         };
56         @Override
57         protected void onDestroy() {
58                 super.onDestroy();
59                 //销毁线程
60                 mHandler.removeCallbacks(mBackgroundRunnable);
61                  
62         }
63         @Override
64         public void onClick(View v) {
65                 Toast.makeText(getApplication(), "click the button!!!", Toast.LENGTH_SHORT).show();
66         }
67 }

上面的angrycode中,如果在onCreate()方法中里面没有使用HandlerThread而是在直接使用Handler的默认构造方法来创建Handler,那么mBackgroundRunnable将会阻塞UI线程。

4、线程销毁
用上面的方式来创建线程,在销毁时就可以使用

1 mHandler.removeCallbacks(mBackgroundRunnable);

销毁一个线程,这样就可以避免在多次进入同一个Activity时创建多个同时运行着的线程。

原文:http://angrycode.cn/archives/447

原文地址:https://www.cnblogs.com/D-Luffy/p/3140096.html