简体   繁体   中英

Looper, worker thread and other concurrency issues

Can someone please explain to me in short the following terms, and how they relate to each other?

  1. handler - each thread has a handler? each view has a handler? perform the post() . there is a default handler for the OS main thread?

  2. looper -

  3. looper thread -

  4. post - adding a runnable to the end of the thread's tasks queue

  5. task - a runnable or a message

  6. executor - manages a threads' pool to execute some tasks. useful if preforming the same task few times.

  7. timer -

  8. runnable vs message - when to use each of them?

  9. thread's queue - each thread has a tasks queue.

Any other terms I should be familiar with regarding concurrency issues in Android?

I will try to explain shortly but to fully understand those terms I'll have to practice developing android applications.

handler : an object attached to a thread (any thread) that both processes messages and executes runnables in its thread. That is important sometimes because ie only the UI thread can do operations on views, and sometimes a background thread must use an UI thread handler to do action in the views

looper is an object that iterates through a thread message queue, popping runnables and messages and giving them to the thread handlers. The UI thread comes already with a Looper running

looper thread is the thread of a given looper, nothing special here

post , task and executor ou answered yourself already

timer and class to manage small amounts of time. Like measuring some seconds or milliseconds, wainting some amount of time and so on.

runnable vs message the main difference is: when you use runnables, the poster thread mus specify the implementation of something, ehile when you use messages, the handlers specifies the implementation of some action. When your working with only one poster and one handles you can use both almost interchangeably. When you have many different threads posting the same thing to a same handler, I would use messages and vice-versa.

Additionally to what @hsgubert said, there's some other concepts that's worth mentioning. Once you enter the "multithread world" you should know there are many synchronization and concurrency handling methods. As you're running things in paralell, you'll be running your app and once in a time, at different points, you'll probably get some beautiful ConcurrentModificacionException .

In my own experience, this is one of the biggest pains (memory leaks permitting) I've ever experienced, probably because there's not a pattern how to solve them. So I think you should also know these concepts:

  • Use synchronized methods/variables whenever needed. In methods, it will prevent another Thread enter before the current Thread has ended its execution. It will not prevent other Thread s modify insider data structures in another methods, though, so be clear about it. Used as a synchronized(variable_name) { ... } block, it will prevent variable_name being modified app-wide by any other Thread until you exit that block.

  • There are many inter- Thread synchronization methods. The two I use most are Semaphore and CountDownLatch (probably I use most the latter). Sometimes it might happen that you don't want to execute some code portion until some other portion (or Thread ) has finished its execution. This methods safe lifes.

  • There are several already-concurrency-prepared data structures. These datastructures basically already implement the above mentioned, so if you use .add() on multiple places concurrently, Java itself handles that event and manages to not show any exception. More info here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM