2

In my code I have to send a message as long as my ToggleButton is checked. To prevent the UI Thread from freezing, I put the action in a seperate Thread.

My Problem is, that it still freezes, but I don't know why

This is the relevant code:

private ToggleButton.OnClickListener lightMirrorOnClickListener = new ToggleButton.OnClickListener() {

    @Override
    public void onClick(View v) {
        if (lightMirrorBtn.isChecked()) {
            lightThread = new LightThread();
            lightThread.start();
        } else if(!lightMirrorBtn.isChecked()) {
            lightThread.interrupt();
        }
    }

};

class LightThread extends Thread {

    Handler lightHandler = new Handler();

    Runnable light = new Runnable() {
        public void run() {

            while (lightMirrorBtn.isChecked()) {
                lightTxMsg.frameFormat = ConstantList.STANDARD_FRAME;
                lightTxMsg.frameType = ConstantList.DATA_FRAME;
                lightTxMsg.dataLength = (byte) 8;
                lightTxMsg.messageID = 0x3C1;
                int[] messageArray = AMBI_LIGHT;
                for (int i = 0; i < lightTxMsg.dataLength; i++) {
                    lightTxMsg.data[i] = messageArray[i];
                }

                returnCode = demoController.transmitMessage(lightTxMsg,
                        ConstantList.BINARY_FORMAT);    
            }
        }
    };

    public void run() {
        while (!isInterrupted()) {
            try {
                Thread.sleep(60);
                lightHandler.post(light);

            } catch (InterruptedException e) {
                break;
            }
        }

    }

}

EDIT: This was the solution for the problem:

private ToggleButton.OnCheckedChangeListener lightMirrorOnClickListener = new ToggleButton.OnCheckedChangeListener() {

    @Override
    public void onCheckedChanged(CompoundButton buttonView,
            boolean isChecked) {
        if (isChecked == true) {
            new Thread(new Runnable() {
                public void run() {
                    lightTxMsg.frameFormat = ConstantList.STANDARD_FRAME;
                    lightTxMsg.frameType = ConstantList.DATA_FRAME;
                    lightTxMsg.dataLength = (byte) 8;
                    lightTxMsg.messageID = 0x3C1;
                    int[] messageArray = AMBI_LIGHT_ON;
                    for (int i = 0; i < lightTxMsg.dataLength; i++) {
                        lightTxMsg.data[i] = messageArray[i];
                    }

                    returnCode = demoController.transmitMessage(lightTxMsg,
                            ConstantList.BINARY_FORMAT);
                }
            }).start();

        }  else if (!isChecked) {
            new Thread(new Runnable() {
                public void run() {
                    lightTxMsg.frameFormat = ConstantList.STANDARD_FRAME;
                    lightTxMsg.frameType = ConstantList.DATA_FRAME;
                    lightTxMsg.dataLength = (byte) 8;
                    lightTxMsg.messageID = 0x3C1;
                    int[] messageArray = AMBI_LIGHT_OFF;
                    for (int i = 0; i < lightTxMsg.dataLength; i++) {
                        lightTxMsg.data[i] = messageArray[i];
                    }

                    returnCode = demoController.transmitMessage(lightTxMsg,
                            ConstantList.BINARY_FORMAT);
                }
            }).start();
        }
    }

};
6
  • I suggest you check some tutorial about multi-threading in Java, you have several mistakes in there. For example extend Thread and create a Runnable inside it is redundant, a thread is already a Runnable.
    – m0skit0
    Commented Mar 24, 2014 at 15:31
  • could you maybe name some of them, so its easier to do research ?
    – Fraggles
    Commented Mar 24, 2014 at 15:32
  • Sorry, this isn't a place to learn but to ask specific questions. You shouldn't even attempt to write multi-threading code without reading how first. I can point to Oracle's offical tutorials, they're a good place to start.
    – m0skit0
    Commented Mar 24, 2014 at 15:34
  • Btw when it does freeze? When it's checked or not?
    – m0skit0
    Commented Mar 24, 2014 at 15:36
  • Is freezes when its checked
    – Fraggles
    Commented Mar 24, 2014 at 15:36

1 Answer 1

4
 Handler lightHandler = new Handler();

When you create your handler your thread has not yet started. It is just being created. So, according to the Handler's default constructor documentation this handler is associated "with the Looper for the current thread" ... which is currently the main(UI) thread. So you post your messages on the main thread.

You don't need a Handler to post your runnable on. You can either:

  1. Create a Thread and specify it's actions in the run() method or
  2. Pass a Runnable to your thread that will be executed in your thread using the Thread(Runnable) constructor

Here are the basic articles about Threads:

  1. Processes and threads
  2. Keeping your app responsive
  3. Specifying the Code to Run on a Thread
9
  • Good information, thx! But I'm wondering, why this only is a problem on this Thread. On all my other Threads I create a new Handler object at the same position
    – Fraggles
    Commented Mar 24, 2014 at 15:44
  • I don't know what are your other Threads doing. This one has a loop that runs while while lightMirrorBtn.isChecked(). And it is also sleeping for 60ms. Do you have those long-running actions in the other threads? Are you sure that they (and their handler) are also created in the UI thread (in onClick, for example)?
    – stan0
    Commented Mar 24, 2014 at 15:49
  • I don't think this is the issue. A Handler can only be instantiated in UIThread (main thread), or a thread that has called Looper.prepare(), which AFAIK you can't call manually. And no, Handler#post (and family) does not run in the UIThread.
    – m0skit0
    Commented Mar 24, 2014 at 15:50
  • 2
    Sorry, you're right, as the documentation says about Handler#post: "The runnable will be run on the thread to which this handler is attached".
    – m0skit0
    Commented Mar 24, 2014 at 15:58
  • 1
    @Fraggles - try to follow the guides and simply create a new Thread class that has its actions inside its run() method. Then simply start the thread. Don't make the things complicated with Handlers.
    – stan0
    Commented Mar 24, 2014 at 16:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.