我想知道这是否可能以某种方式请 java 7 或 8
public class App{
public static void main(String[] args) {
new Thread(){
public void run(){
//Something
}
}.start().setName("Something") //Here!!
//Something
}
}
不,这是不可能的,因为既不返回start()
也不setName()
返回线程。创建的匿名类是 的子类Thread
,因此可以将其分配给这样的变量:
Thread thread = new Thread {
// something
};
thread.setName(...);
thread.setPriority(...);
thread.start();
或使用功能符号:
Thread thread = new Thread( () -> { ... } );
thread.setName(...);
thread.setPriority(...);
thread.start();
和我的首选(没有创建额外的类),使用方法引用:
Thread thread = new Thread(this::runInThread);
thread.setName(...);
thread.setPriority(...);
thread.start();
...
}
private void runInThread() {
// something to run in thread
}
添加setPriority()
只是为了有更多的电话
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句