GeeCON Prague 2014*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Threads Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Threads" Watch "Threads" New topic
Author

Threads

Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Hi,
I was going through the Threads chapter in Khalid Mughal's book when I found this excercise problem (#9.6, Page 282-83). I'd be grateful if anyone could explain me the mechanics of this program...

Thanks in Advance
Shyam
[This message has been edited by Shyamsundar Gururaj (edited October 25, 2001).]
R K Singh
Ranch Hand

Joined: Oct 15, 2001
Posts: 5371
I am totally gone ....
ANYONE please help us ......
I tried to run the above code ...
modified the code and run again :
modified code is:

If i could change the subject of this then changed it to... "check your thread knowledge??"
I tried and failed
------------------
Regards
Ravish


"Thanks to Indian media who has over the period of time swiped out intellectual taste from mass Indian population." - Chetan Parekh
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
This is what Khalid Mughal says as an explanation:
Once cannot be certain whether any of the letters i, j and k will be printed during execution. For each invocation of check(), each variable pair is incremented and their values are always equal when the method returns. The only way a letter could be printed would be if the method check() was executed between the time the first and the second variable was incremented. Since check() method does not depend on owning any monitor, it can be executed at any time, and the method doit() cannot protect the atomic nature of its operations by acquiring monitors.
Waiting for your comments...
Shyam
Michael Ernest
High Plains Drifter
Sheriff

Joined: Oct 25, 2000
Posts: 7292

That explanation helps considerably in understanding the output: your code as shown outputs the character 'i' for all cases.
I see what's happening in this code, but what it intends to demonstrate is far from self-evident. I don't really see the point Khalid is making in the explanation provided other than "make sure your critical sections are correctly synchronized."
------------------
Michael Ernest, co-author of: The Complete Java 2 Certification Study Guide


Make visible what, without you, might perhaps never have been seen.
- Robert Bresson
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
>I see what's happening in this code
Sadly, Michael, I don't see that too clearly ..It'd be great if you could run me through the code. By the way, I made an error in the print statements. I have corrected that now.
Since I was just trying to understand the code in the first place, I did not post the 5 answer choices that came with the code. Here they go...

  1. The program will fail to compile.
  2. One cannot be certain whether any of the letters i, j and k will be printed during execution
  3. One can be certain that none of the letters i, j and k will ever be printed during execution.
  4. One can be certain that the letters i and k will never be printed during execution.
  5. One can be certain that the letter k will never be printed during execution.

  6. The correct answer is given to be B.

    Thanks in Advance
    Shyam
    [This message has been edited by Shyamsundar Gururaj (edited October 25, 2001).]
Rashmi Hosalli
Ranch Hand

Joined: Jun 25, 2001
Posts: 50
Hi,
I feel that the explanation given is self sufficient and that the answer is B.However according to me,the answer should also be E.Please read the explanation.
There are two threads created and run in the main method and since the method doit() contains synchronised code, inorder to execute the synchronised code the monitor lock has to be obtained by the thread(s).Apart from that,if everything goes on as expected, all the static variables are incremented equally and the check method should not have printed out anything.
But this does not happen as the threads have to obtain the locks to the monitor and this may or may not happen immediately after one another and hence in the meantime the check method may be executed.So there are chances that i and j will be printed out...however keep in mind that since k1 and k2 gets incremented in one atomic process they will always be equal and it will not be printed out.
Thanks,
Rashmi
R K Singh
Ranch Hand

Joined: Oct 15, 2001
Posts: 5371
Thanks Rashmi...
Can anyone elaborate this quote:
Originally posted by Shyamsundar Gururaj:
Since check() method does not depend on owning any monitor, it can be executed at any time, and the method doit() cannot protect the atomic nature of its operations by acquiring monitors.
Waiting for your comments...
Shyam


------------------
Regards
Ravish
R K Singh
Ranch Hand

Joined: Oct 15, 2001
Posts: 5371

Anyone .. any answer for my above prob
this is just to put on top.
sorry !!!

------------------
Regards
Ravish
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
Hi all, this a great example to understand Threads
I have runned the code and i and j are printed. Also is k if you let it go enough time.

Since check() method does not depend on owning any monitor, it can be executed at any time, and the method doit() cannot protect the atomic nature of its operations by acquiring monitors.

This explained the behaviour of the program.
At the beginning one could think that k1 and k2 are going to have always the same value. But this is only true is the "peeker" thread is also synchronized. Because check it is not, it can peek the values after k1++ but before k2++. Thus only B is correct.
hope helps

SCJP2. Please Indent your code using UBB Code
 
GeeCON Prague 2014
 
subject: Threads