This week's book giveaway is in the Cloud/Virtualization forum.
We're giving away four copies of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds and have James Denton on-line!
See this thread for details.
Win a copy of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds this week in the Cloud/Virtualization forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

Threading in Java  RSS feed

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi

i have some questions regarding threading in java.

i have a class which implements runnable interface.
for one of the methods, i need to set the values and send these values back to the server.
i wrote the client-server connection code in "run" method so that i can do this in anohter thread.

my question is to run a new thread,
do i need to create a new thread and invoke "start" method (as in option 1)

or

i just invoke "run" method (as in option 2)



regards
terence
 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's a good question, and can only be answered by the code that calls setXY -- does the calling thread want this method to return immediately and then call the server later or is the calling thread willing to wait and have the server called synchronously?

If it were me, and I were writing a method that calls a server asynchronously, I would hide the Runnable object from the caller -- have some local variable that was Runnable or a private field that was Runnable -- why should the caller see that a class is Runnable, they might call run() by mistake!
 
Terence Thant Zin Oo
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
no, i dont want to wait the thread. i want to return from the method immediately and do the client-server communication later.
(if i want to wait, i will just need to do normal method invoking, am i right???)


have some local variable that was Runnable or a private field that was Runnable



can you please explain me how to achieve that???

regards
Terence
 
Dana Bothner-By
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Something like this:
 
Terence Thant Zin Oo
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


you mean i should create an inner class or something like this???

anyway what is the answer for my very first question? (ie., whether to invoke "run" method or create a new thread and call "start" as you did)

thanks and regards
terence
 
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Calling run() is just like calling any other method. The next statement won't execute until the run() method returns. This doesn't sound like what you want.

Calling Thread.start() does some fairly serious magic behind the scenes, starts a new thread, runs the run() method on the new thread and lets your next statement execute on the original thread. That's what you want to happen this time.

You'll have to give your Runnable everything it needs to do its job, say a reference to the server it needs to send results to.

Does that help?
 
Terence Thant Zin Oo
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes

thanks a lot, Dana and James.
 
Dana Bothner-By
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Terence Thant Zin Oo:


you mean i should create an inner class or something like this???



I never mentioned "inner class". If you feel you need to define an inner class in your particular case, go right ahead. The choice is orthogonal to the question of starting a new thread.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!