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的值,说明线程确实在后台运行