One of the easiest way is to use a thread handler to which you post
events with data.
Say you're in your activity, and you want to start a thread and get
back some data, you can do so like this:
//worker thread...
class MyThread extends Thread
{
private Handler mHandler;
private Bundle mBundle = new Bundle();
MyThread(Handler pHandler)
{
mHandler = pHandler;
}
@Override public void run()
{
//do some work...
//once we're done, we send the data back to the caller...
Message msg = Message.obtain();
mBundle.clear();
mBundle.putInt("TYPE", 100);
mBundle.putString("SOMETHING", "WHATEVER");
msg.setData(mBundle);
mHandler.sendMessage(msg);
}
}
Then somewhere in your activity or other thread....
//this handles messages from the worker thread...
private Handler mHandler = new Handler()
{
@Override public void handleMessage(Message msg)
{
Bundle bundle = msg.getData();
int type = bundle.getInt("TYPE");
Log.d(TAG, "Handler got message type: " + type);
}
};
//start the worker thread...
void some_fun()
{
MyThread mThread = new MyThread(mHandler);
mThread.start();
}
You can put other data types in a bundle, and it's quite easy to use.
There are other ways to do this such as a global/shared variable with
a lock protecting it, which in my experience is a bit faster, but less
clean.
Look up the android documentation on Bundle, Message and ReentrantLock.
On Sat, Apr 24, 2010 at 12:04 PM, HeHe <[email protected]> wrote:
> or,
>
> 5. STL queue in C++/NDK :-P
>
> On Apr 24, 11:18 am, Anurag Singh <[email protected]> wrote:
>> 1. use global varibales
>>
>> 2. use named pipe streams
>>
>> 3. use socket
>>
>> 4. use file system
>>
>> - Anurag Singh
>>
>>
>>
>>
>>
>> On Sat, Apr 24, 2010 at 9:31 PM, Shekhar <[email protected]> wrote:
>> > Hi All,
>>
>> > I have a basic question on support of mutithreading in the android
>> > application programming.
>> > Suppose I have an activity running on the main thread and from the
>> > main thread, I have
>> > started two threads .Now my intention is to pass the data between
>> > these two threads.
>>
>> > Which method I have to use to pass the data between these two new
>> > threads?
>>
>> > Thanks,
>> > Shekhar
>>
>> > --
>> > You received this message because you are subscribed to the Google
>> > Groups "Android Developers" group.
>> > To post to this group, send email to [email protected]
>> > To unsubscribe from this group, send email to
>> > [email protected]<android-developers%2Bunsubs
>> > [email protected]>
>> > For more options, visit this group at
>> >http://groups.google.com/group/android-developers?hl=en
>>
>> --
>> You received this message because you are subscribed to the Google
>> Groups "Android Developers" group.
>> To post to this group, send email to [email protected]
>> To unsubscribe from this group, send email to
>> [email protected]
>> For more options, visit this group
>> athttp://groups.google.com/group/android-developers?hl=en
>
> --
> You received this message because you are subscribed to the Google
> Groups "Android Developers" group.
> To post to this group, send email to [email protected]
> To unsubscribe from this group, send email to
> [email protected]
> For more options, visit this group at
> http://groups.google.com/group/android-developers?hl=en
--
http://diastrofunk.com, http://developingthedream.blogspot.com/,
http://www.youtube.com/user/revoltingx, ~Isaiah 55:8-9
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en