4

I'm trying to understand the Looper...

This time i'm getting a little bit confused, because I've only used (until now) the while(true) condition to maintain the thread in an infinite loop.

I just want to know if it is better to use the Looper or the while(true) condition.

I've searched a possible answer to my question in internet and I was quite disappointed in finding codes that use Looper and while(true) together.

Probably I didn't get the point of using the Looper but, you know, we are here to learn... isn't it?

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
  • I was looking into multi threading a few days ago here, and came across this answer and this lecture. Answer - https://stackoverflow.com/a/29655572/2104990 Lecture - https://www.youtube.com/watch?v=aV2XfWwpiDU&index=8&list=PLZ9NgFYEMxp50tvT8806xllaCbd31DpDy Documentation - https://developer.android.com/topic/performance/threads.html It can certainly be a daunting task in this instance, but those should help you out – miversen33 Dec 06 '17 at 15:01
  • These documents talk about the difference between handler thread asynctask and handlerthread. maybe I was not able to transmit the purpose of my question. I wanted to understand if using the Looper class is better than using the while(true) condition inside the run method of the Thread class... as I saw in internet (https://blog.mindorks.com/android-core-looper-handler-and-handlerthread-bd54d69fe91a) I can use the Looper to launch runnables in queue. Is it better than using the while(true) condition? if yes, why? We have to use them in combination? if yes, why? – Andrea Zedda Dec 06 '17 at 15:20
  • 1
    Possible duplicate of [What is the purpose of Looper and how to use it?](https://stackoverflow.com/questions/7597742/what-is-the-purpose-of-looper-and-how-to-use-it) – Ravindra babu Dec 06 '17 at 15:25
  • 1
    not a duplicate, DeeV gets perfectly the point. – Andrea Zedda Dec 06 '17 at 15:34

2 Answers2

5

Looper is a mechanism that keeps thread open to process Messages that are sent to it via Handler. In that sense, they are similar to while(true) in that the Thread stays open forever (or in this case until you explicitly tell it to close). However, unlike while(true), you can send the Messages to it from another thread. Every time you create a Handler, it binds itself to the Looper thread that it was created in. Then, every time you post a message to that Handler, it processes that message on the thread in which it was created.

The most common, well-known Looper thread in Android is the main UI thread. This thread is always running. It runs code that are posted to it. Most commonly drawing operations, but if you take a view and call View#post, it will run the code in the next draw cycle. This is because all View objects have a Handler that was built on the main UI Looper thread.

The UI Looper can actually be referenced to Looper.getMainLooper() method, and you can create a Handler that posts messages to this thread like so: new Handler(Looper.getMainLooper());.

Though if you need, you could create your own Looper thread that acts as a parallel thread to the UI thread. You can't do drawing operations on it, but you could use it as a worker thread for other operations. That way you don't have the overhead of creating a new thread every time you need to do something intensive.

DeeV
  • 35,865
  • 9
  • 108
  • 95
  • This is the answer I was looking for, thank you @DeeV. Can you also say if there are cases (at least one) where using Looper and while(true) together make sense? – Andrea Zedda Dec 06 '17 at 15:33
  • 3
    @AndreaZedda No. There's no need to use both. Looper is more powerful so I recommend using it over `while(true)`. – DeeV Dec 06 '17 at 15:39
  • 1
    @AndreaZedda looper is well structured replacement for what you would do with a `while(true)` – payloc91 Dec 06 '17 at 15:40
  • Ok your both answer are extremely helpful for me and they will be cause for reflection for sure. I was in doubt just because I saw other people (talking about similar topics) that use them together. Anyway thank you!! :) You made my day – Andrea Zedda Dec 06 '17 at 15:46
1

I just want to know if it is better to use the Looper or the while(true) condition.

I'd say, as a general rule, unless you need some very specific low-level control, use the API that are provided by the framework, whichever the framework is.

They usually take care of many aspects that would otherwise have you implement them yourself, such as messaging, task deletion, and so on...

I guess that with the while (true) loop you are trying to achieve some regular task repetition.

In this particular case, yes, use a Handler with its own Looper, so the UI Thread is not blocked, and post your Runnable instances regularly with postDelay(Runnable, long).

@DeeV 's answer already gives you a good understanding about the mentioned components, but you sure will find out everything you need in Android documentation.

payloc91
  • 3,724
  • 1
  • 17
  • 45