在调用读者线程后,GUI被挂起了

时间:2018-03-14 06:51:49

标签: java multithreading jbutton telnet flush

通过Jbutton调用读者线程进行Telnet客户端读取操作后,GUI被挂起了。

Telnet读写操作:

public class Telnet {
static TelnetClient telnet;
    public static void halt() {

    telnet = new TelnetClient();

    try {
        telnet.connect("000.000.0.000", 4444);
        String cmd = "halt \r";
        telnet.getOutputStream().write(cmd.getBytes());
        } catch{}

   readWrite(telnet.getInputStream(), telnet.getOutputStream(),
            System.in, System.out);

    try {
        telnet.disconnect(); 
    } catch {}
}

public static final void readWrite(final InputStream remoteInput,
                                   final OutputStream remoteOutput,
                                   final InputStream localInput,
                                   final OutputStream localOutput)
{
    Thread reader, writer;

    reader = new Thread()
    {
        @Override
        public void run()
        {
            int ch;
            try
            {
                while (!interrupted() && (ch = localInput.read()) != -1)
                {
                    System.out.println("!interrupted() && (ch = localInput.read()) != -1");
                    remoteOutput.write(ch);
                    System.out.println("remote output write ch ");
                    remoteOutput.flush();
                    System.out.println("flushed");  
                }
            }catch{}
        }
    };
    writer = new Thread()
    {
        @Override
        public void run()
        {
            try
            {
                Util.copyStream(remoteInput, localOutput);

            }
            catch {}
        }
    };
    writer.setPriority(Thread.currentThread().getPriority() + 1);
    writer.start();
    reader.setDaemon(true);
    reader.start();
    try
    {
        writer.join();
        reader.interrupt();
    }
    catch {}
}

}

GUI代码:

private void haltPanel() throws Exception {

    halt = new JButton("HALT");
    halt.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent arg0) {
            try{
                    Telnet.halt();
                }catch{}
        }
    });
}

我觉得读者线程正在等待中断而不是在while循环中出现(!interrupted()&&(ch = localInput.read())!= -1)。读完Jbutton的输入后怎么走出循环? 这些是我在GUI挂起后得到的打印件。

!interrupted() && (ch = localInput.read()) != -1
remote output write ch 
flushed
!interrupted() && (ch = localInput.read()) != -1
remote output write ch 
flushed

请帮我解决这个问题,并提前致谢。

1 个答案:

答案 0 :(得分:0)

我同意Scary Wombat:始终记录,或者至少打印您的例外情况。否则你永远不会知道出了什么问题。

无论如何,您可以在后台线程中运行长时间运行的进程。拥有ExecutorService为您处理Threads会更好。最佳选择是为您的应用设置一个ExecutorService,即一个Thread池。通过这种方式,您的应用程序可以通过线程实现高效。

以下是一个示例代码:

// this could go into a global utility class
// or injected wherever needed
final ExecutorService executor = Executors.newCachedThreadPool();

halt.addActionListener(new ActionListener() {

    @Override
    public void actionPerformed(ActionEvent e) {
        // () -> {} is a shorthand for new Runnable() {...}
        executor.submit(() -> {
            try {
                Telnet.halt();
            } catch (Exception ex) {
                // catch the exception and print it
                ex.printStackTrace();
            }
        });
    }

};