Java中的匿名类扩展线程 [英] Anonymous class extending thread in Java
问题描述
这是Java语法问题,但仅出于某些背景.
This is a Java syntax question, but just for some background.
使用android,我创建了一个很小的应用程序,运行非常缓慢,并且经常崩溃,因为单击按钮时,onClick()方法更改了各种按钮的按钮图像.这确实很笨拙而且很慢,我在网上搜索后发现问题很普遍,并且当在onClick()方法中更改许多图像时,最好将它们放在单独的线程中.还有一个好心的人为此提供了代码.
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();
nextQ()方法和"quest" TextView是我的.这与我的问题做什么无关紧要,但是nextQ进行数据库搜索并更新图像,并且在线程外运行它确实很慢且很麻烦. 现在,我将此代码复制并粘贴到我的代码中,并且运行良好.一个快乐的结局.但是我不习惯使用我不理解的代码.而且我不太了解Java.因此,我尽了最大的努力研究匿名内部类,但是我仍然对代码感到困惑.
The nextQ() method and the "quest" TextView are mine. It's not really 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.
因此,匿名类扩展了线程,并且它的参数使用了一个匿名类,实现了可运行的,其中放置了代码的肉". 问题: 1)我为什么要这样做?我可以理解使用线程OR可运行,但是为什么要一起使用呢? 2)如何运作?我回到了线程和可运行的基础知识,但是我看不到您如何以这种方式一起使用它们.
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.
推荐答案
- 您正在创建
Runnable
接口的匿名实现 - 您正在将该实现传递给
Thread
类的构造函数.因此,您无需扩展Thread,而只需实例化它,并传递一个参数即可.
然后 - 您
start()
线程.因此,传递的参数的run()
方法将在新线程中调用. - 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. -
Runnable
定义执行的内容 -
Thread
实际上执行它. - the
Runnable
defines what gets executed - the
Thread
actually executes it.
您需要这样做,因为:
您的困惑是可以理解的,因为Thread
也是Runnable
.这就是为什么在Java 5中执行器框架被引入,它将执行机制(执行程序)与执行的代码(Runnable
或Callable
)分开.
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
).
这篇关于Java中的匿名类扩展线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!