JDK 7 Java 文档建议使用以下两个习惯用法来创建 Java 线程:
扩展线程并覆盖 run()
class PrimeThread extends Thread { long minPrime; PrimeThread(long minPrime) { this.minPrime = minPrime; } public void run() { // compute primes larger than minPrime . . . } } ... //And to launch the custom Thread PrimeThread p = new PrimeThread(143); p.start();
实现 Runnable 并创建一个新线程,将 Runnable impl 传递到其构造函数中
class PrimeRun implements Runnable { long minPrime; PrimeRun(long minPrime) { this.minPrime = minPrime; } public void run() { // compute primes larger than minPrime . . . } } ... //And to launch a new Thread with this Runnable's behavior PrimeRun p = new PrimeRun(143); new Thread(p).start();
这些已经足够好了,但我希望能够创建一个 Thread 的子类,然后在稍后的某个时间定义和设置它的 Runnable 实现(例如,不仅仅是在 Thread 的构造函数中)。据我所知,Java 的 Thread 类没有提供实现这一点的方法,所以我想出了以下内容:
public class FlexiThread extends Thread{
//The Runnable impl to be executed
private Runnable mvRunner;
//Construct an empty subclass of Thread
public FlexiThread(){
super();
}
//Construct a subclass of Thread which provides
//its Runnable impl immediately
public FlexiThread(Runnable r){
super(r);
mvRunner = r;
}
/**
*
* @return -- the Runnable implementation whose
* run() method will be invoked when this thread
* is started
*/
public Runnable getRunnableToExecute(){
return mvRunner;
}
/**
* @param runner -- the Runnable implementation whose
* run() method will be invoked when this thread
* is started
*/
public void setRunnableToExecute(Runnable runner){
mvRunner = runner;
}
@Override
public void run(){
mvRunner.run();
}
}
我测试了 FlexiThread,它似乎可以按预期工作(它在通过 DDMS 验证的单独执行线程中执行我在 Runnable impl 的 run 方法中提供的任何代码)至少在 Android ICS 和 JB 上;上面给出的 FlexiThread 策略有什么错误/潜在危险/低效吗?如果是这样,在构造 Thread 子类的 Runnable 之后,有什么更好的方法来定义它?