Runnable Interface : Replace this lambda with a method reference. (sonar.java.source not set. Assuming 8 or greater.)

六月ゝ 毕业季﹏ 提交于 2019-12-11 01:13:22

问题


private void runAsyncImport() {
      Runnable task = () -> runImport(); 
      new Thread(task).start();
}

I am getting sonar issue for the above code, Replace this lambda with a method reference. (sonar.java.source not set. Assuming 8 or greater.)

How to fix it


回答1:


If your class has a non-static runImport() method, then you can write like this:

Runnable task = this::runImport;

If the runImport() method is static, then instead of this, write the name of the class, for example if the name of the class is MyClass, then:

Runnable task = MyClass::runImport;


来源:https://stackoverflow.com/questions/46612287/runnable-interface-replace-this-lambda-with-a-method-reference-sonar-java-so

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!