File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes synchronized test Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "synchronized test" Watch "synchronized test" New topic
Author

synchronized test

Shrinath M Aithal
Ranch Hand

Joined: May 20, 2009
Posts: 82
source: katherine sierra and bert bates. (self test of chapter 9)

Given:


What is the result of this code?
(skipping irrelevant parts of choices given, )

D. It prints XY with a 10-second delay between X and Y.

G. An exception is thrown at runtime.

Answer in book says :
" G is correct. The code does not acquire a lock on t before calling t.wait(), so it throws
an IllegalThreadStateException.
If the wait were placed inside a synchronized(t)
block, then the answer would have been D. "

My problem is :
I actually tried to get D as answer by wrapping t.wait(10000); with synchronized(t){ t.wait(10000); } but didn't get the required result.. how should i've done it?


Regards
Shri..

SCJP 5.0
avi sinha
Ranch Hand

Joined: Mar 15, 2009
Posts: 453

nothing is wrong with the code if you wrap the wait method invokation in a synchronised block.
running fine on my system.




avi sinha


SCJP 5.0 SCWCD 5.0
Shrinath M Aithal
Ranch Hand

Joined: May 20, 2009
Posts: 82
No dude, I too tried the same code, and its not "waiting" 10 seconds before printing y..
I am getting the output as "xy" fine, but not with 10 second gap..
did you get 10 second gap?
avi sinha
Ranch Hand

Joined: Mar 15, 2009
Posts: 453

Shrinath M Aithal wrote:No dude, I too tried the same code, and its not "waiting" 10 seconds before printing y..
I am getting the output as "xy" fine, but not with 10 second gap..
did you get 10 second gap?


ya i am getting the lag between x & y
Shrinath M Aithal
Ranch Hand

Joined: May 20, 2009
Posts: 82
super confusion now
why isn't it waiting in my system ? I tried running it on another system too, both not giving lag between x and y..

and I tried other programs with "wait()" command, no program is waiting on this command.. ???
why isn't wait working with my system?
avi sinha
Ranch Hand

Joined: Mar 15, 2009
Posts: 453

Shrinath M Aithal wrote:super confusion now
why isn't it waiting in my system ? I tried running it on another system too, both not giving lag between x and y..

and I tried other programs with "wait()" command, no program is waiting on this command.. ???
why isn't wait working with my system?



i got the point actually when i executed it again it gave XY instantly.

reason:

wait() method will force the thread to stop executing until

1> the thread on which it has been called becomes dead or specified time

since you haven't overridden run method here the thread t becomed dead as soon as it is started hence there is no lag.

avi sinha
avi sinha
Ranch Hand

Joined: Mar 15, 2009
Posts: 453



this will give you lag for sure
Shrinath M Aithal
Ranch Hand

Joined: May 20, 2009
Posts: 82
avi sinha wrote:
since you haven't overridden run method here the thread t becomed dead as soon as it is started hence there is no lag.



so you mean to say, "t" is a dead thread when we are calling t.wait(10000); ???
and you can acquire synchronization on a dead thread?

and one more thing .. just to bug avi sinha ---> how did you get lag the first time? and why???

and the code you posted above, is infinitely waiting even after printing x and y and is inside your run method forever..
Shrinath M Aithal
Ranch Hand

Joined: May 20, 2009
Posts: 82
I think i found a proper way..

watch this :


that does the job exactly as required.. but is that mature way?
avi sinha
Ranch Hand

Joined: Mar 15, 2009
Posts: 453

Shrinath M Aithal wrote:

so you mean to say, "t" is a dead thread when we are calling t.wait(10000); ???
and you can acquire synchronization on a dead thread?


first point to notify you is that you can synchronize a method or a block on objects not a thread.
and even if a thread is dead by isAlive() method it's still a valid object irrespective of its status

and one more thing .. just to bug avi sinha ---> how did you get lag the first time? and why???


since you are reading k & B so i can say you know that a very little is guaranteed in multithreading.

adding to the above statement threads in java are system dependent. os decides the thread everytime to be executed. so you can't be sure that at a particular instant a thread will be executed.

so everytime even if you are not overridding the run method thread t can take different time intervals for completion.

i think this much is sufficient for you.


and the code you posted above, is infinitely waiting even after printing x and y and is inside your run method forever..


i posted the code to show the case when the wait() method will again allow the main thread to go to runnable state before the completion of run method for thread t DEAD STATE

also if the exception is not being thrown there is no any difference between declaring or handling the exception.there will be no any effect of it on code's behaviour.

avi sinha

avi sinha
Ranch Hand

Joined: Mar 15, 2009
Posts: 453

Shrinath M Aithal wrote:I think i found a proper way..

watch this :


that does the job exactly as required.. but is that mature way?


the original code you posted in you first post & this code is entirely different.

wait method in the first code was being called by the main thread & here it is being called by the thread you ve created. & it is not allowing any other thread to execute because there is no any thread hence it is just waiting for 10 secs

avi sinha
Shrinath M Aithal
Ranch Hand

Joined: May 20, 2009
Posts: 82
so, does it suggest that the answer given in book is wrong? because it says
If the wait were placed inside a synchronized(t)
block, then the answer would have been D.


that didn't happen.. so it doesn't work as said?

>> about bugging Mr avi sinha :
I meant to ask you how you got the "10 second " lag the first time I agree with whatever you said about the thread taking different times to complete printing x and y on different executions.. But usually, its instantaneous.. and you getting proper 10 second lag, is that co incidence ?? ;)

lets drop it.. that'll not help with threads lets talk about the priority of this discussion " is the answer specified in k&b about option "D" wrong? "
Shrinath M Aithal
Ranch Hand

Joined: May 20, 2009
Posts: 82
avi sinha wrote:
the original code you posted in you first post & this code is entirely different.

wait method in the first code was being called by the main thread & here it is being called by the thread you ve created. & it is not allowing any other thread to execute because there is no any thread hence it is just waiting for 10 secs

avi sinha


ya you are right on that one.. I was on the aim of creating a lag between x & y, so forgot the big picture about what we are talking..

so the code you posted would be right on that case
but please do something to stop the infinite running thread.. add t.stop() or something to main before it ends..
avi sinha
Ranch Hand

Joined: Mar 15, 2009
Posts: 453

Shrinath M Aithal wrote:so, does it suggest that the answer given in book is wrong? because it says
If the wait were placed inside a synchronized(t)
block, then the answer would have been D.


that didn't happen.. so it doesn't work as said?

>> about bugging Mr avi sinha :
I meant to ask you how you got the "10 second " lag the first time I agree with whatever you said about the thread taking different times to complete printing x and y on different executions.. But usually, its instantaneous.. and you getting proper 10 second lag, is that co incidence ?? ;)

lets drop it.. that'll not help with threads lets talk about the priority of this discussion " is the answer specified in k&b about option "D" wrong? "



well i think the book is saying about the possiblity . ya its possible but not always

 
Consider Paul's rocket mass heater.
 
subject: synchronized test