Page 1 of 1

About Question enthuware.ocpjp.v7.2.1438 :

Posted: Sat Aug 23, 2014 4:20 am
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

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

Posted: Sat Aug 23, 2014 4:37 am
by admin
main cannot finish before other threads because it calls wait.

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

Posted: Sat Dec 13, 2014 4:43 am
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?

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

Posted: Sat Dec 13, 2014 9:56 am
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.

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

Posted: Sun Dec 14, 2014 3:41 am
by SepticInsect
Ok. I just read that wait releases the lock.
Thanks!

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

Posted: Sat Aug 01, 2015 1:12 pm
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?.

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

Posted: Sat Aug 01, 2015 8:39 pm
by admin
You are right. That is a possibility. The code has been updated to avoid this problem.
thank you for your feedback!
Paul.

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

Posted: Sun Aug 09, 2015 9:37 am
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 :)

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

Posted: Sun Aug 09, 2015 10:18 am
by admin
You are right. Updated again :oops:

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

Posted: Sun Aug 16, 2015 12:58 pm
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?

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

Posted: Sun Aug 16, 2015 1:35 pm
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.

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

Posted: Tue Sep 08, 2015 1:22 am
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?

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

Posted: Tue Sep 08, 2015 2:39 am
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?

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

Posted: Sun Nov 15, 2015 5:02 pm
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?

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

Posted: Sun Nov 15, 2015 9:14 pm
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.

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

Posted: Fri Dec 11, 2015 3:03 am
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 ?

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

Posted: Fri Dec 11, 2015 1:14 pm
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

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

Posted: Sat Dec 12, 2015 2:02 am
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?

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

Posted: Sat Dec 12, 2015 2:04 am
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.

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

Posted: Tue Mar 08, 2016 3:25 am
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

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

Posted: Tue Mar 08, 2016 9:42 pm
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.