About Question enthuware.ocpjp.v7.2.1335 :
Moderator: admin
-
- Posts: 97
- Joined: Wed Dec 28, 2016 9:00 am
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1335 :
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.
-
- Posts: 37
- Joined: Mon Jun 12, 2017 2:16 am
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1335 :
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 ?
-
- Site Admin
- Posts: 10036
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1335 :
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.
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.
-
- Posts: 85
- Joined: Mon Dec 24, 2018 6:24 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1335 :
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?
Do you mean thread1 increment the first variable and thread2 execute condition of if statement?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.
-
- Site Admin
- Posts: 10036
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1335 :
>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.
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.
-
- Posts: 85
- Joined: Mon Dec 24, 2018 6:24 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1335 :
By that I mean, lock1 is a static field; therefore, it is shared among the instance of TestClass.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.
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?
-
- Site Admin
- Posts: 10036
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1335 :
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.
-
- Posts: 85
- Joined: Mon Dec 24, 2018 6:24 pm
- Contact:
Who is online
Users browsing this forum: Google [Bot] and 32 guests