• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Looper Handler in java

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ??
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks
 
Being a smart alec beats the alternative. This tiny ad knows what I'm talking about:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic