线程只循环一次

时间:2011-11-06 15:29:23

标签: android multithreading runnable

我一直在考虑这个问题几个小时,而且我没有接近解决方案! 当我出于某种原因从服务器获取消息时,我的线程只是停止循环,并且在我不这样做时工作正常。

这样可以每秒刷新一次:

 public class ChatRoom extends Activity implements OnClickListener, Runnable {
        private Thread t = new Thread(this);

        public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.chatroom);

                Button send = (Button) findViewById(R.id.send);
                send.setOnClickListener(this);

                Intent receiver = getIntent();
                String host = receiver.getStringExtra("Host");
                int port = receiver.getIntExtra("Port", 4456);


                try
                {
                    socket = new Socket(host, port);
                    this.receive = new BufferedReader(new InputStreamReader(this.socket.getInputStream()));
                    this.send = new PrintWriter(this.socket.getOutputStream(), true);

                } 
                catch(IOException ioe) { System.out.println(ioe); }
                t.start();


            }

            public void run()
            {
                String message = "";

                while(true)
                {
                    try
                    {
                        // message = receive.readLine(); BufferedReader
                        t.sleep(1000);
                    } 
                    //catch(IOException ioe)            { System.out.println(ioe); }
                    catch (NullPointerException npe)    { System.out.println(npe); } 
                    catch (InterruptedException e)      { System.out.println(e);   }

                    System.out.println("Refreshing...");

                }
            }

当我使用我的评论代码时,它实际上工作,我从服务器收到一条消息,但它只循环一次!这是为什么?

Output:
Server Message
Refreshing...

我没有得到异常或错误,但我之前有一些类似的代码表示我无法更改其他线程上的UI。所以我一直在看一些runOnUiThread,但它没有让它变得更好,我不知道为什么它应该:(

3 个答案:

答案 0 :(得分:3)

方法BufferedReader.readLine()会阻塞,直到收到换行符。如果您的接收器流中没有换行符,它将永久阻止。

答案 1 :(得分:3)

这里有一些事情:

  • 从System.out.println(“string”)交换; to Log.d(“tagname”,“string”);然后在DDMS上查找输出行。

我认为你没有正确地创建一个线程,你肯定没有提供任何杀死它的接口,这可能会在你测试它时引起问题。我会将线程分成一个新文件,比如NameOfThread

//File "NameOfThread"
public class NameOfThread extends Thread{
//any fields you want here to mess with e.g.
private String message;
private boolean running;


public NameOfThread(){
    message = "";
    running = true;
}

@Override
public void run(){
    while(running){
        //do stuff
    }   
}

public void setRunning(boolean run){
    running = run;
}   

}

//When you want to call it
NameOfThread varThread = new NameOfThread();
varThread.start();

//when you want to kill the thread
varThread.setRunning(false);

你可能会想'为什么要担心这整个running变量垃圾,我不需要它。但这个线程怎么会优雅地结束呢?还有另一种正确杀死线程的方法,即使用InterruptedException并使用清理代码,但这只是一种替代方法。

首先尝试这样做,然后你需要整理邮件本身(你当前使用的方法不是很好,因为readLine()会阻塞直到收到一行(意味着你会得到)当你获得一条新线而不是每秒一次时,“刷新......”。

答案 2 :(得分:0)

你肯定会抛出一些异常,你只是看不到它们,因为你试图在标准输出上打印它们,这在Android上是不存在的。您的异常处理正确并且代码完成。要正确获取异常信息,请使用Logs,或者只是抛出RuntimeException。希望这会有所帮助。