• 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
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Enthuware question doubt

 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


This is an Enthuware question .
The above code will always print "false".???
Option: true or false
Answer:True

In a real time scenario the program output is false and false again
 
Bartender
Posts: 4116
72
Mac TypeScript Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
These are two different threads which run independently. But if one thread gets a chance and call setXY(1,2) (note that those values have no effect on the x,y) it then calls only setX() after that the next thread gets a chance and completes it's run method. Then x --> 2 and y --> 1. So I think true also possible as output.
 
Sheriff
Posts: 9708
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
This is an error in enthuware mock exam. It has been asked before many times

https://coderanch.com/t/264707/Programmer-Certification-SCJP/certification/Thread
 
Ranch Hand
Posts: 808
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sonali Sehgal wrote:

This is an Enthuware question .
The above code will always print "false".???
Option: true or false
Answer:True

In a real time scenario the program output is false and false again



One keyword was added by me and that code will always print false false.

Correct me if I am wrong.
 
Sonali Sehgal
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Lukas,
The answer of the question is already coming as falsefalse
My question was that the entuware says as this is not the right output so I highlighted it on the forum...

@Ankit
Thanks a lot
I do agree the question is wrong.However if this question comes in SCJP exam then what is the right output.
 
Lucas Smith
Ranch Hand
Posts: 808
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Usually false false but it is not guaranteed
 
Ankit Garg
Sheriff
Posts: 9708
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
@Sonali, if the question comes in SCJP, then the answer is that the ouput can be true false or false false. The second output can never be true.

@Lukas, I still feel that there is a tiny possibility that the output can be true even after your change (although I'm not sure). Since testXY is not synchronized, there is a chance that the value of x is incremented by the second thread and before the second thread can increment the value of y, testXY is called by the first thread. So the expression x != y will evaluate to 2 != 1 thus true...
 
Enthuware Software Support
Posts: 4895
60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sonali,
Can you please quote the QuestionId so that I can look into the question and investigate further?

Ankit,
The code that you have referenced from another thread is different from the one that Sonali has mentioned here. I couldn't find that question in our question bank either.

HTH,
Paul.
 
Ankit Garg
Sheriff
Posts: 9708
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
Yes, its my fault. This question looked very similar to the one in that other thread and I didn't see it closely...
 
Paul Anilprem
Enthuware Software Support
Posts: 4895
60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, I managed to track down this question. Its QuestionId is com.enthuware.ets.scjp.v6.2.174 and the question is :



The answer that is given is "False", which is right.

It also contains the following explanation:


You are creating 2 threads in main().
Consider this scenerio:
thread 1, calls setXY, in setXY it calls setX() (after getting the lock) and sets x to 1. Now setX ends and the lock is released. Before it could again get the lock to call setY(), the OS runs thread 2. The thread 2 again increments x to 2 and then it calls setY thereby incrementing y to 1. Now, it calls testXY and x is 2 and y is 1 so it will return true!
The point to note here is, calling two synchronized methods one after another from a non-synchronized method does not guarantee the order of updation of shared data.



HTH,
Paul.
 
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The Enthuware explanation is quite correct, although it may be very unlikely to happen it is still possible that the output is "true".

Lukas's addition is also correct. Making testXY synchronised means that there is no "break" in the locking between the calls to setX and setY any more and thus no chance for another thread to interfere.
 
Vijitha Kumara
Bartender
Posts: 4116
72
Mac TypeScript Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Lukas's addition is also correct. Making testXY synchronised means that there is no "break" in the locking between the calls to setX and setY any more and thus no chance for another thread to interfere.



I don't think that would still work? They are synchronizing on two instances.
 
Sonali Sehgal
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Paul,
The code of the enthuware question is:-

com.enthuware.ets.scjp.v5.2.174

The question id is different from the one you mentioned.
 
Paul Anilprem
Enthuware Software Support
Posts: 4895
60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sonali Sehgal wrote:Hi Paul,
The code of the enthuware question is:-

com.enthuware.ets.scjp.v5.2.174

The question id is different from the one you mentioned.



Hi Sonali,

Thanks for reporting the QID. The id you mentioned is from an older version but the question, its answer, and its explanation are same. Are you seeing any difference in that?

HTH,
Paul.
 
Sonali Sehgal
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Paul,

As far as I understand the answer is wrong and in the explanation part the output part is wrong.Henceforth, question is correct and answer iscoming in the software but answer in enthuware is not correct.
 
Paul Anilprem
Enthuware Software Support
Posts: 4895
60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sonali Sehgal wrote:Hi Paul,

As far as I understand the answer is wrong and in the explanation part the output part is wrong.Henceforth, question is correct and answer iscoming in the software but answer in enthuware is not correct.



Hi,
I am sorry but I am not sure what is the confusion. I am looking at both the versions and I see that in both the versions, the given answer and its explanation are correct. I am not sure what you mean by "answer is coming in software but answer in enthuware is not correct."
 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi everyone

I feel the question and solution are correct in enthuware. Correct me if i am wrong

Actually in the code there are 2 different thread objects initiating start method so there are two different thread objects and also the methods setXY and testXY are not synchronized so there is a chance of second thread to access setX method immediately after first thread sets X value.

I have tried with the sleep method inside synchronized method setX() and I got the output as true,false. As Thread.sleep sleeps the current thread for specific amount of time but without releasing the lock. Since there are 2 different thread objects the second thread can enter the setX method.

Also I got output true false when I made a minor change.
I added in setX() method and in setY() method. I think this console printing gives enough time for the other thread to access the same method.

I got the output as

Thread-0 X: 2
Thread-0 Y: 1
true
Thread-1 X: 2
Thread-1 Y: 2
false


If this is not the case then the synchronized method setX() should not allow other thread to enter right??? In the above output I found out that when thread 0 entered the setX method it got incremented to 1 and immediately the control switched to thread 1 and again the x value got incremented to 2 and hence in both the methods the value of X is 2. This can be possible since three were 2 different thread objects and even though presence of synchronized keyword it doesn't stop different threads in accessing the same method.

Please correct me if my explanation is wrong.

Regards
Vijay
 
Jason Irwin
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Vijitha Kumara wrote:I don't think that would still work? They are synchronizing on two instances.


You're right. It's been a long day.....I'm going home to do some mocks.
 
Well THAT's new! Comfort me, reliable tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic