• Post Reply Bookmark Topic Watch Topic
  • New Topic

doubts>thread basic  RSS feed

 
Tarun Pahuja
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As ws we know thread is a seprate call of stack(list of stack frame),but at the same time its an object of class Thread
object and stack(isn,t both these are contradict)?
And when we write,


Thread t= Thread.currentThread();
//does this statement implies that we passing the address of stack pointer of current stack frame of seprate call of stack in t?
"Correct me if i am wrong"?
and when we write


t = new Thread
(Runnable threadob,"threadname");//since threadob only have acess to run function extend in class that implements runnalbe interface,so we passing the address of that run function in thread t,which define where execution of thread ll begin?
 
Vidmantas Maskoliunas
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know if that helps, but actual objects live on the heap... So references point to the common heap for all threads rather than stack, that should solve the contradiction, in my opinion.
 
Lorand Komaromi
Ranch Hand
Posts: 276
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tarun Pahuja wrote:As ws we know thread is a seprate call of stack(list of stack frame),but at the same time its an object of class Thread


Thread is a java class, an instance of Thread is an Object that represents a thread of execution (probably this is what you meant by "a seprate call of stack(list of stack frame)"). This thread of execution exists only after it has been started (Thread.start()) until the run() method returns.
 
Tarun Pahuja
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vidmantas Maskoliunas,thank you very much
may be you are right,but please give me anwser of other two queries too,in which basically i wanna know
how thread getta know about run()?
 
Vidmantas Maskoliunas
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think Lorand Komaromi gave a good hint for that -- so the overall threading routine could look like following:

current thread owns stack1, the new thread will own stack2, they both share the same heap

Runnable rr = ... // variable rr lying on stack1 gets a reference to the object lying on heap which contains the method "run"
Thread t = new Thread(rr, "SomeName"); // variable t lying on stack1 gets a reference to the newly created thread object. The thread inside has a reference to the instance of object with "run" method, and that object lies on heap (not stack1 nor stack2)
t.start(); // the JVM starts new thread of execution, and executes the method of the object lying on heap.

So, answers could be (in my opinion):

Thread t= Thread.currentThread();
//does this statement implies that we passing the address of stack pointer of current stack frame of seprate call of stack in t?
"Correct me if i am wrong"?


This line of code assigns t with a reference to the Thread object representing the current thread of execution. It is the reference to the object lying on common heap.

t = new Thread
(Runnable threadob,"threadname");//since threadob only have acess to run function extend in class that implements runnalbe interface,so we passing the address of that run function in thread t,which define where execution of thread ll begin?


In this case, we are not passing the address of function (method), but reference to the heap where instance of Runnable exists (actually instance of some object implementing Runnable, but who cares...). The thread of execution will take this object from heap and executes it's run method.

I make a guess that you have been a programmer of C/C++/Delphi, haven't been?
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!