简体   繁体   English

Java,您怎么称呼它? 以及为什么)};

[英]Java, what do you call this? and why )};

I am going through Hello Android (Android PDF/tutorial) and have now seen this syntax a couple of times. 我正在研究Hello Android(Android PDF /教程),现在已经看过几次这种语法了。 Can someone please explain to me what Java syntax is used when run Runnable is defined? 有人可以告诉我定义运行Runnable时使用什么Java语法吗?

private class AndroidBridge {

    public void callAndroid(final String arg) { // must be final
        handler.post(new Runnable() {
            public void run() {
                Log.d(TAG, "callAndroid(" + arg + ")" );
                textView.setText(arg);
            }
            ...

Is the code defining a Runnable object and overriding it's run method? 代码是否定义了Runnable对象并覆盖了它的run方法?

The .post method expects a Runnable object, which in your code sample is declared anonymously and passed as the argument. .post方法需要一个Runnable对象,该对象在您的代码示例中被匿名声明并作为参数传递。

That will start a new thread for some long-running process. 这将为长时间运行的进程启动一个新线程。

The thread constructor needs a Runnable object, which has a run method that's called when the thread is ready. 线程构造函数需要一个Runnable对象,该对象具有一个run方法,该方法在线程准备就绪时被调用。

When many Java apps start, all of the operations pile up on one thread, including the UI. 当许多Java应用程序启动时,所有操作(包括UI)都堆积在一个线程上。 I mainly use threads to avoid freezing up the UI if I'm doing something "heavy". 如果执行“繁重”操作,我主要使用线程来避免冻结UI。

You've seen this happen when you click "execute" or something, and the UI suddenly is less than responsive. 您已经看到了这种情况,当您单击“执行”之类的东西时,UI突然变得反应迟钝。 This is because the current thread doesn't have enough resources to build the UI and do whatever "execute" is asking. 这是因为当前线程没有足够的资源来构建UI并执行“执行”所要求的任何操作。

So, sometimes that's done elsewhere, on a different thread, which needs a Runnable object. 因此,有时这是在其他线程上的其他地方完成的,这需要一个Runnable对象。

It's worth noting that multithreading (where you make more than one thread on purpose) is notoriously difficult to work with, for debugging reasons mostly, IMO. 值得注意的是,众所周知,多线程(您故意在其中创建多个线程)很难使用,主要是出于调试原因,IMO。 But it is a useful tool, of course. 但这当然是有用的工具。

As Dave Newton indicated, this is an anonymous inner class implementing the Runnable interface. 正如Dave Newton所指出的,这是一个实现Runnable接口的匿名内部类。

As to why one would want to use this, it could be thought of as syntactic sugar of sorts. 至于为什么要使用它,可以将其视为各种语法糖。 You'll notice that in your example, the code in run() has access to the same scope as where the anonymous inner class itself is defined. 您会注意到,在您的示例中, run()的代码run()访问与定义匿名内部类本身相同的作用域。

This simplifies access to those members, as if you defined the class externally, you'd have to pass in a reference to any object whose members you wanted to invoke/use. 这简化了对这些成员的访问,就好像您在外部定义该类一样,您必须传递对要调用/使用其成员的任何对象的引用。

In fact, IIRC, this is actually what happens when Java compiles the anonymous inner class; 实际上,IIRC实际上就是Java编译匿名内部类时发生的情况。 if there are references to the outer containing class, the compiler will create a constructor that passes in a reference to the outer containing class. 如果存在对外部包含类的引用,则编译器将创建一个构造函数,该构造函数传入对外部包含类的引用。

该代码定义了一个匿名内部类,该类实现了Runnable接口,并实现了run方法以执行适当的操作。

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

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