• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

doubts>thread basic

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 276
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic