Monday 15 August 2011

multithreading - Android static variable different in service than from activity -


I'm hoping someone can explain what's going on in my code for me to read the code and My database is a block of writing that is running from the service and with my activities. I need this block of service if I am told by the service or activities, despite this, I should be intimidated. As such, I blocked the code and created a static locking object to place a thank-lock in it as you can see in the blocks of the block below:

  Synchronize (AnalyticsMessages. Sync_lock) {Log.v ("MixPlanel", "Lock Off:" + AnalyticsMessages.sync_lock.toString ()); String [] eventsdata = mDbAdapter.generateDataString (table); If (event data! = Null) {string last id = events [0]; String raw message = event data [1]; HTTPPoster Poster = Gate Pester (MidepointHost, MumblebackHost); HttpPoster.PostResult event posted = poster. PostData (rawMessage, endpointUrl); If (events were posted ==httpstepter.prosticult.UCDD) {Logboot MessageMomixpanel ("Post" + Endpoint URL); LogAboutMessageToMixpanel ("Sent Message \ n" + rawMessage); // log V ("MaxPennell", "Sent Message \ n" + Raw Message); MDbAdapter.cleanupEvents (last, table); Logs. V ("Mixpanel", "ID Deletion:" + Last ID); } Else if (eventsPosted == HttpPoster.PostResult.FAILED_RECOVERABLE) {// Try again later (! HUMSEAS (FLUSH_QUEUE) {sendEmotionMessageDelayed (FLUSH_QUEUE, mFlushInterval); }} And {// Skip, we have an unreadable failure; MDbAdapter.cleanupEvents (last, table); }}}   

The sync_lock is defined in AnalyticsMessages in this way:

  Private static final object sync_lock = new object ();   

This was not explicitly locking, so logging with the toasting log tells me that i somehow think 2 different sync_lock variables (because it prints an address in memory) is. I should mention that AnalyticsMessages is immediate:

  get public static analytics messagesinstants (reference message context) {synchronize (sinestance) {reference exponent = message context Tag Applications (CONTACE); Analytics Message Write; If (! SInstances.containsKeyskey (appContext)) {if (MPConfig.DEBUG) logs D. (LOGTAG, "Creating New Analytics Messages for Context" + App Contains); Rate = New AnalyticsMessages (appContext); SInstances.put (appContext, ret); } And (if (MPCInfig. DEBUG) log D. (LOGTAG, "Analytics Message for Reference" + App Contact + "already exists - Returning"); Ret = sInstances.get (App Contex);} Return Returns;}}   

What is happening is that when the service itself passes into the Analytics message. Log in as a reference, I end up with a sync_log variable, but when As one of my activities passes itself, as reference here, I have a different A Ending with? Does it show that an Android service uses a different class loader than the main process? Is there a work around? I would have thought that this would actually be a relatively common problem? Can I completely Thank you in advance!

It seems that Android: This was done for the background service

Although there is a common source of difficulty, however, from the point of view of the Java source, all this is an app, the fact that you are using two different processes, that means you have two different object piles, And therefore a global (stable data member) similar in the same heap - named Global in another heap.

Is the service running in a different process than the rest of the app?

Not usually. This causes your app to consume more memory and more CPU time, and as you saw, limit the ability of various components (running in different processes) to interoperate. I usually advise against many processes, it is said, it needs to be changed and tested thoroughly, especially if you do not know why this technique was used in the first place.

No comments:

Post a Comment