About Question enthuware.ocpjp.v7.2.1705 :

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

Moderator: admin

Post Reply
ThufirHawat
Posts: 28
Joined: Wed Feb 25, 2015 9:03 am
Contact:

About Question enthuware.ocpjp.v7.2.1705 :

Post by ThufirHawat »

When the answer said:
"The Java memory model does not guarantee any updates made to a field from one thread to be visible to another thread if the field is not accessed through a synchronized block".

First Question:
This is because of the cache system that JVM makes to increase performance? (copying the variables of Thread A in Thread B, and when Thread A changes, no guarantee that Thread B can see the changes)

Second Question:
If we putted

Code: Select all

volatile
key word in the

Code: Select all

public static final StaticSyncTest sst = new StaticSyncTest();
, then, we solve this problem mentioned in First Question?

Despite that making declaration volatile, don't change the answer of this question because static method and instance method still not synchronized each other.

Tnx guys.

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

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

Post by admin »

1. Probably. You may have to go through JLS to understand why they designed it like this.
2. Volatile will ensure that the changes will be visible in another thread eventually. However, volatile does not guarantee the sequence of operations i.e. it cannot guarantee that one thread will block before other thread finishes the operation. For that, you need synchronization.

HTH,
Paul.

krohani
Posts: 31
Joined: Tue Oct 06, 2015 1:57 pm
Contact:

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

Post by krohani »

Paul - If the sum integer was changed to a static variable instead then the answer would still be the same (10 or 20 but not 0, correct? Since it is a static variable and resides on the heap just like an object it is still succeptible to the happens-before relationship and race conditions, right?

I tried to run the code to verify this but on my system I keep getting 20 regardless of whether the integer is static or not so I thought I would ask to ensure that my logic was correct. I also did not see anything about static variables in the happens-before article.

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

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

Post by admin »

That is correct.

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

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

Post by jagoneye »

I have one doubt- does synchronizing static method place a lock only on static methods of the class or on all static variables of the class also?

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

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

Post by admin »

I am not sure I understand your question. When you synchronize a method (any method), it is only that method that is synchronized. If the method that is synchronized is static, then the lock used for synchronizing is the Class object of that class. If the method that is synchronized is an instance method, then the lock used for synchronizing is that particular instance.

I will suggest you to read this topic thoroughly from a book first before attempting questions on this topic.

HTH,
Paul.

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

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

Post by jagoneye »

Code: Select all

public class StaticSyncTest {
    static int sum = 0;
    public synchronized void add(int x){
        sum = sum + x;
    }

    public static synchronized void add(StaticSyncTest sst, int x){
        sst.sum = sst.sum +x;
    }

    public static final StaticSyncTest sst = new StaticSyncTest();
    
    public static void main(String[] args)throws Exception {
        
        Thread t = new Thread(){
            public void run(){
                    StaticSyncTest.add(sst, 10);
            }
        };
        t.start();
        sst.add(10);
        t.join();
        System.out.println(sst.sum);
    }
}
I have studied the topic but after reading krohani's comment, I have this doubt suppose I changed the question's code just by a little to what krohani said ->
make the sum variable static. Now leave aside the output and join method here.
What if two different threads call one non static synchronized method and one static synchronized method at the same time just like it's happening in the question and suppose both these methods change the state of sum variable. Now my question is will both threads corrupt the value of the static variable or will one thread be only able to lock, change its state, release the lock and then let the other thread change the state of the variable?? I have this doubt because non static methods can directly access the static variables of the class. I hope you got my question. If not then I'll try to pin point the steps.

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

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

Post by admin »

They will indeed corrupt the variable because the methods will be using different lock. static method will using the lock of the Class object while the instance method will use that lock of the instance. So both the threads will be able to access the static variable simultaneously and will corrupt it.

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

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

Post by jagoneye »

Thanks! I should have used code in the first place for asking my question. :D
One last thing suppose if this situation is encountered in an application
then what will be the best possible solution?
I guess changing the non-static method's code to this might be one?

Code: Select all

public class StaticSyncTest {
    static int sum = 0;
    public synchronized void add(int x){
        synchronized(StaticSyncTest.class)
        {
        sum = sum + x;
         }
    }

    public static synchronized void add(StaticSyncTest sst, int x){
        sst.sum = sst.sum +x;
    }

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

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

Post by admin »

Yes, this solution will work fine because both the methods are now synchronizing on the same Class object before manipulating the same sum.

thodoris.bais
Posts: 25
Joined: Sat Jun 03, 2017 4:56 pm
Contact:

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

Post by thodoris.bais »

Description says:
Therefore, it is possible for the both threads to update sum simultaneously, which may cause one update to be lost. Thus, sum can be 10 or 20.
Having read both the happens-before documentation and this entire thread, I still cannot understand how an update can be lost.
I mean, even if both threads update sum simultaneously, what I expect is that a happens-before takes place and eventually both updates are executed.

Thodoris

Post Reply

Who is online

Users browsing this forum: No registered users and 5 guests