A quick question about Thread in Droid?

Put your problem here if it does not fit any of the other categories.

A quick question about Thread in Droid?

Postby NickLee » Fri Nov 12, 2010 5:58 pm

Dear all,

I am new to Android and I have a quick question here:
Let's say we have a class, it might be an Activity, it starts a Thread at some point:
Code: Select all
public class MyClass
{
    private Thread m_worker = null;
    public void StartThread(MyData data)
    { 
            if(null == m_worker)
            {
                  m_worker = new Thread()
                  {
                              public void Run()
                             {
                                   //do some work with data.
                                  while(!m_bStop)
                                  {
                                       SendDataToServer(data);               
                                  }
                             }
                   }
             }
    }
    public void SendMoreDataData(MyData data)
    {
        //How to hand the data to the thread we just created above?
    }
}

The main question is, I am not familiar with the way Android/Java declare the embedded Run() method.
1. In Run() method, how does it access class member of MyClass?
Most likely, I want to have m_bStop, SendDataToServer() as class member of MyClass, and probably create another queue to store all MyData that need to be posted to server.
2. Again, in Run(), how does it access any local variable in StartThread()? for example the MyData?

Thanks everyone.
NickLee
Junior Developer
Junior Developer
 
Posts: 10
Joined: Tue Jun 29, 2010 11:49 pm

Top

Re: A quick question about Thread in Droid?

Postby urbantrad » Fri Nov 12, 2010 6:30 pm

Since you declare the thread as an inner class, it can access any method/field of its containing class (MyClass in your case). It can use variables local to the method (StartThread) as well as its parameters, as long as they are defined to be final.
urbantrad
Senior Developer
Senior Developer
 
Posts: 104
Joined: Thu Sep 09, 2010 10:19 pm

Re: A quick question about Thread in Droid?

Postby NickLee » Sat Nov 13, 2010 12:30 am

Thanks a lot for your reply. It works exactly as you stated.
Also, there is another article to explain inner class, to share with everyone.
http://mobile.tutsplus.com/tutorials/an ... r-classes/
NickLee
Junior Developer
Junior Developer
 
Posts: 10
Joined: Tue Jun 29, 2010 11:49 pm

Re: A quick question about Thread in Droid?

Postby NickLee » Sat Nov 13, 2010 12:37 am

Code: Select all
public void StartThread(MyData data)
    {
            if(null == m_worker)
            {
                  m_worker = new Thread()
                  {
                              public void Run()
                             {
                                   //do some work with data.
                                  while(!m_bStop)
                                  {
                                       SendDataToServer(data);               
                                  }
                             }
                   }
             }
             m_worker.start();
    }

I have another issue now. I called the m_work.start(), however, it does not look like the thread started. I have break point in Run(), but it never gets called. What could be causing this?
I saw in regular java, in order to use thread, MyClass has to implement interface Runnable, is that necessary in Android? Here MyClass is just a regular class, not an Activity.

Thanks.
NickLee
Junior Developer
Junior Developer
 
Posts: 10
Joined: Tue Jun 29, 2010 11:49 pm

Re: A quick question about Thread in Droid?

Postby urbantrad » Sat Nov 13, 2010 12:36 pm

Since you define a thread, it is automatically runnable . It does kinda surprise me that the method you use starts with a capital R, are you sure that's right? If it is, then the only thing i can think of is that the m_bStop value is always set to true.
urbantrad
Senior Developer
Senior Developer
 
Posts: 104
Joined: Thu Sep 09, 2010 10:19 pm

Re: A quick question about Thread in Droid?

Postby NickLee » Mon Nov 15, 2010 5:20 pm

Thank you very much urbantrad. Stupid me, you know what, the problem is Run(). It should be run(). One little R took me a whole day....
Thanks again.
NickLee
Junior Developer
Junior Developer
 
Posts: 10
Joined: Tue Jun 29, 2010 11:49 pm

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 27 guests