我正在使用一个按钮在我的应用程序中启动后台服务。这是我正在使用的代码:
@Override
public void actionPerformed(ActionEvent action) {
if (action.getActionCommand().equals("Start")) {
while (true) {
new Thread(new Runnable() {
public void run() {
System.out.println("Started");
}
}).start();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
这确实每秒更新一次服务,这正是我想要的。问题是它冻结了应用程序的其余部分。我如何实现它以防止这种情况发生?