File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Threads Please help Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Threads Please help" Watch "Threads Please help" New topic
Author

Threads Please help

Shaveta Chawla
Greenhorn

Joined: Aug 01, 2007
Posts: 13

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

Joined: Aug 14, 2007
Posts: 24
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.


SCJP 6.0
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 1945
    
  28
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

Joined: Sep 19, 2013
Posts: 2
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
Sheriff

Joined: Sep 28, 2004
Posts: 18120
    
  39

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Threads Please help
 
Similar Threads
use of start() without reference of the class
join a thread behind that thread...
Thread class join() method
a.run() and a.start()
Threads and join()