In my application I have two threads (I’m using AsyncTask class). in doInBackground() method of each thread i’m calling my own getNewsItems(Url url) method, this method will connect to server and get response. after that in onPostExecute() method of each thread, i’ll call displayData() method to show results on screen.
The problem is when i run, the application crashes. I think its because these two thread have access to common methods in same time because when i separate common methods then the app runs successfully. I have no idea is it because of don’t use of serialization?
Thanks
you can put a
synchronizedmodifier to your method that is accessed by multiple threads.e.g.
so that, When one thread is executing a synchronized method for an object, all other threads that invoke synchronized methods for the same object block (suspend execution) until the first thread is done with the object.
you can also read about it here: http://docs.oracle.com/javase/tutorial/essential/concurrency/syncmeth.html
UPDATE:
you can have the
getNewsItems(Url url)in a separate class like this:and then instantiate it within your threads like this:
this way, same method will be called but in different class instances…