Page 1 of 1

About Question enthuware.oce-ejbd.v6.2.373 :

Posted: Fri May 13, 2011 4:18 am
by kka

Code: Select all

    
@Singleton
@LocalBean
@Lock(LockType.WRITE)
public class CrazySingletonBean {
    String s1 = "x";
    String s2 = "y";

    public void setStrings(){
        s1 = "x"; s2 = "y1";
    }

    public void clearStrings(){
        s1 = ""; s2 = "";
    }
 
    @Lock(LockType.READ)
    public String getValue(){
        return s1+" "+s2;
    }
}
What about this situation:
1. Thread_A executes getVaule(), but it is suspended right after concatening s1 and " " ("x" + " ")
2. then Thread_B executes clearStrings() - method completes (because it have LockType.WRITE)
3. Thread_A reasume and continue with concatenation s2 but now it has "" and when conacatenation completes it result is "x "

Both types of method (writing and reading variables) should have lock type = WRITE.

Re: About Question enthuware.oce-ejbd.v6.2.373 :

Posted: Fri May 13, 2011 5:50 am
by admin
Hello,
If one thread gets a Read lock on the bean other threads cannot get a Write lock. So the situation that you've described cannot occur.

HTH,
Paul.

Re: About Question enthuware.oce-ejbd.v6.2.373 :

Posted: Sun Jun 12, 2011 1:26 pm
by admin
This has been fixed.

thanks for your feedback!

Re: About Question enthuware.oce-ejbd.v6.2.373 :

Posted: Mon Mar 19, 2012 10:24 pm
by Emir Cortes
While it's true that << Some clients may always get "x y" while some may always get " ".>>, the answer << "x y" or " " >> is correct too.

Re: About Question enthuware.oce-ejbd.v6.2.373 :

Posted: Tue Mar 20, 2012 4:25 am
by admin
Hi, The correct answer is <<"x y" or " ">> as given in the question. Option 4 << Some clients may always get "x y" while some may always get " ". >> is not correct because the same client may not always get the same value.

HTH,
Paul.

Re: About Question enthuware.oce-ejbd.v6.2.373 :

Posted: Sat Jan 12, 2013 2:04 pm
by roxy
What would happen if all methods had lock type = WRITE?

Thanks!

Re: About Question enthuware.oce-ejbd.v6.2.373 :

Posted: Wed Dec 04, 2013 8:31 pm
by PMiglani
Please explain why if one thread gets a Read lock on getValue() method, the other thread can't get write lock on setStrings() or clearStrings() method.

Re: About Question enthuware.oce-ejbd.v6.2.373 :

Posted: Mon Dec 09, 2013 12:40 am
by admin
PMiglani wrote:Please explain why if one thread gets a Read lock on getValue() method, the other thread can't get write lock on setStrings() or clearStrings() method.
This is as per section 4.8.5.1:
If the container invokes a method associated with a Write lock, no other concurrent invocations will be allowed to proceed until the initial Write method’s processing completes.
HTH,
Paul.

Re: About Question enthuware.oce-ejbd.v6.2.373 :

Posted: Mon Apr 27, 2015 10:27 am
by himaiMinh
Let me explain the above question:
When the "Read" lock is acquired, the "Write" lock cannot be acquired until the "Read" lock is released.

However, the "Read" lock can be acquired by multiple clients or threads while the "Write" lock is only acquired by one thread.
The "Write" lock can be acquired once all the clients or threads release the "Read" lock.