1

I've a method to write the data in shared preference like,

private static void save(final Context context, final String key, final Object value) {
     new Thread(new Runnable() {
         @Override
         public void run() {
             SharedPreferences sharedPref = context.createDeviceProtectedStorageContext().getSharedPreferences(MY_SHARED_PREF, Context.MODE_PRIVATE);
             SharedPreferences.Editor editor = sharedPref.edit();
             if (value instanceof Integer) {
                 editor.putInt(key, (Integer) value);
             } else if (value instanceof Long) {
                 editor.putLong(key, (Long) value);
             }
             editor.apply();
         }
     }).start();
 }

Android documentation says,

Another consideration in deciding on how many threads to have is that threads aren’t free: they take up memory. Each thread costs a minimum of 64k of memory. This adds up quickly across the many apps installed on a device, especially in situations where the call stacks grow significantly.

it encourages to create a single thread like Handler thread and use it to do background job.

My thought is that save operation might take sometime so planned to have it in background thread but for the following questions i do not have clear answer which is stopping me to do it.

  1. Will the resources allocated for this thread be freed-up when the caller exists this method?

  2. What happens when there are too many calls to this save util method? Will thread creation be an overhead?

  3. In my case I do not want to have communication with UI thread or neither trying to communicate with another thread. My only purpose is to do a time (not a long running one) consuming tasks in separate thread. Here which one would be better thread creation or Handler or Async task?

Someone please help me understand this.

Tom Taylor
  • 3,344
  • 2
  • 38
  • 63
  • 1
    Check this link: https://stackoverflow.com/questions/28319636/when-thread-in-java-is-removed-from-memory – Daniyal Nasir May 12 '20 at 10:56
  • 1. There is no such thing as the scope of s thread. Only the scope of a variable. 2. It will fail. 3. I don't see why this needs to be a separate thread at all, let alone any of the alternatives you mention. – user207421 May 12 '20 at 10:57
  • Thank you for pointing out @user207421 the term scope is confusing here. I've edited the question. But just curious to know why do you say (2) would fail? – Tom Taylor May 12 '20 at 11:13

1 Answers1

1
  1. The resources will be freed up when the thread is finished. In your case, after editor.apply() finishes its work. This will typically happen later (or much later) than the save() method returns.

  2. Thread creation should not be a big issue here, but coexistence of many many threads is. Not only will they consume much memory, they will also compete for access to your SharedPreferences, and this could result in very bad performance.

    But yes, CPU overhead of creating short-living threads too often is also significant. That's why any kind of thread pool (e.g. Executor) will serve you better.

  3. AsyncTask is a nice utility, but it has its glitches (it can cause reference leaks and other mischiefs when not used with caution). And it has recently been deprecated. Also, in your case you want a single worker thread to perform all manipulations (as I mentioned above). And you don't need a Handler managed by Android. A SingleThreadExecutor is probably the best solution for you.

Community
  • 1
  • 1
Alex Cohn
  • 56,089
  • 9
  • 113
  • 307