The JDK 7 Java documentation offers the following two idioms for creating Java threads:
Extend Thread and override run ()
class PrimeThread extends Thread { long minPrime; PrimeThread(long minPrime) { this.minPrime = minPrime; } public void run() {
Deploy Runnable and create a new thread that passes the Runnable impl to your constructor
class PrimeRun implements Runnable { long minPrime; PrimeRun(long minPrime) { this.minPrime = minPrime; } public void run() {
This is good enough, but I would like to create a subclass of Thread, and then define and install its Runnable implementation after some time (for example, not only in the Thread constructor). From what I can say, the Java Thread class provides no means to achieve this, so I came up with the following:
public class FlexiThread extends Thread{
I tested FlexiThread and it seems to work as expected (it executes any code that I provide in the Runnable impl run method in a separate thread of execution, verified through DDMS), at least on Android ICS and JB; Is there anything wrong / potentially dangerous / ineffective with the FlexiThread strategy above? If so, what could be the best way to define Thread Runable subclasses after building it?
source share