Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Threads Please help

 
Shaveta Chawla
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

please tell me that when the start method will be invoked in separate thread then the makeit method will execute i9n main thread or in in the stack of starter thread. and the join ,method will join which thread to another thread.

Thanks
 
prakash chandra
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here your problem is not clear. Still I will try to answer. I hope you will understand. Following examples will make you clear how join() works.

Lets say,
three threads main, Thread1 and Thread2 .
main thread starts a thread Thread1 and Thread1 starts another thread Thread2.

join() method always makes the parent thread to wait till the child thread ends. After child thread terminates parent thread resumes.

main is parent of Thread1.
Thread1 is parent of Thread2.

if you invoke join() in main() then program will run normally, it will not show any effect, because main() is started by JVM.
 
Tony Docherty
Bartender
Posts: 2965
59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Once the Starter constructor has finished you have 2 threads running, the main method executes the makeit() method, the Starter thread executes the run() method. The main thread calls join() and is blocked until the Starter terminates (ie it exits the run() method). The main thread then continues.

Of course with threads being threads and having no guarantee of execution order it is possible that the Starter thread will have terminated before the join() method is even called. In which case the main thread will just continue.
 
nikita Gupta
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tony Docherty wrote:Once the Starter constructor has finished you have 2 threads running, the main method executes the makeit() method, the Starter thread executes the run() method. The main thread calls join() and is blocked until the Starter terminates (ie it exits the run() method). The main thread then continues.

Of course with threads being threads and having no guarantee of execution order it is possible that the Starter thread will have terminated before the join() method is even called. In which case the main thread will just continue.


whatever you explained is correct..now tell me after run method why make method not uses the value as of x as 5 as it is the method of separate thread and the value 10 is local to run method?? please tell me where i am wrong?
 
Henry Wong
author
Marshal
Pie
Posts: 21199
81
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
nikita Gupta wrote:
whatever you explained is correct..now tell me after run method why make method not uses the value as of x as 5 as it is the method of separate thread and the value 10 is local to run method?? please tell me where i am wrong?



The x variable used in the run() method, is an instance method of the Starter class. It is specific to the Starter instance that the run() method is called on. It is not a local variable of the run() method -- as there is no declaration for it anywhere in the run() method.

Henry
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic