Anonymous class extending thread in Java
This is a Java syntax question, but just for some background.
Using android, I created a small app that went really slow and often crashed because when a button was clicked the onClick() method changed button images for various buttons. It was really clunky and slow and I found out after a search on the net that the problem is pretty common, and when changing lots of images in the onClick() method, its best to put them in a separate thread. And some kind person gave the code for doing this.
new Thread(new Runnable() {
public void run() {
quest.post(new Runnable() {
public void run() {
correct = nextQ();
}
});
}
}).start();
The nextQ() method and the "quest" TextView are mine. It's not reall开发者_Go百科y relevant to my question what they do, but nextQ does a database search and updates images, and running it outside the thread is really slow and crashy. Now I copied and pasted this code into my code and it runs fine. A happy ending. But I dont feel comfortable using code I don't understand. And I don't know Java very well. SO I researched as best I could anonymous inner classes, but I'm still stumped by the code.
So the anonymous class extends thread, and as it's argument uses an anonymous class, implementing runnable, in which the "meat" of the code is placed.. Questions: 1)Why would I do this? I can understand using thread OR runnable, but why together? 2)How does this work? I went back to basics of thread and runnable, but I don't see how you use them together this way.
- you are creating an anonymous implementation of the
Runnable
interface - you are passing that implementation to the constructor of the
Thread
class. So you are not extending Thread, you are just instantiating it, and pass an argument. - you then
start()
the thread. Thus therun()
method of the passed argument will be invoked in a new thread.
You need to do this, because:
- the
Runnable
defines what gets executed - the
Thread
actually executes it.
Your confusion is understandable, since a Thread
is also Runnable
. That's why in Java 5 the executors framework was introduces, which separated the execution mechanism (an executor) from the executed code (Runnable
or Callable
).
So the anonymous class extends thread,
No it does not. It implements Runnable.
1)Why would I do this? I can understand using thread OR runnable, but why together?
If you use Runnable, you still need to run it somehow, and one way to do that is to create a new Thread and give it to Runnable (which is what is being done here).
2)How does this work?
new Thread(runnable)
creates a new Thread, which you can then start(). It will execute the code in the Runnable's run() method.
Runnable
contains the code that should be executed (an alternative is Callable
).
Thread
controls how and when the code is executed (alternatives are ExecutorService
or calling run() directly for synchronous execution on the current Thread)
In general (and to keep these two functions apart), you do not want to extend Thread, you only want to implement Runnable or Callable.
It is possible to extend Thread and directly implement run (rather than letting it delegate to the Runnable), but that is sort of old school. If they were to redesign the Java API today, Thread would probably not implement Runnable anymore. Thread is more of a Runner than it is a Runnable.
精彩评论