(keitai-l) Re: Moving work to the docomo event Thread

From: Reto Grob <rgrob_at_klab.org>
Date: 06/13/03
Message-Id: <20030613172935.D5A8.RGROB@klab.org>
Hi Trent & Jason,

> > It's quite simple really - my application has created a thread. Some time 
> > later, this thread needs to update the user interface eg. to change the text
> > on a label. Now, as far as I know the nttdocomo.ui library is not thread
> > safe  ie. it is only safe to call Label.setText from the event handling thread. 

Not thread-safe means that if multiple threads access the same object
concurrently,  the modifications may not be correct.

In order to solve that, you can simply make sure that only one thread at
a time can access the object. This can be done using the "synchronized"
statement in Java.

It does not mean that you need to do that in the event handling thread.


On Fri, 13 Jun 2003 00:58:18 -0700 (PDT)
Jason Pollard <jasonpollard@yahoo.com> wrote:

> 
> Hi Trent,
> 
> I'm fairly certain there's nothing like the invokeLater() in the doja API.  I
> guess you've tried some kind of non-threadsafe method?  You say you can't
> safely update the UI from the HTTP thread.  What's happening?  blocking?  I'm
> no thread guru, so I really don't know.  How about passing your HTTP thread a
> reference to the UI button, so that it can call the setText() directly?  or
> maybe vice-versa, so the button can kill the HTTP thread.
> 
> Hope I didn't confuse you anymore.  Let me know what you come up with, as I may
> need it myself someday.  Good luck!
> 
> --jason
> 
> 
> --- Trent Hill <Trent.Hill@bullant.com.au> wrote:
> > Hi Jason,
> > 
> > > What exactly are you trying to do?
> > 
> > It's quite simple really - my application has created a thread. Some time 
> > later, this thread needs to update the user interface eg. to change the text
> > 
> > on a label. Now, as far as I know the nttdocomo.ui library is not thread
> > safe 
> > ie. it is only safe to call Label.setText from the event handling thread. So
> > 
> > I somehow need to execute the necessary code on the ui thread.
> > 
> > If I were using C/Win32 I'd use PostMessage(...)
> > If I were using Java/Swing I'd use SwingUtilities.invokeLater(...)
> > If I were using Java/Midp I'd use Display.callSerially(...)
> > 
> > I can find anything like this in the com.nttdocomo.ui library.
> > 
> > > Can you give a pseudo-code example?
> > 
> > Of course.. this is not my real application but here's a similar example:
> > 
> > Imagine you had a ui with a button and a label. When the user clicks the
> > button, a stock quote is downloaded from an HTTP server and displayed on
> > the label.
> > 
> > Now, the HTTP download is blocking and I do not want to block the main event
> > 
> > handling thread while the download is occurring eg. I may want a cancel 
> > button to be available during the download attempt. Therefore, I must create
> > 
> > a second thread to handle the HTTP download. When the download succeeds, I 
> > need to update the label from the event handling thread to be thread safe.
> > 
> > So, in pseudo code:
> > 
> >   // Event handling thread:
> >   method handleEvent(event, arg)
> >   {
> >     if (event == buttonClick)
> >     {
> >       // create new thread to download quote
> >       new Thread(downloadQuote)
> >     }
> >     else if (event == dataReceived)
> >     {
> >       // display the new quote
> >       myLabel.setText(arg)
> >     }
> >   }
> > 
> > 
> >   // Network thread:
> >   method downloadQuote
> >   {
> >     openHttpConnection(myUrl)
> >     quote = downloadData() // <- long blocking operation
> >     closeHttpConnection()
> > 
> >     // somehow notify event handling thread since I cannot safely update
> >     // the ui from this thread
> >     sendMessage(handleEvent, dataReceived, quote) // *** <- how to do this??
> >   }
> > 
> > I hope this makes some sense to you. Any assistance would be appreciated.
> > 
> > Thanks, Trent.
> > 
> > --
> > Trent Hill
> > Software Engineer
> > Gravana Pty Ltd (trading as Bullant Software)
> > 
> > 
> > 
> > 
> > This mail was sent to address jasonpollard@yahoo.com
> > Need archives? How to unsubscribe? http://www.appelsiini.net/keitai-l/ 
> > 
> 
> 
> __________________________________
> Do you Yahoo!?
> Yahoo! Calendar - Free online calendar with sync to Outlook(TM).
> http://calendar.yahoo.com
> 
> This mail was sent to address bizmail@grob.ch
> Need archives? How to unsubscribe? http://www.appelsiini.net/keitai-l/ 
> 


__________________________________________________________
Reto Grob, rgrob@klab.org
Researcher, K Laboratory

Tel: +81-3-3436-1010 Fax: +81-3-5408-1203
Received on Fri Jun 13 11:41:03 2003