About Question enthuware.ocpjp.v7.2.1438 :
Moderators: Site Manager, fjwalraven
-
- Posts: 20
- Joined: Thu Dec 19, 2013 8:22 am
- Contact:
About Question enthuware.ocpjp.v7.2.1438 :
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
-
- Site Admin
- Posts: 10036
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1438 :
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.
-
- Posts: 20
- Joined: Tue Nov 04, 2014 1:13 am
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1438 :
But when AnotherThread executes the main thread is still in the synchronized(args) block. Can AnotherThread still enter its synchronized(args) block?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.
-
- Site Admin
- Posts: 10036
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1438 :
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.
-
- Posts: 20
- Joined: Tue Nov 04, 2014 1:13 am
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1438 :
Ok. I just read that wait releases the lock.
Thanks!
Thanks!
-
- Posts: 4
- Joined: Fri Mar 20, 2015 4:34 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1438 :
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?.
-
- Site Admin
- Posts: 10036
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1438 :
You are right. That is a possibility. The code has been updated to avoid this problem.
thank you for your feedback!
Paul.
thank you for your feedback!
Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 4
- Joined: Fri Mar 20, 2015 4:34 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1438 :
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
-
- Site Admin
- Posts: 10036
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1438 :
You are right. Updated again
If you like our products and services, please help us by posting your review here.
-
- Posts: 1
- Joined: Sun Aug 16, 2015 12:54 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1438 :
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?
So considering above IT MAY PRINT a, b, c and IT MAY PRINT nothing should be correct. Am I wrong?
-
- Site Admin
- Posts: 10036
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1438 :
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.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.
If you like our products and services, please help us by posting your review here.
-
- Posts: 16
- Joined: Fri Sep 04, 2015 5:06 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1438 :
what happens in the situation where the "new AnotherThread(args).start()" thread gets to run first.
wont the main thread wait for ever?
wont the main thread wait for ever?
-
- Site Admin
- Posts: 10036
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1438 :
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.
-
- Posts: 9
- Joined: Fri Jun 26, 2015 11:15 am
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1438 :
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?
-
- Site Admin
- Posts: 10036
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1438 :
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.
-
- Posts: 22
- Joined: Mon Jul 06, 2015 11:45 am
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1438 :
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 ?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.
-
- Posts: 22
- Joined: Mon Jul 06, 2015 11:45 am
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1438 :
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
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
-
- Site Admin
- Posts: 10036
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1438 :
Why do you think main thread will not leave synchronize block if sorted is true?fariz.siracli wrote: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 ?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.
If you like our products and services, please help us by posting your review here.
-
- Site Admin
- Posts: 10036
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1438 :
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.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
If you like our products and services, please help us by posting your review here.
-
- Posts: 1
- Joined: Tue Mar 08, 2016 3:08 am
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1438 :
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
Thanks
Dan
-
- Site Admin
- Posts: 10036
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1438 :
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.
thank you for your feedback!
HTH,
Paul.
If you like our products and services, please help us by posting your review here.
Who is online
Users browsing this forum: No registered users and 23 guests