04-14-2010 01:14 PM
What is the best way to implement thread synchronization in BlackBerry?
I've a communication thread that handle the http communication with my server. The main thread put requests in a que and the communication thread handle them.
How can I 'wake' the communication thread when putting a request?
If there is no request from the main thread for a long time (1 sec) I also need to pool the server to check for updates.
I looked at GlobalEvent but didn't see how to make sure the eventOccur will happen in the communication thread.
Solved! Go to Solution.
04-14-2010 01:36 PM
When you synchronize, you are synchronizing on some object. (When you mark a method as synchronized, it's synchronizing on an instance of that class.) Typically, a thread will call wait() on the synchronization object to suspend itself until "something" happens. That "something" is usually another thread completing some work and then calling notify() or notifyAll() on the synchronization object, which will wake up the waiting thread.
Sun's Java tutorial on concurrency has a nice explanation of all this. You can also search the web for Java implementations of the producer/consumer pattern.
For the specific application of timed polling, take a look at the java.util.TimerTask class.