About Question enthuware.ocpjp.v7.2.1438 :

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
codecodecode67
Posts: 14
Joined: Sun Dec 06, 2015 2:15 pm
Contact:

About Question enthuware.ocpjp.v7.2.1438 :

Post by codecodecode67 »

How will this happen?
If 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.
If the main thread gets to run first, and starts waiting - it's waiting within the synchronized(args){} block. How will the thread AnotherThread manage to enter it's synchronized(args){} block in order to sort the array? Isn't the idea of the synchronized(args){} block that no 2 threads can be in a synchronized(args){} block at the same time - because args is the same array => they're using the same lock.... => if the main thread manages to go into it's synchronized(args){} block first, the program should stall on main thread's wait() because the thread AnotherThread will not be able to enter its own synchronized(args){} block because it doesn't have the lock for it....

Please explain...

Thanks

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 args.wait(); in main, which releases the lock and allows another thread enter its synchronized block.

You have asked a very fundamental question, which tells me that you are attempting mock exams without going
through a book. I would suggest you to read this (and other) topics from any good book first.

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

neokit2008
Posts: 1
Joined: Mon Oct 24, 2016 6:12 am
Contact:

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

Post by neokit2008 »

notifyAll is called on args which is member variable of class AnotherThread. how will it notify the main thread waiting on args which is member of class WaitTest? if possible please reply soon sir. i have exam tomorrow.

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 »

When you call notifyAll on an object, it wakes up all the threads that have called wait on that same object. It doesn't matter which class is that object part of.
This is a very fundamental concept. I suggest you to go through a good book to learn about it.

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

daniloj
Posts: 1
Joined: Sun Sep 10, 2017 10:29 am
Contact:

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

Post by daniloj »

Hi!

Does the fact the variable sorted is NOT volatile imply in the possibility of the static field not being updated in the memory for the main Thread [making the option 'It MAY print nothing' possible]?

If it does not, would it be related to the "happens-before relationship", since it is guaranteed that at least the second time while(!sorted) is called will happen after "AnotherThread" instance had modified the WaitTest.sorted variable?

Thank you,

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 »

The main thread is reading the value of sorted variable within a synchronized block. Therefore, updates made to sorted variable by another thread (which is also writing the value to sorted variable within a synchronized block that is using the same lock as the main thread) will be visible to the main thread. Thus, making sorted variable as volatile is not required here.

"It may print nothing" cannot be the right answer in any case because the main thread prints a List created using args in the same thread and that list is not empty.

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

horst1a
Posts: 37
Joined: Mon Jun 12, 2017 2:16 am
Contact:

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

Post by horst1a »

I have a different problem with this code. Assuming AnotherThread runs first, it will sort the List<String>m
in its run() method. Fine. But now the main thread again fills a List<String> m = Arrays.asList(args) , and therefore fills it with a,c,b

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 »

horst1a wrote:I have a different problem with this code. Assuming AnotherThread runs first, it will sort the List<String>m
in its run() method. Fine. But now the main thread again fills a List<String> m = Arrays.asList(args) , and therefore fills it with a,c,b
Did you read the explanation? The first line of the explanation answers your question -
1. Arrays.asList() method creates a List representation of the same array. Therefore, any changes made to the list are actually reflected in the array as well.
Thus, even if the main thread runs after AnotherThread, List<String> m = Arrays.asList(args) will still create a sorted list.
If you like our products and services, please help us by posting your review here.

danidark9312
Posts: 4
Joined: Tue Jul 10, 2018 5:28 pm
Contact:

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

Post by danidark9312 »

daniloj wrote:
Sun Sep 10, 2017 11:00 am
Hi!

Does the fact the variable sorted is NOT volatile imply in the possibility of the static field not being updated in the memory for the main Thread [making the option 'It MAY print nothing' possible]?

If it does not, would it be related to the "happens-before relationship", since it is guaranteed that at least the second time while(!sorted) is called will happen after "AnotherThread" instance had modified the WaitTest.sorted variable?

Thank you,

I still have the same question, I have seen another similar question for this and the actual answer is related with the volatile variable where we can't guarantee that the main thread will see the changes done in a non-volatile variable regardless if its syncronized or not

The answer here ("Since I can't find this in any book or any documentation") is that when you use a syncronized block any thread will see the updated value of a variable even if they are non-volatil ?
Sorry for my english, I'm from Colombia.

If anyone can give any link where I can find this explanation I would appreciate because for me, there's still a volatile issue here since every thread will see a local copy of what the "sorted" non-volatile boolean variable has and the answer would be "it MAY print nothing" instead of "it Will print [a,b,c]", because the second time in the loop that the thread call wait, there won't be any thread to wake it up

Thank you

PD: in the attached image you can see another question with thread and syncronized block, this time the explanation says that since the variable is not volatile the changes to that variable are not guaranteed to ve visibles for the other threads
Attachments
Untitled.png
Untitled.png (33.51 KiB) Viewed 3993 times

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 huge difference between the two codes. In the code of this question (2.1438), the variable sorted is always accessed from within a synchronized block, whereas in question 2.1558, the variable threadCounter is not accessed from a synchronized block.

For a thread to be able to see modifications to a variable done by other threads, either the variable must be volatile or the the threads must access the variable from within a synchronized block.
You need to read about "Java Memory Model" (just google it) from a good book. Even oracle's official Java tutorial is a good start. If the book that you are reading doesn't explain this fundamental concept then I will not recommend that book.

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

danidark9312
Posts: 4
Joined: Tue Jul 10, 2018 5:28 pm
Contact:

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

Post by danidark9312 »

admin wrote:
Wed Jul 11, 2018 9:30 pm
There is a huge difference between the two codes. In the code of this question (2.1438), the variable sorted is always accessed from within a synchronized block, whereas in question 2.1558, the variable threadCounter is not accessed from a synchronized block.

For a thread to be able to see modifications to a variable done by other threads, either the variable must be volatile or the the threads must access the variable from within a synchronized block.
You need to read about "Java Memory Model" (just google it) from a good book. Even oracle's official Java tutorial is a good start. If the book that you are reading doesn't explain this fundamental concept then I will not recommend that book.

HTH,
Paul.
Thank you Paul for replying.

I've googled the concept of "Java Memory Model", but for me that's nothing to do with concurrency, in top of that I'm quite sure those topics about java memory model are not being included in the exam, even in the exam topics at the oracle certified website is not included that concept.

Also before I made the question in this forum I checked the specs about concurrency and syncronized blocks, also the definition of a volatile variable, multiple times and I still couldn't find anything abount this case.

Btw this is my book: learning java. patrick niemeyer daniel leuck 4th edition Java 7

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 »

Not sure what article you read, but you may start here: http://tutorials.jenkov.com/java-concur ... model.html
If the book that you are reading doesn't explain volatile and synchronized, I suggest you use another book.
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 38 guests