| Author |
Niko's mock exam question
|
jayalakshmi charugundla
Ranch Hand
Joined: Jul 22, 2009
Posts: 57
|
|
Hi all,
I have a doubt in this program.It is compiling successfully. According to the below statement the answer is false. I think it should be true.
Will you explain me ?
Just before the main method exits, the account’s number field is guaranteed to have value 2000.
|
 |
Ankit Garg
Saloon Keeper
Joined: Aug 03, 2008
Posts: 9189
|
|
Did you try to run the program?? The output will clear your doubt I guess. Also try this modified version of the program
|
SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
|
 |
jayalakshmi charugundla
Ranch Hand
Joined: Jul 22, 2009
Posts: 57
|
|
Ankit I ran the program what you have given there. But now it is displaying something like this,
run:
Exception in thread "main" java.lang.VerifyError: (class: Account, method: <init> signature: ()V) Constructor must call super() or this()
at Job.main(Job.java:38)
Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds)
I m sorry I couldnt find out any difference btw the two programs. How is it possible?
thanks in advance,
jaya
|
 |
Siva Masilamani
Ranch Hand
Joined: Sep 19, 2008
Posts: 377
|
|
OK here is the correct answer.
Since because get and set are not atomic the value is not gauranteed.
E.g Lets consider number of iteration is 2.
First Iteration :
Thread 1 executes the getNumber and its value is 0 but before setNumber is executed for this thread,Thread 2 gets the lock and executes the getNumber and retrieve its value as 0 again and also execute setNumber and set the value as 1. Note:Thread 1 did not execute setNumber yet.
Now again Thread 1 gets the lock and execute the setNumber method ,But the value retrieved by this thread was 0 (when its getNumber was executed) so it again update the value to 1.
Iteration 2:
Thread 1 executes getNumber and get the value as 1 and then execute the setNumber method and set the value as 2.
Thread 2 execure the getNumber and get the value as 2 and then execute the setNumber and set the value as 3.
So value 4 is never reached in this scenario.
Correct me if i am wrong?
|
SCJP 6,SCWCD 5,SCBCD 5
Failure is not an option.
|
 |
Siva Masilamani
Ranch Hand
Joined: Sep 19, 2008
Posts: 377
|
|
|
Let me know if oyu have any question.
|
 |
Raghavan Muthu
Ranch Hand
Joined: Apr 20, 2006
Posts: 3327
|
|
jayalakshmi charugundla wrote:Ankit I ran the program what you have given there. But now it is displaying something like this,
run:
Exception in thread "main" java.lang.VerifyError: (class: Account, method: <init> signature: ()V) Constructor must call super() or this()
at Job.main(Job.java:38)
I did NOT get any errors. It ran successfully by showing the result as '2000'.
|
Everything has got its own deadline including one's EGO!
[CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
|
 |
Raghavan Muthu
Ranch Hand
Joined: Apr 20, 2006
Posts: 3327
|
|
jayalakshmi charugundla wrote:
I m sorry I couldnt find out any difference btw the two programs. How is it possible?
Please look at the code carefully. The modified version does 'synchronize' on the 'account' object before getting into the loop!
This is one such incident to show how we generally overlook and give a different or possibly wrong answer in the exams!
|
 |
Siva Masilamani
Ranch Hand
Joined: Sep 19, 2008
Posts: 377
|
|
OK here is the correct answer.
Since because get and set are not atomic the value is not gauranteed.
E.g Lets consider number of iteration is 2.
First Iteration :
Thread 1 executes the getNumber and its value is 0 but before setNumber is executed for this thread,Thread 2 gets the lock and executes the getNumber and retrieve its value as 0 again and also execute setNumber and set the value as 1. Note:Thread 1 did not execute setNumber yet.
Now again Thread 1 gets the lock and execute the setNumber method ,But the value retrieved by this thread was 0 (when its getNumber was executed) so it again update the value to 1.
Iteration 2:
Thread 1 executes getNumber and get the value as 1 and then execute the setNumber method and set the value as 2.
Thread 2 execure the getNumber and get the value as 2 and then execute the setNumber and set the value as 3.
So value 4 is never reached in this scenario.
Correct me if i am wrong?
|
 |
jayalakshmi charugundla
Ranch Hand
Joined: Jul 22, 2009
Posts: 57
|
|
Hi,
I ran the program again n now it is showing the correct output. I have replaced the value 2 where actually it was in the for loop . But it is displaying 4 as the
output.
As I know, join() stops the current loop iteration(ie. thread1) and will be joined to the thread2 after finishing its (thread2) work. That means first thread 2 finished its work. Somewhere I have seen second thread1 finisned its job before the first one.
Am I right?
Siva I didnt understand the program.
If you dont mind could you please explain it easily about join() and this program.?
|
 |
Siva Masilamani
Ranch Hand
Joined: Sep 19, 2008
Posts: 377
|
|
Did you look at my recent post???
I will edit my old post
I explained it clearly in the recent post..If you still have doubt i could explain it again.
|
 |
Siva Masilamani
Ranch Hand
Joined: Sep 19, 2008
Posts: 377
|
|
jayalakshmi charugundla wrote:Hi,
I ran the program again n now it is showing the correct output. I have replaced the value 2 where actually it was in the for loop . But it is displaying 4 as the
output.
As I know, join() stops the current loop iteration(ie. thread1) and will be joined to the thread2 after finishing its (thread2) work. That means first thread 2 finished its work. Somewhere I have seen second thread1 finisned its job before the first one.
Am I right?
Siva I didnt understand the program.
If you dont mind could you please explain it easily about join() and this program.?
No you are wrong about join method.
Join method will cause the currently running thread to join at the completion of the thread reference by it.
In the above program join is called from main thread.So in this case main thread will wait until thread one completes and it has nothing to do with thread two.
currently running thread corresponds to where the join call is present,if it is in main then it is the main thread that will execute the join call and hence main thread will wait if call from another method lets sat getNumber in this case then the currently running thread could be one or two.
My example program
Hope it helps you.
|
 |
jayalakshmi charugundla
Ranch Hand
Joined: Jul 22, 2009
Posts: 57
|
|
yes, I have seen it. It is showing the output as 4.
I am really unable to understand? Here I am defining other different program and is similar to the previous . The output for the progrm is false.
How ?
If you dont feel pain, you can tell me?
|
 |
Siva Masilamani
Ranch Hand
Joined: Sep 19, 2008
Posts: 377
|
|
Did you see my example program with its output?
That will answer both of your question
let me know if you still have doubt onthis.
|
 |
G Venu
Greenhorn
Joined: Mar 30, 2009
Posts: 5
|
|
I dont think we can arrive at the same output all the time. the final value may or may not be 2000.
|
 |
jayalakshmi charugundla
Ranch Hand
Joined: Jul 22, 2009
Posts: 57
|
|
Thank you all
|
 |
 |
|
|
subject: Niko's mock exam question
|
|
|