简体   繁体   English

Java线程似乎无法正常运行

[英]Java Thread doesn't seem to run properly

This is the first time i'm asking a question here at stack overflow so bear with me. 这是我第一次在堆栈溢出时在这里问一个问题,所以请多多包涵。 I'll try to keep this brief. 我会尽量保持简短。

I'm writing a simple client socket android application. 我正在编写一个简单的客户端套接字android应用程序。 The server I'm connecting to has a chat robot named Alice. 我连接到的服务器有一个名为Alice的聊天机器人。 I have managed to connect to the server and I receive the message "Hello from alice" but then the thread seems to stop because I'm not receiving any more messages. 我已经设法连接到服务器,并且收到消息“来自alice的问候”,但是该线程似乎停止了,因为我没有再收到任何消息。

Here is some code: 这是一些代码:

    @Override
public void run() {

    try 
    {
        while (true)
        {
        String _input = _rd.readLine();
        if (_input != null)
        {
        _field.append("Alice : "+ _input+"\n");
        }
        else
        {
            _field.append("null");
        }
            sleep(50);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
     catch (InterruptedException e) {
        e.printStackTrace();
    }
}

As you can see I have attempted to troubleshoot my code by appending "null" even if I don't receive a message, however this is neither shown in my EditText. 如您所见,即使我没有收到消息,我也尝试通过附加“ null”来对代码进行故障排除,但这在EditText中均未显示。

I call .start() from my main Activity class when the user presses a button. 当用户按下按钮时,我从主Activity类调用.start()

        @Override
        public void onClick(View arg0) {

            setContentView(R.layout.second);
            InitiateSecondFrame();  

            _cs = new ClientSocket();
            String _check = _cs.EstablishConnection(_host.getText().toString(),
                    Integer.parseInt(_port.getText().toString()),
                    (EditText)findViewById(R.id.historyField));

            _cs.start();

            Toast.makeText(getApplicationContext(), _check, 100).show();
            SaveHostPort(_host.getText().toString(), _port.getText().toString());                           
        }
    }); 

The ClientSocket class is extended by Thread and implements Runnable, I've also tried just extending or implementing. ClientSocket类由Thread扩展并实现Runnable,我也尝试过扩展或实现。 I do not get any error message at all. 我根本没有收到任何错误消息。 I hope the information I have given is enough for you to realize what's wrong. 希望我所提供的信息足以让您意识到问题所在。 Remember I'm new at multithreading and sockets so I might have missed something fundamental. 请记住,我是多线程和套接字的新手,所以我可能错过了一些基本知识。

If it helps, here's how you start a thread: 如果有帮助,请按以下步骤启动线程:

Runnable runnable = new MyRunnableClass(); // has the run() method
new Thread(runnable).start();
_rd.readLine();

is your problem here. 这是你的问题。 It will block and wait forever for input from the remote system, and will only return the content received when it detects a newline character from the remote system. 它将阻止并永远等待来自远程系统的输入,并且仅当它从远程系统检测到换行符时才返回接收到的内容。

The thread is not stopped. 线程未停止。 It's just waiting for a message to come. 它只是在等待消息的到来。 _rd.readLine() will block until some line (ending with a newline char) is read, or until the connection ends. _rd.readLine()将阻塞,直到读取了某行(以换行符char结尾)或连接结束为止。 So If Alice keeps the connection open and doesn't send anything, readLine will block forever, and your "null" messages won't ever be printed. 因此,如果Alice保持连接打开并且不发送任何内容,则readLine将永远被阻止,并且您的“ null”消息将永远不会被打印。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM