0

i am working on an app that has running many async tasks that bring data from server and approx 20 async task are running at a time and there are some threads which validate the data simulatenously.

I have made data models which update my Collections in which data is being stored. The structure to save data resides on Hashmap that contains an arraylist

So my Question is

  1. Is Android Context ThreadSafe
  2. Are Hashmaps in android ThreadSafe

I am using a Handler to Communicate messages so in this case Handler has a Message Queue is that threadsafe.

because sometimes one out of 10 times my app crashes giving

this is just one logcat in this case it happens while iterating, there are cases in which one task is reading hashmap and other is updating it this is the case in which mostly app crashes.

 07-28 15:36:59.815: ERROR/AndroidRuntime(4026): FATAL EXCEPTION: main
07-28 15:36:59.815: ERROR/AndroidRuntime(4026): java.util.ConcurrentModificationException
07-28 15:36:59.815: ERROR/AndroidRuntime(4026):     at java.util.ArrayList$ArrayListIterator.next(ArrayList.java:573)
07-28 15:36:59.815: ERROR/AndroidRuntime(4026):     at com.Juggle2.Panel.onTouchEvent(Panel.java:823)
07-28 15:36:59.815: ERROR/AndroidRuntime(4026):     at android.view.View.dispatchTouchEvent(View.java:3766)
07-28 15:36:59.815: ERROR/AndroidRuntime(4026):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:863)
07-28 15:36:59.815: ERROR/AndroidRuntime(4026):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:863)
07-28 15:36:59.815: ERROR/AndroidRuntime(4026):     at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1767)
07-28 15:36:59.815: ERROR/AndroidRuntime(4026):     at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1119)
07-28 15:36:59.815: ERROR/AndroidRuntime(4026):     at android.app.Activity.dispatchTouchEvent(Activity.java:2086)
07-28 15:36:59.815: ERROR/AndroidRuntime(4026):     at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1751)
07-28 15:36:59.815: ERROR/AndroidRuntime(4026):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1785)
07-28 15:36:59.815: ERROR/AndroidRuntime(4026):     at android.os.Handler.dispatchMessage(Handler.java:99)
07-28 15:36:59.815: ERROR/AndroidRuntime(4026):     at android.os.Looper.loop(Looper.java:123)
07-28 15:36:59.815: ERROR/AndroidRuntime(4026):     at android.app.ActivityThread.main(ActivityThread.java:4627)
07-28 15:36:59.815: ERROR/AndroidRuntime(4026):     at java.lang.reflect.Method.invokeNative(Native Method)
07-28 15:36:59.815: ERROR/AndroidRuntime(4026):     at java.lang.reflect.Method.invoke(Method.java:521)
07-28 15:36:59.815: ERROR/AndroidRuntime(4026):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:893)
07-28 15:36:59.815: ERROR/AndroidRuntime(4026):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:651)
07-28 15:36:59.815: ERROR/AndroidRuntime(4026):     at dalvik.system.NativeStart.main(Native Method)

which is clear indication that when async tasks and threads are trying to update arrayList / hashmaps they give this concurrency exception.

Umer Kiani
  • 3,783
  • 5
  • 36
  • 63
  • no 128 is the current threadpool limit of async tasks. and secondly i cannot cancel those tasks as it is the client requirement and this app does alot of server communication and server changes occur very frequently. – Umer Kiani Jul 30 '14 at 17:05
  • http://stackoverflow.com/questions/4068984/running-multiple-asynctasks-at-the-same-time-not-possible see the accepted answer – Illegal Argument Jul 30 '14 at 17:07
  • http://stackoverflow.com/questions/9654148/android-asynctask-threads-limits – Umer Kiani Jul 30 '14 at 17:09
  • brother our app has a design in which one listview has many horizontal listview and overthere i need to get data for each row using async tasks and there are 28 items in that. and i have been doing them using 28 async tasks which run simultaneously fully tested and since the invension of core processors the threadPoolLimit is 128 not sure how concurrency works but threadpool limit is for sure 128 – Umer Kiani Jul 30 '14 at 17:12
  • 1
    we were refering to different terms the threadpool limit is surely 128 but the number of asynctask that can run simultaneously is surely 5 the 6th asynctask is queued. So my point was if you have 20 asynctask then at most 5 are running and 15 are in queue. Check this and I think I am correct but correct me if I am wrong. – Illegal Argument Jul 30 '14 at 17:17
  • NO you are absolutely Right :) i was talking about ThreadPool Limit. – Umer Kiani Jul 30 '14 at 17:18
  • 1
    Got the Solution too. its to Use ConcurrentHashMaps instead of Simple Hashmap – Umer Kiani Jul 30 '14 at 17:19
  • Refer to This Documentation Link if you are Too Concerned About Concurrency Updation: http://developer.android.com/reference/java/util/concurrent/package-summary.html and http://developer.android.com/reference/java/util/concurrent/ConcurrentHashMap.html – Umer Kiani Jul 30 '14 at 17:24

1 Answers1

0

Array Lists and Hashmaps are not thread safe. I would suggest using mutex / synchronized blocks to ensure that only one thread is reading / writing to your collections at a time. Here is a small example to get you started.

    public class SomeClass {
    private final Object mutex = new Object();

    public void someMethodThatNeedsAMutex() {
        synchronized(mutex) {
            //here you hold the mutex
        }
    }
}
tier1
  • 6,303
  • 6
  • 44
  • 75
  • This cant be the case due to client requirements. i need a solution to make them threadsafe not to put the code in syncronized block – Umer Kiani Jul 30 '14 at 17:07