This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Niko's mock exam question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Niko Watch "Niko New topic
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
Sheriff

Joined: Aug 03, 2008
Posts: 9291
    
  17

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: 385
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: 385
Let me know if oyu have any question.
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

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: 3344

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: 385
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: 385
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: 385
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: 385
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Niko's mock exam question
 
Similar Threads
Thread Synchronization Problem
nikos thread question
Threads
Question on Thread - from Niko's mock
start() and join() in thread