About Question enthuware.ocpjp.v7.2.1438 :

All the posts and topics that contain only an error report will be moved here after the error is corrected. This is to ensure that when users view a question in ETS Viewer, the "Discuss" button will not indicate the presence of a discussion that adds no value to the question.

Moderators: Site Manager, fjwalraven

Post Reply
shareef.hiasat
Posts: 20
Joined: Thu Dec 19, 2013 8:22 am
Contact:

About Question enthuware.ocpjp.v7.2.1438 :

Post by shareef.hiasat »

i think this question may print a,c,b if the print in main executed before any threads completes its work , please explain why a,c,b is not one of the outputs

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocpjp.v7.2.1438 :

Post by admin »

main cannot finish before other threads because it calls wait.
If you like our products and services, please help us by posting your review here.

SepticInsect
Posts: 20
Joined: Tue Nov 04, 2014 1:13 am
Contact:

Re: About Question enthuware.ocpjp.v7.2.1438 :

Post by SepticInsect »

Now consider the situation where the main thread gets to run first. It will enter the synchronized block and will start waiting. Now, AnotherThread will run, sort the list and call notifyAll(), thereby awaking the main thread. The main thread will dump the contents of the args array. And the program will terminate.
But when AnotherThread executes the main thread is still in the synchronized(args) block. Can AnotherThread still enter its synchronized(args) block?

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocpjp.v7.2.1438 :

Post by admin »

There is a call to wait within the synchronized block of main. So another thread will be able to get a lock for args.
If you like our products and services, please help us by posting your review here.

SepticInsect
Posts: 20
Joined: Tue Nov 04, 2014 1:13 am
Contact:

Re: About Question enthuware.ocpjp.v7.2.1438 :

Post by SepticInsect »

Ok. I just read that wait releases the lock.
Thanks!

magdute70
Posts: 4
Joined: Fri Mar 20, 2015 4:34 pm
Contact:

Re: About Question enthuware.ocpjp.v7.2.1438 :

Post by magdute70 »

What about spurious wake-ups? How can we be assured main thread will not experience spurious wakeup from wait() before AnotherThread will get chance to sort array? Isn't this why enclosing wait() in conditional loop is reccomended? In way this program is written it may output [a, c,  b] in case of spurious wakeup, isn't it?.

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocpjp.v7.2.1438 :

Post by admin »

You are right. That is a possibility. The code has been updated to avoid this problem.
thank you for your feedback!
Paul.
If you like our products and services, please help us by posting your review here.

magdute70
Posts: 4
Joined: Fri Mar 20, 2015 4:34 pm
Contact:

Re: About Question enthuware.ocpjp.v7.2.1438 :

Post by magdute70 »

Sorry to bother You again, but now after update 3rd option seems to be incorrect - main thread can never enter infinite wait, cause in case AnotherThread gets lock first, sorted is already set to true and args.wait never called :)

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocpjp.v7.2.1438 :

Post by admin »

You are right. Updated again :oops:
If you like our products and services, please help us by posting your review here.

hepcat
Posts: 1
Joined: Sun Aug 16, 2015 12:54 pm
Contact:

Re: About Question enthuware.ocpjp.v7.2.1438 :

Post by hepcat »

boolean sorted is not volatile. There is a chance that main-thread could not see any updates to sorted var in which case it will keep waiting.

So considering above IT MAY PRINT a, b, c and IT MAY PRINT nothing should be correct. Am I wrong?

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocpjp.v7.2.1438 :

Post by admin »

hepcat wrote:boolean sorted is not volatile. There is a chance that main-thread could not see any updates to sorted var in which case it will keep waiting.
No, sorted is not volatile but it is being checked from inside a synchronized block. Synchronization ensures that memory writes by a thread before or during a synchronized block are made visible in a predictable manner to other threads which synchronize on the same monitor. Here, both the threads are synchronizing on the same monitor args, so updates by one thread will be visible to the other.
If you like our products and services, please help us by posting your review here.

boyonsea
Posts: 16
Joined: Fri Sep 04, 2015 5:06 pm
Contact:

Re: About Question enthuware.ocpjp.v7.2.1438 :

Post by boyonsea »

what happens in the situation where the "new AnotherThread(args).start()" thread gets to run first.
wont the main thread wait for ever?

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocpjp.v7.2.1438 :

Post by admin »

It is not clear what you mean by "gets to run first". Do you mean just started before the main thread executes synchronized(args) or not just started first but also finishes before the main thread executes synchronized(args)? In either of the cases, why do you think main thread will wait for ever?
If you like our products and services, please help us by posting your review here.

