converting to ScheduledThreadPoolExecutor

不想你离开。 提交于 2019-11-30 05:26:27

Replace

Timer timer = new Timer();

with

ScheduledExecutorService service = Executors.newSingleThreadScheduledExecutor();

and

class Task extends TimerTask

with

class Task implements Runnable

and

timer.scheduleAtFixedRate(new Task(), 0, 1000);

with

service.scheduleAtFixedRate(new Task(), 0, 1000, TimeUnit.MILLISECONDS);

BTW You should not be attempting to update the GUI on another thread. Instead you have to add a task to the Swing GUI Thread to perform the task

    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            textOut.setText("" + i++);
        }
    });
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!