Handlers and threads

Helpful? Then DOWNLOAD and RATE my app!

Helpful? Then DOWNLOAD and RATE my app!

I keep having to relearn the handler and thread concept.  Here are some notes for a future blog:

  • Create a Handler on the main thread; call it, for example, messageHandler
  • Subclass the Thread class and pass a Handler into the constructor; save the handle (e.g. messageHandler) in the thread.
  • When the thread needs to pass information to the main thread it simply passes a message to the main thread using  the handle to the Handler (e.g. messageHandler.post(…something here…));
  • A thread is still an object and a handle to it can be stored.  This is useful because the thread may need to be stopped at times when the configuration changes – since android does not handle the java threads.
  • Since handles of threads can be stored individually, so can List.
  • While List is allowed, it’s not the same as accessing a List of objects from threads.  List is not thread safe!!!
  • Again, List is not thread safe!!! So, don’t try to change data in one List from multiple threads.
  • Supposedly, Collections.synchronizedList() allows multiple threads to touch a list in a safe way but i’ve not investigated
  • synchronized before a method makes it thread safe…i.e. only one thread can access it at a time
Helpful? Then DOWNLOAD and RATE my app!

Helpful? Then DOWNLOAD and RATE my app!

Advertisements