About Question enthuware.ocpjp.v7.2.1335 :

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

Moderator: admin

Post Reply
tn1408
Posts: 28
Joined: Wed Dec 04, 2013 7:57 pm
Contact:

About Question enthuware.ocpjp.v7.2.1335 :

Post by tn1408 »

I think #4 is more correct because i1=i2 and k1=k2 is always the case when the print statement in workWithoutLock method is executed. So we are certain that i and k will never be printed.

j1=j2 or j1!=j2: both cases could occur. So j could be printed.

#2 is not wrong but it's a more general case of #4. Also #2 hinted that i and k could be printed because it states that one cannot be certain whether the letters are printed but said nothing about i and k will not be printed???

Thanks,

Tony,

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

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

Post by admin »

I am sorry but I did not understand what you mean.

#4 is not correct because it is possible that i1 is not equal to i2 and/or k1 is not equal to k2 because the workWithoutLocks method does not use locks and so one thread may execute i1 != i2 when the other thread has only done i1++.
-Paul.
If you like our products and services, please help us by posting your review here.

tn1408
Posts: 28
Joined: Wed Dec 04, 2013 7:57 pm
Contact:

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

Post by tn1408 »

I see
Thanks.
Last edited by admin on Thu Apr 03, 2014 8:23 pm, edited 1 time in total.
Reason: fixed the typo in my response that you pointed out

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

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

Post by SepticInsect »

Can you please explain volatile? The explanation I find online is that volatile variables are "sort of" synchronized but it doesn't seem like it in the question.

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

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

Post by admin »

When a regular i.e. non-volatile variable is accessed by two threads, there is no guarantee that one thread will ever see its new value updated by the second thread. So it is possible that one thread updates an int from 1 to 2, but the second thread will always see 1. It will never see 2.

Volatile fixes the above issue. It guarantees that if one thread updates a volatile variable then any other thread will definitely see the new value after the update is complete.

HTH,
Paul.
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.1335 :

Post by SepticInsect »

Code: Select all

public class TestClass extends Thread {
static volatile int var = 0;
public void run() {
  for (int i=0; i<5; i++) {
    var++;
  }
}

public static void main(String args[]) {
  new TestClass().start();
  new TestClass().start();
}
}
So you mean that you can always be certain that var is 10 when both threads have exited in the code above?

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

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

Post by admin »

No, volatile ensures visibility but not timing. So missed updates are still possible. Timing is ensured by synchronization.

Volatile is a lower cost option and is useful when the delay in viewing the updated value is acceptable. For example, if you have a continually running loop in a thread that has to stop after a variable is updated by another thread and if a few extra iterations are ok, you should make the variable volatile.
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.1335 :

Post by SepticInsect »

Ok. Thanks!

jagoneye
Posts: 97
Joined: Wed Dec 28, 2016 9:00 am
Contact:

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

Post by jagoneye »

I just wanna ask here aquiring lock on static object means the same as acquiring lock on the class, right?

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

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

Post by admin »

No, lock on the class means lock on the Class object for that class (See this if you have any confusion about it: http://stackoverflow.com/questions/4453 ... lang-class ).
Lock on a static object is the lock on that particular object (nothing to do with the Class object).
If you like our products and services, please help us by posting your review here.

jagoneye
Posts: 97
Joined: Wed Dec 28, 2016 9:00 am
Contact:

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

Post by jagoneye »

Thanks though I don't want to get into the details of Class since it is used more in reflection and since reflection is not listed in the syllabus I'd rather look into it after the exam.

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

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

Post by horst1a »

I am very puzzled about this question. Are there not two threads each having his own object 'TestClass', so they dont share the instance variables ? Is that right and it is the static declarer of i1,i2.. that makes the threads share these ?

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

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

Post by admin »

Yes, there are two threads created using two TestClass objects. But there are no instance fields in the given code!! Also, instance fields are not shared between instances. Each instance gets its own copy of instance fields
Yes, i1, i2, j1, j2, k1, k2 are all static and are shared by the two TestClass isntances.
If you like our products and services, please help us by posting your review here.

crazymind
Posts: 85
Joined: Mon Dec 24, 2018 6:24 pm
Contact:

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

Post by crazymind »

I try to figure out the behaviour of synchronized(lock1) and synchronized(lock2). Does synchronized on a static monitor will block thread using different object instance and thread using same object instance?
he only way a letter could be printed would be if the method workWithoutLocks() was executed between the time the first and the second variable was incremented.
Do you mean thread1 increment the first variable and thread2 execute condition of if statement?

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

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

Post by admin »

>Does synchronized on a static monitor will block thread using different object instance and thread using same object instance?
Sorry, I am not able to understand what you mean by this.
>Do you mean thread1 increment the first variable and thread2 execute condition of if statement?
Yes.
If you like our products and services, please help us by posting your review here.

crazymind
Posts: 85
Joined: Mon Dec 24, 2018 6:24 pm
Contact:

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

Post by crazymind »

admin wrote:
Tue Mar 12, 2019 10:10 pm
>Does synchronized on a static monitor will block thread using different object instance and thread using same object instance?
Sorry, I am not able to understand what you mean by this.
>Do you mean thread1 increment the first variable and thread2 execute condition of if statement?
Yes.
By that I mean, lock1 is a static field; therefore, it is shared among the instance of TestClass.
If Thread1 and Thread2 are created by same object, Thread1 acquire the lock and access the synchronized block. Thread2 can not access.
If Thread1 and Thread2 are created by different object, Thread1 acquire the lock and access the synchronized block. Thread2 can not access since static lock share among the instances.

So there is no way to access the synchronize block (use static object as a lock) unless the lock is released?

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

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

Post by admin »

There are two synchronized blocks and they are using two different locks - lock1 and lock2.
If you like our products and services, please help us by posting your review here.

crazymind
Posts: 85
Joined: Mon Dec 24, 2018 6:24 pm
Contact:

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

Post by crazymind »

admin wrote:
Tue Mar 12, 2019 10:47 pm
There are two synchronized blocks and they are using two different locks - lock1 and lock2.
Yeah, Sorry, I mean conceptually, if I have a synchronized block use a static lock.

Post Reply

Who is online

Users browsing this forum: No registered users and 62 guests