简体   繁体   中英

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).

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.

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. 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. You can put that in your Thread that you have created and use runOnUiThread() to update your TextView

Here is a SO answer that may help

This one also looks like what you are doing

Never block the UI thread. 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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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