相关疑难解决方法(0)

Java创建后台线程,它定期执行某些操作

是否有可能创建一个单独的后台线程,它将分别做一些东西?我已经尝试了以下程序,但它没有像我期望的那样工作.

public class Test {

    private static class UpdaterThread extends Thread {
        private final int TIMEOUT = 3000;

        public void run() {
            while (true) {
                try {
                    Thread.sleep(TIMEOUT);
                    System.out.println("3 seconds passed");
                } catch (InterruptedException ex) {
                }
            }
        }
    }

    /**
     * @param args
     *            the command line arguments
     */
    public static void main(String[] args) {
        try {
            Thread u = new UpdaterThread();
            u.start();
            while (true) {
                System.out.println("--");
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我期望每3秒"3秒钟过去"将打印在多个" - …

java multithreading

10
推荐指数
2
解决办法
2万
查看次数

标签 统计

java ×1

multithreading ×1