multithreading problems with atomisboolean

90 views Asked by At

while trying to "develop" a code to drive a robot using an android device, I've been stranded on a stupid issue and after looking to solve it for weeks, I will finally ask you for help. I'm totally new to android and also to java. I'm basically using the bluetoothchat example just changing the main layout drawing a remote with 4 arrows. In order to use those arrows, I've implemented a function which should send a Byte every 300ms as long as the given imageButton is pressed. see following code and don't be too angry if it is a mess (I'm beginner):

private void OnTouch(ImageButton IB , final String str){

    IB.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, final MotionEvent ev){

             Thread t = new Thread() {

                public void run() {
                try{
                    if(ev.getAction()== MotionEvent.ACTION_DOWN){
                        a.set(false);
                        Log.e(TAG, "a set to false");
                    }
                    if(ev.getAction()== MotionEvent.ACTION_MOVE){
                        a.compareAndSet(true, false);
                    }
                    if(ev.getAction() == MotionEvent.ACTION_UP){
                        Log.e(TAG, "actionUp");
                        a.set(true);
                        Log.e(TAG, "a set to true");
                    }
                }
                finally{}
                }
            };

            t.start();
            while(!a.get()){

                    sendMessage(str);

                    Log.e(TAG, "message sent");
                    try {
                        Thread.sleep(300);
                        Log.e(TAG, "dodo");
                    } catch (InterruptedException e) {
                        Log.e(TAG, "error thread.sleep");
                        e.printStackTrace();
                    }
                }

            if(a.get())
            {
                mOutStringBuffer.setLength(0);
                Log.e(TAG, "StringBuffer=0");
                sendMessage(Stop);  
                Log.e(TAG, "S sent");
            }

            t.stop();
            Log.e(TAG, "t.stop and return");
            return a.get();
        }
    });
    }

The atomicBoolean a is set like that at the begining of the class : private AtomicBoolean a = new AtomicBoolean(true);

My issue is the following: when I press a button and hold it the logcat shows "StringBuffer=0" and then "a set to false" "S sent" "t.stop and return" and then nothing for a while (about 12 seconds) and then it begins to loop in "message sent" "dodo" without ending even if I lift my finger. If I don't let my finger too long (simple click), I obtain: "stringBuffer=0" "S sent" "a set to false" "t.stop and return" "actionUp" "a set to true" "message sent" "dodo" "StringBuffer=0" "S sent" "t.stop and return"

I'm really lost and don't understand what's going on with this function.

If it helps, I'm debugging on a samsung galaxy tab (GT-P1000) firmware version 2.2.1

Hope someone can help me debugging it and explain me my issues...

1

There are 1 answers

6
Jon Skeet On

As I understand it, you're sleeping and looping in the UI thread. You're not meant to do that - it stops the UI from reacting to further events. Instead, you should operate in a reactive way - if you want something to happen "in 300 milli-seconds" then set a timer to do so, and cancel it if some other appropriate action occurs before then.