狀況描述 :
請問一下各位,
以下程式碼是我聊天室Client端的Thread, 功能是當接收到Server發出客戶列表訊息,
則刷新UI更新名單 ~
我想請問一下, 假設現在Client要關閉UI退出程式, 那要如何關閉ClientConnectThread
(Thread.stop(因不安全所以不使用)),我目前想到的做法是 Client 通知 Server關閉,
Server收到後再返回一個訊息, 這時ClientConnectThread接收到訊息後
則break離開迴圈,Thread停止 .
但我覺得這方法不太好, 因為還要等待Server的回覆
疑問 : 有沒有辦法當我關閉UI, 就直接停止ClientConnectThread ?
public class ClientConnectThread extends Thread
{
    public void run()
    {
        try {
            while(true) {
                byte[] buf = new byte[5000];
                int length = is.read(buf);
                String xml = new String(buf, 0, length);
                int type  = Integer.parseInt(XMLUtil.extracType(xml));
                if( type == CharacterUtil.USERT_LIST ) {
                    List<String> list = XMLUtil.extractUserList(xml);
                    String users = "";
                    for(String user : list) {
                        users += user + "\n";
                    }
                    this.chatClient.getJTextArea1().setText(users);
                }
            }
        } catch (Exception ex) {}
}