evafang2008
Posts: 9
Joined: Fri Jun 26, 2015 11:15 am
Contact:

Re: About Question enthuware.ocpjp.v7.2.1438 :

Post by evafang2008 »

Hi, I am confused about args.wait() execution result when the other thread args.notifyAll() completed before main thread wait(), is that possible? So main thread will wait forever? The option "It MAY print nothing." looks possible?
admin
Post subject: Re: About Question enthuware.ocpjp.v7.2.1438 :PostPosted: Tue Sep 08, 2015 2:39 am
Offline
Site Admin

Joined: Fri Sep 10, 2010 9:26 pm
Posts: 4701
It is not clear what you mean by "gets to run first". Do you mean just started before the main thread executes synchronized(args) or not just started first but also finishes before the main thread executes synchronized(args)? In either of the cases, why do you think main thread will wait for ever?

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocpjp.v7.2.1438 :

Post by admin »

Yes, that is possible. In that case, the main thread will run and see that sorted is true and so args.wait(); will not be executed so therefore, the main thread will not wait for ever.
If you like our products and services, please help us by posting your review here.

fariz.siracli
Posts: 22
Joined: Mon Jul 06, 2015 11:45 am
Contact:

Re: About Question enthuware.ocpjp.v7.2.1438 :

Post by fariz.siracli »

admin wrote:Yes, that is possible. In that case, the main thread will run and see that sorted is true and so args.wait(); will not be executed so therefore, the main thread will not wait for ever.
But in this case main thread will not leave synchronized block and "while" will work in synchronized block on args lock. So how AnotherThread will get lock on "args" for sorting ?

fariz.siracli
Posts: 22
Joined: Mon Jul 06, 2015 11:45 am
Contact:

Re: About Question enthuware.ocpjp.v7.2.1438 :

Post by fariz.siracli »

ooh, is this explanation for my question ?
When wait is invoked, the thread releases the lock and suspends execution. At some future time, another thread will acquire the same lock and invoke Object.notifyAll, informing all threads waiting on that lock that something important has happened:

public synchronized notifyJoy() {
joy = true;
notifyAll();
}
Some time after the second thread has released the lock, the first thread reacquires the lock and resumes by returning from the invocation of wait.

This is from link : http://docs.oracle.com/javase/tutorial/ ... dmeth.html

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocpjp.v7.2.1438 :

Post by admin »

fariz.siracli wrote:
admin wrote:Yes, that is possible. In that case, the main thread will run and see that sorted is true and so args.wait(); will not be executed so therefore, the main thread will not wait for ever.
But in this case main thread will not leave synchronized block and "while" will work in synchronized block on args lock. So how AnotherThread will get lock on "args" for sorting ?
Why do you think main thread will not leave synchronize block if sorted is true?
If you like our products and services, please help us by posting your review here.

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocpjp.v7.2.1438 :

Post by admin »

fariz.siracli wrote:ooh, is this explanation for my question ?
When wait is invoked, the thread releases the lock and suspends execution. At some future time, another thread will acquire the same lock and invoke Object.notifyAll, informing all threads waiting on that lock that something important has happened:

public synchronized notifyJoy() {
joy = true;
notifyAll();
}
Some time after the second thread has released the lock, the first thread reacquires the lock and resumes by returning from the invocation of wait.

This is from link : http://docs.oracle.com/javase/tutorial/ ... dmeth.html
No, wait() will not even be invoked if sorted is true. It will be invoked only if sorted is false. And in that case, main thread will wait until another thread calls notify. Once another thread calls notify, main thread will proceed from wait and execute the rest of the code.
If you like our products and services, please help us by posting your review here.

dgpoon
Posts: 1
Joined: Tue Mar 08, 2016 3:08 am
Contact:

Re: About Question enthuware.ocpjp.v7.2.1438 :

Post by dgpoon »

Hi there, could you please tell my why one of the answers is: It MAY print [a, b, c] rather than It WILL print [a, b, c]. What are the alternatives - if we assume that the main thread is not interrupted during wait() I.e. an InterruptedException is not thrown?

Thanks
Dan

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocpjp.v7.2.1438 :

Post by admin »

It will actually always print a, b, c as the explanation also notes. Selecting Option 1 and 4 basically amounts to saying same thing as well. The "MAY" is used in the options to confuse the candidate with the possibility of multiple outputs. But I agree that it should say WILL instead of MAY in option 1. Fixed.
thank you for your feedback!

HTH,
Paul.
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: No registered users and 23 guests