Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

synchronrized and join()  RSS feed

 
sudarsan ym
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class test
{
synchronized method1()
{
// some code...
}
}
class demo extends Thread
{
test t;
demo(test t1)
{
t = t1;
}
public void run()
{
t.method1();
}
}
class sample
{
public static void main()
{
test tx = new test()
demo d = new demo(tx);
d.start();
//What will happen if d.join(); is executed
}
}
 
Paul Caudle
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The current thread (the one running the main method) will wait for the execution of t.method1() to finish in the thread labeled d and then continue with execution of the main. Since there is no more code in the main the application will return/end.
Is that the kind of answer you were looking for here?
Paul
 
Tanveer Rameez
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi
if you want to find what join() does, see this code:

here if you comment the t.join(); , then even before the run method completes, the "exiting main" will be printed, i.e the main thread will exit before the thread t completes. But if you run the code as it is, then the main thread will wait for the thread t to complete. Then after "printing 999" is printed, "exiting main" will be printed.
I hope this is what u wanted
bye
Tanveer
Tanveer I have modified your code so that it has a try block for the join() method . Hope you don't mind

[This message has been edited by Rahul Mahindrakar (edited December 12, 2000).]
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!