Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Looper Handler in java

 
muzi bond
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
The Looper, Handler concept of Android SDK is very kool.
Any ideas to implement similar concept in core java ??
 
Steve Luke
Bartender
Posts: 4181
21
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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


 
muzi bond
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic