0

I am trying to develop an audio processing related app in android. I have one thread(not the UI thread) in which I am doing an operation. I need to update the result of the operation while it is still going on. For the same I am using a Handler. I am just using a Toast to display the result inside the handler. As of now my thread is continuing to run for the first time alone and after displaying the first result the thread doesn't run anymore because of which results are not updated. I just came to know that while modifying variables shared by this Thread and the UI, I need to synchronize both the threads. Am I correct? If so how can I achieve it?

Thanks!!

EDIT

I am posting a part of the method which is running in my thread and my handler.

        while(fulldatabuffcnt+200<=fulldatabuffer.size())  
        {   
            double[] windowdata=new double[200];
             classlabel=0;
             //classlabel_new=0;
             int windowcnt=0;
                for (int h=fulldatabuffcnt;h<fulldatabuffcnt+200;h++)
                {
                        windowdata[windowcnt]=fulldatabuffer.get(h);
                        windowcnt++;
                }

                MFCCcoeffs=mfcc_inst.getParameters(windowdata);
                classlabel=likeli_ref.llhmain(MFCCcoeffs);

            try {
                out.writeInt(fulldatabuffer.size());

                } catch (IOException e1) 
                {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }

            classlabel_array[ecount]=classlabel;
            ecount++;

            if (ecount==25)
                {
                      synchronized(SharedData.globalInstance) {

                            SharedData.globalInstance.classlabel_new =occurence(classlabel_array);//<--shared variable classlabel_new getting modified
                        }
                      try {
                            out_max.writeInt(SharedData.globalInstance.classlabel_new);
                            } catch (IOException e1) {
                                // TODO Auto-generated catch block
                                e1.printStackTrace();
                            }

                      ecount=0;
                     uiCallback.sendEmptyMessage(0);


                }


        fulldatabuffcnt=fulldatabuffcnt+80;
        }
       if(fulldatabuffcnt+200>fulldatabuffer.size()){

           AppLog.logString("Setting calclating thread to null");
           calculatingThread = null;
       }
       try {
           out.close();

           out_max.close();
        } 
       catch (IOException e) 
        {
           e.printStackTrace();
        } 

   }


   private Handler uiCallback = new Handler () {

         public void handleMessage (Message msg) {
            int label_handler;
            synchronized(SharedData.globalInstance) {
               label_handler=SharedData.globalInstance.classlabel_new;
            }

             Toast.makeText(MFCC2Activity.this, "Classified label" +label_handler, Toast.LENGTH_SHORT).show();//<--trying to access classlabel_new

         }

     };
user1957734
  • 1
  • 1
  • 4

1 Answers1

2

Yes, you should synchronize to make sure that your UI thread doesn't access variables that are only partially set up by your own thread.

I suggest that you have a singleton object what contains all the variables/data etc that you need to pass between the two threads. For example, suppose you need to share a string and a double between your own thread and the UI thread. Create a class SharedData with a singleton, e.g.

class SharedData {
    public String aString;
    public double aDouble;
    public static SharedData globalInstance = new SharedData();
}

Then in your own thread where you want to set the data

synchronized(SharedData.globalInstance) {
    SharedData.globalInstance.aString = "some string";
    SharedData.aDouble = 42.0;
}

and in your UI thread

String aString;
double aDouble;
synchronized(SharedData.globalInstance) {
    aString = SharedData.globalInstance.aString;
    aDouuble = SharedData.aDouble;
}
// do something with aString and aDouble

If you do it like that, then there won't be any problems relating to partially set data being read by the UI thread.

Stochastically
  • 7,616
  • 5
  • 30
  • 58
  • Thanks for the quick response. I did as you said but still there is no change. Kindly go through the edit I have made. classlabel_new is the variable which is getting modified in the thread which is getting shared with the handler. – user1957734 May 20 '13 at 13:36
  • As far as I can tell, what you've done if a threadsafe way of sharing the int that you've called classlabel_new (a field of the SharedData class). I'm not exactly sure what your code is meant to be doing, but I'm pretty sure that if it's still not doing what you want, it's not a problem related to synchronization. – Stochastically May 20 '13 at 14:28
  • Ok. I dont know if I will be able to fully make you understand the situation but what is happening is that, the buffer fulldatabuffer gets filled whenever a voice activity is detected in the recording made through microphone. This step is done by another thread and the this thread shown in the code above does some kind of processing on the fulldatabuffer. Whenever I comment the display part ie,uiCallback.sendEmptyMessage(0); everything is happening perfectly. – user1957734 May 21 '13 at 04:55
  • But when i include it, After the first voice whenever silence comes ie, when fulldatabuffer stops getting updated by the other thread, this thread stops running. Any idea what the reason for the problem is? – user1957734 May 21 '13 at 04:57
  • Perhaps you should post another question to focus on this issue, because the question that you posted here is simply about synchronization between threads. But here's what I'd do to investigate. I'd put lots of `Log.d(...)` everywhere, so as to work out exactly what path all the threads take, and where they fail. If you post again, post all that information for all threads, together with any error messages. Sorry that I can't be of more help. – Stochastically May 21 '13 at 07:47