在Java中,可以使用Thread
类来创建一个单独的线程,并在该线程上运行一个不返回任何内容的方法。以下是一个示例代码:
public class ExampleThread extends Thread {
@Override
public void run() {
// 在这里编写需要在单独线程上运行的任务代码
// 例如,打印一些信息
System.out.println("This is running on a separate thread.");
}
public static void main(String[] args) {
ExampleThread thread = new ExampleThread();
thread.start(); // 启动线程
}
}
在上面的示例中,ExampleThread
类继承自Thread
类,并重写了run
方法。在run
方法中,您可以编写需要在单独线程上运行的任务代码。在main
方法中,我们创建了一个ExampleThread
对象thread
,并通过调用start
方法启动了线程。
当我们运行上述代码时,将会输出This is running on a separate thread.
,这表明线程成功运行,并在单独的线程上执行了run
方法的代码。
上一篇:不返回任何内容的base64解码
下一篇:不返回任何值