简体   繁体   English

Eclipse自动补全功能可运行,但不适用于线程

[英]Eclipse autocompletion for Runnable working but not for Thread

If I type "new Runnable" and hit CTRL+SPACE, Eclipse allows me to create an anonymous inner class. 如果输入“ new Runnable”并按CTRL + SPACE,则Eclipse允许我创建一个匿名内部类。 However when I do the same with "Thread", it does not. 但是,当我对“线程”执行相同操作时,事实并非如此。

It is a bit annoying that every time I need an anonymous thread, I have to create a new Runnable and then change the "Runnable" to "Thread". 每次我需要一个匿名线程时,都必须创建一个新的Runnable,然后将“ Runnable”更改为“ Thread”,这有点令人讨厌。

Is there a way to let Eclipse automatically create a thread as well? 有没有办法让Eclipse也自动创建线程?

I think Eclipse is doing this because Thread is a concrete class while Runnable is instead of an interface. 我认为Eclipse之所以这样做是因为Thread是一个具体的类,而Runnable则是一个接口。 Eclipse may not by default allow you to create an anonymous class which extends a concreate one. Eclipse默认情况下可能不允许您创建扩展concreate的匿名类。

The more accepted way to create a Thread is to use a Runnable anyway which does not restrict you to the Thread base class: 创建Thread一种更广为接受的方法是使用Runnable ,但这并不限制您使用Thread基类:

 Thread thread = new Thread(new Runnable() {
     public void run() {
        ...
     }
 });
 thread.start();
 ...

As a workaround you could create a new template for the thread creation. 解决方法是,可以为线程创建创建新模板

Eclise (at least in 3.7) alreay has one for Runnable. 埃克利丝(至少在3.7中)盟军有一个Runnable。 You can try it if you type runnable and then Ctrl+Space until the template proposals are shown. 如果您键入runnable ,然后Ctrl+Space直到显示模板建议,您可以尝试一下。

To create a new template for Thread : Thread创建一个新模板:

  • Open the template view Window > Show View > Templates 打开模板视图Window > Show View > Templates
  • Select runnable 选择runnable
  • Copy/Paste from the context menu 从上下文菜单复制/粘贴
  • Edit the new Template by replacing Runnable with Thread 通过将Runnable替换为Thread编辑新模板

It does come. 确实来了。 I am not sure how you're pressing Ctrl + Space . 我不确定您如何按Ctrl + Space

在此输入图像描述

Click "New" on this panel and create a new template. 单击此面板上的“新建”,然后创建一个新模板。 Ctrl-space will then work as you expect. Ctrl-space将按预期工作。

在此输入图像描述

Yes. 是。 Avoid anonymous inner classes. 避免使用匿名内部类。 You need thread? 您需要线程吗? It does something important? 它有重要的作用吗? Create normal class that extends thread and implements run() . 创建扩展线程并实现run()普通类。

Moreover. 此外。 Avoid creating thread instances whenever you want. 避免在需要时创建线程实例。 User thread pools, executors etc instead. 用户线程池,执行程序等。

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

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