• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Niko's mock exam question

 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.




 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Did you try to run the program?? The output will clear your doubt I guess. Also try this modified version of the program
 
jayalakshmi charugundla
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 385
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Siva Masilamani
Ranch Hand
Posts: 385
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Let me know if oyu have any question.
 
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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'.

 
Raghavan Muthu
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 385
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 385
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 385
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 385
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you all
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic