• Post Reply Bookmark Topic Watch Topic
  • New Topic

Will this code work fine?

 
Ajay Xavier
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


when i call the execute method on the Test class will it wait until the run() method of Testrun gets completed?
Also provide the explanation about the flow?
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ummm, are you asking us to a) run the program (which you can easily do yourself), and b) explain it to you (who wrote it)?
[ April 25, 2006: Message edited by: Ulf Dittmer ]
 
Ken Blair
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ajay Xavier:
when i call the execute method on the Test class will it wait until the run() method of Testrun gets completed?
Also provide the explanation about the flow?


No. Your TestRun class synchronizes on itself and your Test class synchronizes on the Thread object, those are two completely different objects. If they actually synchronized on the same thing then Test would wait until TestRun invoked notify*.

* Assuming the TestRun instance was the only thing waiting on that object.
 
Ajay Xavier
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ken thanks for your reply.

Will it wait when i syncronize it on the Testrun object means "run"?

 
Ken Blair
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, now you're waiting and notifying on the same object. Test will create a new instance of TestRun and invoke wait() that instance causing the current thread to place itself in the wait set of that object. That same instance of TestRun, which is executing in a separate thread that you created and started, will invoke notify() on itself and remove a thread from it's wait set. At this point the only thread in it's wait set will be the one that was executing Test so that thread will be removed from it's wait set, acquire the monitor and continue execution.
 
Ken Blair
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I should also point out that if somehow the TestRun instance invoked notify() before the Test instance invoked wait() it would not work. However, since TestRun is sleeping for five seconds this is extraordinarily unlikely but keep that in mind when moving on to something more complicated.
 
Ajay Xavier
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I should also point out that if somehow the TestRun instance invoked notify() before the Test instance invoked wait() it would not work. However, since TestRun is sleeping for five seconds this is extraordinarily unlikely but keep that in mind when moving on to something more complicated.


Ken thanks again for your reply

This quote i am not much clear about it Please can you explain it in detail?
 
Ken Blair
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Consider this:



Then add this to the beginning of the execute method:



Now everytime you execute System.out.println(String) the current thread will sleep for 10 seconds. It will still work, for now, but later when you remove that rather innocent looking println in TestRun it will break. Why? Because now Test will be sleeping for 10 seconds when it executes the println and TestRun won't be. As a result Test will execute wait() after TestRun executes notify() and it will be waiting forever. A rather naive example I know, but hopefully it gets the point across. Don't rely on thread A executing X before thread B executes Y unless you've properly synchronized them so that it's guaranteed, otherwise you end up with race conditions.
 
Ajay Xavier
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Ken,

Thanks for your reply again. i am clear now. can i avoid the race condition if i start the thread(fedthread) inside the sychronized block?

means

 
Ken Blair
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!