简体   繁体   English

Thread.sleep导致android程序崩溃

[英]Thread.sleep causes android program to crash

I'm trying to make a program that monitors network connectivity, one that shows statuses and bandwidth and refreshes every second. 我正在尝试制作一个监控网络连接的程序,一个显示状态和带宽并每秒刷新一次的程序。 Yesterday I learned that network monitoring occurs on a secondary thread, which I created; 昨天我了解到网络监控发生在我创建的辅助线程上; it now works. 它现在有效。

I believe to have the program refresh every second, I do a while-loop, in which the while-condition is always "true", and at the end of the while loop I "try" a Thread.sleep(1000). 我相信让程序每秒刷新一次,我做一个while循环,其中while条件总是“true”,而在while循环结束时我“尝试”一个Thread.sleep(1000)。

I have one question and one problem. 我有一个问题和一个问题。

Question: Do I risk flooding my program? 问题:我是否冒着充斥我的计划的风险? I feel like by setting secondaryThread = null, all the data created during the while loop gets garbage collected, but I'm not sure whether this is the case. 我觉得通过设置secondaryThread = null,在while循环期间创建的所有数据都会被垃圾收集,但我不确定是否是这种情况。

Problem: When I run this, I get a message "[Program] has quit unexpectedly"... leading me to think that I am indeed flooding the program. 问题:当我运行这个时,我收到一条消息“[程序]意外退出”......让我觉得我确实充斥了程序。 Is there a way to get around this? 有办法解决这个问题吗?

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    int linkSpeed = -1;
    TextView textView = new TextView(this);
    while (true)
    {
        SecondaryThread secondaryThread = new SecondaryThread(this);
        new Thread(secondaryThread).start();
        linkSpeed = secondaryThread.getLinkSpeed();
        secondaryThread = null;

        // Create the text view
        textView.setTextSize(25);
        textView.setText("linkspeed = " + linkSpeed);

        // Set the text view as the activity layout
        setContentView(textView);
        try {
            Thread.sleep(1000);
        }
        catch (Exception e) {
            textView.setTextSize(25);
            textView.setText("oh shit");
        }
    }

The LogCat trace stack says everything is running fine, even though that's not the actual case. LogCat跟踪堆栈表示一切正常,即使这不是实际情况。 Specifically, it says the following... 具体来说,它说以下......

06-27 15:07:58.069: D/gralloc_goldfish(1312): Emulator without GPU emulation detected.
06-27 15:41:45.879: I/dalvikvm(1919): threadid=3: reacting to signal 3
06-27 15:41:45.958: I/dalvikvm(1919): Wrote stack traces to '/data/anr/traces.txt'

You never want to call Thread.sleep() on the UI Thread which it looks like you are doing here. 你永远不想在UI Thread上调用Thread.sleep() ,它看起来就像你在这里做的那样。 You can put that in your Thread that you have created and use runOnUiThread() to update your TextView 您可以将它放在您创建的Thread ,并使用runOnUiThread()来更新TextView

Here is a SO answer that may help 这是一个可能有帮助的答案

This one also looks like what you are doing 这个看起来也像你在做什么

Never block the UI thread. 永远不要阻止UI线程。 Use background threads: 使用后台线程:

 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    final TextView textView = new TextView(this);

    new AsyncTask<Void, Void, Void>() {

        @Override
        protected Void doInBackground(Void... params) {
            int globalState = 1;
            while (true) {
                // <cut>

                // Create the text view
                // new scope
                {
                final int linkSpeed = ++globalState;
                textView.post(new Runnable() {
                    @Override
                    public void run() {
                        textView.setTextSize(25);
                        textView.setText("linkspeed = " + linkSpeed);

                        // Set the text view as the activity layout
                        setContentView(textView);
                    }
                });
                }

                try {
                    Thread.sleep(1000);
                } catch (Exception e) {
                    textView.post(new Runnable() {
                        @Override
                        public void run() {
                            textView.setTextSize(25);
                            textView.setText("oh XXXX");
                        }
                    });
                }
            }
        }

    }.execute();

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

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