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

Moderator: admin

Post Reply
kka

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

Post 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.

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

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

Post 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.
If you like our products and services, please help us by posting your review here.

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

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

Post by admin »

This has been fixed.

thanks for your feedback!
If you like our products and services, please help us by posting your review here.

Emir Cortes

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

Post 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.

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

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

Post 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.
If you like our products and services, please help us by posting your review here.

roxy

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

Post by roxy »

What would happen if all methods had lock type = WRITE?

Thanks!

PMiglani
Posts: 14
Joined: Sat Jan 19, 2013 8:42 am
Contact:

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

Post 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.

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

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

Post 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.
If you like our products and services, please help us by posting your review here.

himaiMinh
Posts: 358
Joined: Fri Nov 29, 2013 8:26 pm
Contact:

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

Post 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.

Post Reply

Who is online

Users browsing this forum: No registered users and 37 guests