https://ptodue.gitbooks.io/book2/content/2.1.2.html
http://ptodue.blogspot.tw/search/label/Runnable
http://ptodue.blogspot.tw/search/label/runOnUiThread
實作Thread常用的使用方法有2種:
- 繼承Thread class,再override實現run() method
- implements Runnable(實作Runnable interface), 再override實現run() method。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MyThread extends Thread { | |
public MyThread() { | |
super("MyThread"); | |
} | |
public void run() { | |
System.out.println("Thread run() method executed."); | |
} | |
} | |
//Started with a "new MyThread().start()" call |
或是簡寫
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Thread t = new Thread() { | |
@Override | |
public void run() { | |
// Block of code to execute | |
// worker thread(background thread),處理需長時間或大量的運算 | |
System.out.println("Thread run() method executed."); | |
} | |
}; | |
t.start(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MyRunnable implements Runnable { | |
public void run() { | |
//Code | |
System.out.println("Runnable run() method executed."); | |
} | |
} | |
//Started with a "new Thread(new MyRunnable()).start()" call |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Runnable r = new Runnable() { | |
@Override | |
public void run() { | |
// Block of code to execute | |
System.out.println("Runnable run() method executed."); | |
} | |
}; | |
Thread t = new Thread(r); | |
t.start(); |
沒有留言:
張貼留言