【问题标题】:GUI is getting hanged after calling the reader thread调用阅读器线程后 GUI 被挂起
【发布时间】:2018-08-22 14:31:43
【问题描述】:

通过 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 {}
}

}

界面代码:

private void haltPanel() throws Exception {

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

我觉得阅读器线程正在等待中断并且没有退出 while 循环 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

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

【问题讨论】:

  • a) 从不默默吞下异常} catch{}
  • 你不能调用线程。

标签: java multithreading jbutton telnet flush


【解决方案1】:

我同意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();
            }
        });
    }

};

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多