在构造之后设置Java Thread的Runnable

CCJ*_*CCJ 5 java multithreading android

JDK 7 Java文档提出了以下两个用于创建Java线程的习惯用法:

  1. 扩展线程并覆盖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();
    
    Run Code Online (Sandbox Code Playgroud)
  2. 实现Runnable并创建一个新的Thread,将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();
    
    Run Code Online (Sandbox Code Playgroud)

这些已经足够了,但是我希望能够创建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();
}

}
Run Code Online (Sandbox Code Playgroud)

我测试了FlexiThread,它似乎按预期工作(它执行我在Runnable impl的run方法中给出的任何代码,在通过DDMS验证的单独执行线程中)至少在Android ICS和JB上; 上面给出的FlexiThread策略是否有任何错误/潜在危险/低效?如果是这样,在构造之后定义Thread子类的Runnable可能是更好的方法吗?

Pet*_*rey 8

我会使用Executor,因为它是可重用和可控制的.

ExecutorService es = Executors.newSingleThreadedPool();

// set a runnable later.
es.submit(new MyRunnable());

// give it another runnable when that finishes.
es.submit(new MyRunnable2());

// don't need it any more
es.shutdown();
Run Code Online (Sandbox Code Playgroud)