いくつかのことを別々に行う別のバックグラウンドスレッドを作成することは可能ですか?次のプログラムを試しましたが、期待どおりに動作しません。
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();
}
}
}
3秒ごとに「3秒経過」が複数の「-」文字列の流れで出力されることを期待していました。実際、「3秒経過」は印刷されません。なんで?また、メインスレッドから独立して何かを実行するバックグラウンドスレッドを作成するにはどうすればよいですか?