標籤:
最近在做一個項目,在網路請求時考慮用Handler進行處理,然後就研究了一下Handler和Runnable
首先在看一下java中的Runnable
The Runnable interface should be implemented by any class whose instances are intended to be executed by a thread. The class must define a method of no arguments called run.
This interface is designed to provide a common protocol for objects that wish to execute code while they are active. For example, Runnable is implemented by class Thread. Being active simply means that a thread has been started and has not yet been stopped.
In addition, Runnable provides the means for a class to be active while not subclassing Thread. A class that implements Runnable can run without subclassing Thread by instantiating a Thread instance and passing itself in as the target. In most cases, the Runnable interface should be used if you are only planning to override the run() method and no other Thread methods. This is important because classes should not be subclassed unless the programmer intends on modifying or enhancing the fundamental behavior of the class.
這個官方文檔中的描述,看原版文檔還是很有好處的,就不翻譯了。
在Java中Runnable可以實現資源共用的多線程,網上多拿賣票的例子來講,但是看上去有點模糊不清,對於這樣一個代碼
MyRunnable runnable = new MyRunnable;new Thread(runnable,"a").start();new Thread(runnable,"b").start();
它們之間資源共用到底是怎麼個共用呢?
現在我們來看一下例子
public class Main { public static void main(String[] args) { new Main().test(); } void test(){ MyRunnable runnable = new MyRunnable(); new Thread(runnable,"a").start(); new Thread(runnable,"b").start(); }
public class MyRunnable implements Runnable{ @Override public void run() { for (int i=0;i<5;i++) { System.out.println(Thread.currentThread().getName() + i); } } }}
執行結果
a0
b0
b1
b2
b3
b4
a1
a2
a3
a4
可以看到這兩個線程相互之間並沒有影響得都執行了5次
然後在我們的Runnable類裡邊加一個全域變數
public class Main { public static void main(String[] args) { new Main().test(); } void test(){ MyRunnable runnable = new MyRunnable(); new Thread(runnable,"a").start(); new Thread(runnable,"b").start(); } public class MyRunnable implements Runnable{ int num=0; @Override public void run() { for (int i=0;i<5;i++) { ++num; System.out.println(Thread.currentThread().getName() + i+"--------"+num); } } }}
執行結果
b0--------2
b1--------3
a0--------2
b2--------4
a1--------5
b3--------6
a2--------7
b4--------8
a3--------9
a4--------10
可以發現這個全域變數是在兩個線程之間共用的
這說明什嗎?當一個Runnable對象傳遞到多個線程執行時,Runnable對象的run()方法會在多個線程中執行,而全域變數是在這些線程中共用的
因此,我們可以通過一個全域變數充當線程鎖比如:
public class Main { public static void main(String[] args) { new Main().test(); } void test(){ MyRunnable runnable = new MyRunnable(); new Thread(runnable,"a").start(); new Thread(runnable,"b").start(); } public class MyRunnable implements Runnable{ int num=0; final String string =""; @Override public void run() { synchronized (string){ for (int i=0;i<5;i++) { ++num; System.out.println(Thread.currentThread().getName() + i+"--------"+num); } } } }}
這樣的執行結果
a0--------1
a1--------2
a2--------3
a3--------4
a4--------5
b0--------6
b1--------7
b2--------8
b3--------9
b4--------10
這樣只有當一個線程中的run()方法執行完之後才會執行另一個
然後把同步鎖的地方變一下
public class Main { public static void main(String[] args) { new Main().test(); } void test() { MyRunnable runnable = new MyRunnable(); new Thread(runnable, "a").start(); new Thread(runnable, "b").start(); } public class MyRunnable implements Runnable { int num = 0; final String string = ""; @Override public void run() { for (int i = 0; i < 5; i++) { synchronized (string) { ++num; System.out.println(Thread.currentThread().getName() + i + "--------" + num); } } } }}
執行結果
a0--------1
b0--------2
b1--------3
b2--------4
b3--------5
b4--------6
a1--------7
a2--------8
a3--------9
a4--------10
可以看到,兩個線程交替執行,但只有一個線程的
++num;System.out.println(Thread.currentThread().getName() + i + "--------" + num);
執行完之後才會執行另一個線程的,所以全域變數的值是依次增加的。
再來看Android中的Handler的post(Runnale)這個方法
public class MainActivity extends AppCompatActivity { Handler handler = new Handler(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); MyRunnable myRunnable = new MyRunnable(); handler.post(myRunnable); } class MyRunnable implements Runnable{ @Override public void run() { for (int i=0;i<5;++i){ System.out.println(Thread.currentThread().getName()+"---"+i); } } }}
可以看到,這個Runnable實在主線程上啟動並執行,因為Handler是在主線程上建立的,與主線程綁定。一些新手可能就會以為這樣是建立了一個線程,把耗時操作放裡邊造成UI卡頓。api說明如下:
Causes the Runnable r to be added to the message queue. The runnable will be run on the thread to which this handler is attached.
那麼我們post兩個呢,代碼修改為
MyRunnable myRunnable = new MyRunnable();handler.post(myRunnable);handler.post(myRunnable);
那相應的結果
可以看到是依次啟動並執行,這也體現了Handler的訊息佇列的思想
再改一下
MyRunnable myRunnable = new MyRunnable();handler.post(myRunnable);handler.post(myRunnable);System.out.println("post finish");
可以看到在post之後並不是立即執行。
那麼如何post一個runnable對象到子線程執行呢?這就要將Handler與一個子線程綁定
public class MainActivity extends AppCompatActivity { HandlerThread thread = new HandlerThread("subThread"); Handler handler = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); thread.start(); handler = new Handler(thread.getLooper()); MyRunnable myRunnable = new MyRunnable(); handler.post(myRunnable); System.out.println("post finish"); } class MyRunnable implements Runnable{ @Override public void run() { for (int i=0;i<5;++i){ System.out.println(Thread.currentThread().getName()+"---"+i); } } }}
執行結果:
android中的Handler和Runnable