Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Threading in Java  RSS feed

 
Terence Thant Zin Oo
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
 
Dana Bothner-By
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
 
Stan James
(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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!