在一定时间后在SwingWorker中循环时终止BufferedReader

时间:2013-06-18 14:34:37

标签: java swing while-loop bufferedreader swingworker

我有一个SwingWorker,它应该一个接一个地顺序执行3个方法。方法1包含BufferedReader的while循环。这个while循环永远不会终止,因为BufferedReader永远不会用结尾发出信号。我希望在5秒后使用某种计时器终止循环。我该怎么做?

    public class TestWorker extends SwingWorker<Void,Void>{
        public TestWorker(){
        }

        private void doSomething1(){
            TelnetClient telnetClient
            telnetClient = new TelnetClient();
            telnetClient.connect(telnet_IPAddress, telnet_Port);

            InputStream telnetInputStream = telnetClient.getInputStream();

            try (BufferedReader br = new BufferedReader(new InputStreamReader(telnetInputStream))) {
                String line;
                while ((line = br.readLine()) != null) {
                    //Do something
                    //The bufferedreader is not signaled with an end of a stream. I want to terminate this loop after 5 seconds.
                }
            } catch (IOException ex) {
            }
        }

        private void doSomething2(){
        }

        private void doSomething3(){
        }

        @Override
        public Void doInBackground() {
            doSomething1();
            doSomething2();
            doSomething3();
            return null;
        }

        @Override
        public void done() {
            //Update something on a GUI
        }
    }

0 个答案:

没有答案