import java.io.IOException; public class ResponseUI extends Thread{ private static volatile double d = 1; public ResponseUI(){ super.setDaemon(true); //设置后台运行 start(); } public void run(){ while(true){ d = d + (Math.E + Math.PI) / d; } } /** * @param args * @throws InterruptedException * @throws IOException */ public static void main(String[] args) throws InterruptedException, IOException { // TODO Auto-generated method stub new ResponseUI(); Thread.sleep(300); System.in.read(); System.out.println(d); } }
这样只要在控制台输入任何东西,都能看到d的值,说明线程确实在后台运行
本文提供了一个Java后台线程的示例代码,通过创建并启动一个后台线程,不断更新一个浮点数变量d的值。主线程等待一段时间后读取该变量,并输出其值,验证了后台线程的正常运行。
3861

被折叠的 条评论
为什么被折叠?



