简体   繁体   English

Java中的匿名类扩展线程

[英]Anonymous class extending thread in Java

This is a Java syntax question, but just for some background. 这是一个Java语法问题,但仅出于某些背景。

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. 使用android,我创建了一个很小的应用程序,运行非常缓慢,并且经常崩溃,因为单击按钮时,onClick()方法更改了各种按钮的按钮图像。 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. 这确实很笨拙而且很慢,我在网上搜索后发现问题很普遍,并且在onClick()方法中更改许多图像时,最好将它们放在单独的线程中。 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. nextQ()方法和“ quest” TextView是我的。 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. 这与我的问题做什么无关紧要,但是nextQ进行数据库搜索并更新图像,并且在线程外运行它确实很慢且很崩溃。 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. 而且我不太了解Java。 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? 因此,匿名类扩展了线程,并且它的参数使用了一个匿名类,实现了可运行的,其中放置了代码的“肉”。问题:1)为什么要这样做? I can understand using thread OR runnable, but why together? 我可以理解使用线程OR可运行,但是为什么要一起使用呢? 2)How does this work? 2)如何运作? 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 您正在创建Runnable接口的匿名实现
  • you are passing that implementation to the constructor of the Thread class. 您正在将该实现传递给Thread类的构造函数。 So you are not extending Thread, you are just instantiating it, and pass an argument. 因此,您没有扩展Thread,只是实例化了它,并传递了一个参数。
  • you then start() the thread. 然后,您可以start()线程。 Thus the run() method of the passed argument will be invoked in a new thread. 因此,传递的参数的run()方法将在新线程中调用。

You need to do this, because: 您需要这样做,因为:

  • the Runnable defines what gets executed Runnable定义执行什么
  • the Thread actually executes it. Thread实际执行它。

Your confusion is understandable, since a Thread is also Runnable . 您的困惑是可以理解的,因为Thread也是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 5中引入了执行程序框架的原因,该框架将执行机制(执行程序)与已执行的代码( RunnableCallable )分开。

So the anonymous class extends thread, 因此,匿名类扩展了线程,

No it does not. 不,不是的。 It implements Runnable. 它实现了Runnable。

1)Why would I do this? 1)我为什么要这样做? I can understand using thread OR runnable, but why together? 我可以理解使用线程OR可运行,但是为什么要一起使用呢?

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). 如果使用Runnable,则仍然需要以某种方式运行它,一种方法是创建一个新线程并将其提供给Runnable(在此完成)。

2)How does this work? 2)如何运作?

new Thread(runnable) creates a new Thread, which you can then start(). new Thread(runnable)创建一个新线程,然后您可以启动()。 It will execute the code in the Runnable's run() method. 它将在Runnable的run()方法中执行代码。

Runnable contains the code that should be executed (an alternative is Callable ). Runnable包含应执行的代码(一种替代方法是Callable )。

Thread controls how and when the code is executed (alternatives are ExecutorService or calling run() directly for synchronous execution on the current Thread) Thread控制代码的执行方式和时间(替代方法是ExecutorService或直接调用run()以便在当前线程上同步执行)

In general (and to keep these two functions apart), you do not want to extend Thread, you only want to implement Runnable or Callable. 通常,(为了将这两个功能区分开),您不想扩展Thread,而只想实现Runnable或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. 可以扩展Thread并直接实现run(而不是让它委托给Runnable),但这有点老套了。 If they were to redesign the Java API today, Thread would probably not implement Runnable anymore. 如果他们今天要重新设计Java API,则Thread可能不再实现Runnable。 Thread is more of a Runner than it is a Runnable. 线程与其说是可运行的,不如说是奔跑者。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM