我有以下的代码片段:
public class A {
public static void main(String[] arg) {
new Thread() {
public void run() {
System.out.println("blah");
}
};
}
}
在这里,我如何调用线程的start()
方法而不创建线程类的实例?
我有以下的代码片段:
public class A {
public static void main(String[] arg) {
new Thread() {
public void run() {
System.out.println("blah");
}
};
}
}
在这里,我如何调用线程的start()
方法而不创建线程类的实例?
您已经创建了Thread类的实例 - 只是没有使用它。您可以甚至不使用本地变量调用start()
:
new Thread()
{
public void run() {
System.out.println("blah");
}
}.start();
...但是个人建议将其分配给本地变量,进行其他任何操作(例如设置名称等),然后启动它:
Thread t = new Thread() {
public void run() {
System.out.println("blah");
}
};
t.start();
Thread
的子类,而在另一个情况下,则是Runnable
的匿名类实现。它们是不同的,但通常差异是无关紧要的。 - Jon Skeet由于匿名类扩展了给定的类,因此您可以将它们存储在变量中。
例如:
Thread t = new Thread()
{
public void run() {
System.out.println("blah");
}
};
t.start();
或者,您可以直接在所创建的对象上调用start方法。
new Thread()
{
public void run() {
System.out.println("blah");
}
}.start();
// similar to new Thread().start();
个人建议始终创建一个匿名的Runnable实例,而不是Thread实例。因为如果您意外地错误地获取方法签名,编译器会发出警告(对于匿名类,它将无论如何发出警告,因为匿名类不能定义新的非私有方法)。
例如:
new Thread(new Runnable()
{
@Override
public void run() {
System.out.println("blah");
}
}).start();
我不确定您的意思,但是您可以尝试以下方法:
new Thread() {
public void run() {
System.out.println("blah");
}
}.start();
注意在匿名类的末尾有start()
方法。您创建了线程对象,但需要启动它才能获得另一个运行中的线程。
比创建匿名Thread
类更好的方法是创建匿名Runnable
类:
new Thread(new Runnable() {
public void run() {
System.out.println("blah");
}
}).start();
与其在Thread
中覆盖run()
方法,不如将一个目标Runnable
注入到新线程中运行。这是一种更好的模式。
新增:现在您可以使用Lambda表达式来简化语法。要求:Java 8或以上版本。
public class A {
public static void main(String[] arg)
{
Thread th = new Thread(() -> {System.out.println("blah");});
th.start();
}
}
只需调用 start() 函数即可。
new Thread()
{
public void run() {
System.out.println("blah");
}
}.start();
new
表达式是一个对象引用,因此可以在其上调用方法:public class A {
public static void main(String[] arg)
{
new Thread()
{
public void run() {
System.out.println("blah");
}
}.start();
}
}
把这个留在这里以备将来参考,但它也是一个答案。
new Thread(() -> whatever()).start();
// This also starts the new thread when instantiating the Anonymous class
// By invoking "this.start()" in a instance initializer block of the Anonymous Thread.
// Note: We can also override the run from the Thread class itself instead if providing external runnable just in case.
Thread t = new Thread(()->{}){{start();}};
Runnable r1 = ()->{}; // Runnable Functional Interface lambda format
Thread t1 = new Thread(r1){{start();}};
// Anonymous class which implements Runnable interface
Runnable r2 = new Runnable(){
@Override
public void run(){
//do some work
}
}
Thread t2 = new Thread(r2){{start();}};
// Anonymous class which extends Thread class
Thread t3 = new Thread(){
{
start();
}
@Override
public void run(){
//do some useful work
}
}
class ExtendedThread extends Thread{
ExtendedThread(){this(null);}
ExtendedThread(Runnable runnable){
super(runnable);
start();
}
@Override
public void run(){
super.run();
//do some more useful work always even if there was no runnable was passed via constructor
}
}
Thread t3 = new Thread();
Runnable
实例将在内部被包装并执行为线程。import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
// ...
ExecutorService threadExecutor = Executors.newSingleThreadExecutor();
try {
threadExecutor.execute(
new Runnable() {
@Override
public void run() {
System.out.println("blah");
}
}
);
} finally {
threadExecutor.shutdownNow();
}
为了方便起见,请查看JDoodle上的代码。
Thread
的实例。 - Pacerier