Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / October 2005

Tip: Looking for answers? Try searching our database.

Thread Execution

Thread view: 
Linus - 30 Oct 2005 22:50 GMT
Im programming for a mobile phone (though this is not a J2ME question)
and basically I cant make a blocking call (such as opening a connection)
from an CommandListener because it causes deadlock.

So i created a new class that extends Thread that opens a connection, in
the constructor it takes the class that created it (i think its whats
called a call back) so that when it has created the connection it gives
back the data streams to the original class.

Should I continue execution from the call back method invoked by the
connection creator, or should I poll to see when the data streams are !=
null.

The latter seems pointless in situation where im trying to avoid
blocking. Im not quite sure how the garbage collector works but
basically after the call back has been made i dont need the connection
creater object any more so will it be destroyed? Or because thats the
thread thats running will it be kept alive, anyway im a bit confused
about how this all works so if someone could give me some pointers that
would be great.

HoboZilla
Matt Humphrey - 31 Oct 2005 01:09 GMT
> Im programming for a mobile phone (though this is not a J2ME question) and
> basically I cant make a blocking call (such as opening a connection) from
[quoted text clipped - 8 lines]
> connection creator, or should I poll to see when the data streams are !=
> null.

Those pretty much are the two choices.  The thread that actually creates the
connection must either call back to the original object (or some listener /
handler / lock object) or it must maintain state and be polled.  Callback is
usually preferred to prevent delays and wasted time polling, but it requires
that the callback object be properly synchronized so that it can accept the
call from a 2nd thread.  Polling still requires some synchronization, but
the caller can remain single-threaded.

> The latter seems pointless in situation where im trying to avoid blocking.

That would only be true if your main thread spent all its time polling.
Usually it would intersperse polling with its regular work.  Doing
"busy-idle" spin-polling is a bad idea all around. You can either embed
periodic polling into your other thread or go with the callback.  GUIs like
Swing include an "invoke later" operation so the callback request occurs
properly threaded with the GUI.

> Im not quite sure how the garbage collector works but basically after the
> call back has been made i dont need the connection creater object any more
> so will it be destroyed? Or because thats the thread thats running will it
> be kept alive, anyway im a bit confused about how this all works so if
> someone could give me some pointers that would be great.

Your new connection thread acts as an anchor to keep alive all the objects
reachable from the current thread state.  After the connection thread makes
the callback, its work is done and the thread can exit.  The objects
anchored by the thread will become eligible for garbage collection sometime
after that.

The platform you're on may also offer special techniques to help either with
the polling or callback operations.

Cheers,


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.