| Author |
Looper Handler in java
|
muzi bond
Greenhorn
Joined: Jul 16, 2010
Posts: 4
|
|
Hi
The Looper, Handler concept of Android SDK is very kool.
Any ideas to implement similar concept in core java ??
|
 |
Steve Luke
Bartender
Joined: Jan 28, 2003
Posts: 3028
|
|
I haven't looked at the code, but my guess is this:
The static method Looper.prepare() creates a ThreadLocal reference to a Looper instance. The Looper instance has a Queue (probably a BlockingQueue of some sort) associated with it.
The Handler constructor uses the static method Looper.myLooper() which uses that ThreadLocal reference to return the proper Looper instance to the Handler.
The static method Looper.loop() then causes the Looper instance for the current Thread (the one assigned to the ThreadLocal reference) to run in a loop consuming the Queue, taking messages and calling the Handler's handleMessage() method.
The Handler then can use the Looper instance it retrieved to push messages or actions into the Looper's Queue and therefore into the Looper's thread loop()ing Thread.
Example of what it might look like in code. Untested, so treat it like pseudo-code
|
Steve
|
 |
muzi bond
Greenhorn
Joined: Jul 16, 2010
Posts: 4
|
|
thanks
|
 |
 |
|
|
subject: Looper Handler in java
|
|
|