Skip to content Skip to sidebar Skip to footer

Android Synchronizing Methods Across Processes

I have synchronized static methods in a class called Remote which are invoked from a service to fetch data from the network. The service is an IntentService that's started every ti

Solution 1:

A couple of points:

  • You can place all the network related code in one IntentService and send it intents from different places passing the task details as extras. IntentService will process these intents sequentially;

  • Within a process you can use FutureTask[1] or Volley's RequestFuture<>[2] class to synchronize your network requests.

[1] see an example of how to use FutureTask to prevent re-starting the same operation: http://jcip.net/listings/Memoizer3.java

[2] see examples here: Can I do a synchronous request with volley?

Solution 2:

I am not a Android developer, but I think you may be getting confused between processes and threads.

A process has a self-contained execution environment. A process generally has a complete, private set of basic run-time resources; in particular, each process has its own memory space. source

When you launch the SyncAdapter as a process it has no knowledge of the other process (or its threads) which are using the same syncronized block, and each will obtain a lock in their own memory space.

I think you should be launching the SyncAdapter as a thread from your main process, as this way the threads will block while the other is in the synchronized block.

Post a Comment for "Android Synchronizing Methods Across Processes"