大家都知道,service和activity都是运行在UI线程中,超时的数据读取和网络耗时等操作必须新建线程,下面说说service中执行新线程的几种方式。
1.传统的java方式
我们都知道 java中新建线程可以继承Thread类,也可以implement Runnable接口。实质都是实现Runnable的run方法,此处底层应该是jvm识别run这个方法分配并且创建了线程。run方法只是准备好了新线程的资源,要调用
start() native方法才能启动线程。
1 public interface Runnable { 2 3 /** 4 * Starts executing the active part of the class' code. This method is 5 * called when a thread is started that has been created with a class which 6 * implements {@code Runnable}. 7 */ 8 public void run(); 9 }
1 public class Thread implements Runnable
那么在service中也是一样,我们可以用Thread类:
1 @Override 2 public int onStartCommand(Intent intent, int flags, int startId) { 3 L.e(TAG,"onStartCommand"); 4 new Thread(new Runnable() { 5 @Override 6 public void run() { 7 8 } 9 }).start(); 10 return super.onStartCommand(intent, flags, startId); 11 }
也可以实现Runnable接口:
1 Runnable runnable= new Runnable() { 2 @Override 3 public void run() { 4 L.e(TAG, "***********************"); 5 } 6 }; 7 @Override 8 public int onStartCommand(Intent intent, int flags, int startId) { 9 runnable.run(); 10 return super.onStartCommand(intent, flags, startId); 11 }
2.使用IntentService
具体见上篇博文http://www.cnblogs.com/hxy0107/p/4552486.html
3.使用HandleThread和Headler
handler本身不创建新线程,只是主UI线程和新线程通信的句柄,传递信息Message。在下次android进程通信中详细说明。使用方式如下:
1 final Handler myHandler=new Handler(){ 2 3 @Override 4 public void handleMessage(Message msg) { 5 if(msg.what==0x123){ 6 L.e(TAG, "handleMessage"); 7 } 8 } 9 }; 10 @Override 11 public void onStart(Intent intent, int startId) { 12 13 L.e(TAG,"onStart"); 14 15 new Thread(new Runnable() { 16 @Override 17 public void run() { 18 myHandler.sendEmptyMessage(0x123); 19 } 20 }).start(); 21 super.onStart(intent, startId); 22 }
这种方式下创建的Handler它是在context上下文中创建的,所以是和UI主线程的Looper绑定的,消息序列message也是Ui的looper接收的。如果我们想让Handler绑定在自己定义的线程上,那我们必须在新线程上创建消息序列Looper,好在android有了HandlerThread.HandlerThread本身只是个Thread,只是它内部实现了自己的消息序列looper,这样其他的线程就可以和我们的新线程通过handler来通信啦~(handler必须绑定新线程的looper)
1 HandlerThread thread = new HandlerThread("MyHandlerThread"); 2 thread.start(); 3 mHandler = new Handler(thread.getLooper()); 4 mHandler.post(new Runnable(){...});
1 MyHandlerThread myHandlerThread; 2 @Override 3 public void onStart(Intent intent, int startId) { 4 5 L.e(TAG,"onStart"); 6 myHandlerThread=new MyHandlerThread("myhandlerThread"); 7 myHandlerThread.start(); 8 final Handler handlerelse=new Handler(myHandlerThread.getLooper()){ 9 10 @Override 11 public void handleMessage(Message msg) { 12 if(msg.what==0x111) { 13 L.e(TAG, "handlerelse handleMessage"); 14 } 15 super.handleMessage(msg); 16 } 17 }; 18 19 new Thread(new Runnable() { 20 @Override 21 public void run() { 22 handlerelse.sendEmptyMessage(0x111); 23 } 24 }).start(); 25 super.onStart(intent, startId); 26 }
上面新建了两条线程,其中handlerThread线程具有looper,可以实现类似主UI线程和其他线程交互的操作。
4.定时性发送消息可以使用TimerTask,timerTast也是实现Runnable接口的方法,新建了线程定时发送消息。
1 new Timer().schedule(new TimerTask() { 2 @Override 3 public void run() { 4 myHandler.sendEmptyMessage(0x123); 5 } 6 },0,2000);
5.线程池ThreadPool与ThreadPoolExecutor
吃完饭在写...