Results 1 to 17 of 17
  1. #1
    mrhyman is offline Member
    Join Date
    Oct 2008
    Posts
    8
    Rep Power
    0

    Exclamation Thread Safe Methods / Locks

    I am new to java and I have a question about synchronizing two methods that updates some variables. I am not sure how to perform this operation so that it is thread safe. I cannot use Synchronize Method itself, rather I should use Semaphores or Lock(but avoid Deadlock).

    Any help would be appreciated.


    buyItem() {
    ....

    sellItem();


    //do some transaction here
    //Example Update Account Balance
    ....
    }


    sellItem() {
    //do some transaction here
    //Update Account Balance
    }

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    8

  3. #3
    mrhyman is offline Member
    Join Date
    Oct 2008
    Posts
    8
    Rep Power
    0

    Exclamation

    Thanks but actually I was more looking for a confirmation or example how to do this in my sitation. I had already read the Concurrency part on the Sun Page but as I said, I am new to java.

    Here's how I am thinking of doing it:

    private float Balance = float (1.0);
    private Object Lock1 = new Object();

    Public buyitem(){

    synchronized(Lock1){
    Balance = Balance / 2;
    sellitem();
    }


    public sellitem(){

    synchronized(Lock1){
    Balance = Balance * 2;
    }
    }

  4. #4
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    8

    Default

    So, you were expecting us to do it for you?

    Well, that what you have seems okay. But what I think they were really getting at was the new Lock objects as of 1.5 Look through the concurrency tutorial again (the "high level concurrency" part).

  5. #5
    mrhyman is offline Member
    Join Date
    Oct 2008
    Posts
    8
    Rep Power
    0

    Default

    Listen, I never asked anybody to write anything for me. I would suggest you refrain to reply to my post if you cannot understand my point here.

    I went through the tutorial already and I need some advice like would it be better to use Semaphores than locks, would it be better to use 1 or 2 locks since I want to obtain maximum of cuncurrent programming.
    Last edited by mrhyman; 10-07-2008 at 07:58 PM.

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,585
    Rep Power
    23

    Default

    Can you add some comments to your code to describe what it is supposed to do?

  7. #7
    mrhyman is offline Member
    Join Date
    Oct 2008
    Posts
    8
    Rep Power
    0

    Default

    Hi Norm,

    Sure I can do that:

    I have a client server program I have

    On the server there is the

    Balance Variable

    The Buyitem(Item,Quantity) method
    the Sellitem(item,Quantity) method

    The client can invoke

    Buyitem which looks like this:

    public buyitem(item,quantity){

    if (balance > quantity * item){
    //Ask seller's server to sell
    sellitem(item,quantity)
    balance = balance - quantity * seller.Getprice();

    }

    the sellitem method looks like

    public sellitem(item,quantity){

    balance = balance + Quantity * price

    }

    I need to make sure that the variable balance, quantity, etc on the server are synchronized with the correct calls.

    There can be multiple servers: each managing their own balance and quantity

    there exists the same number of clients as servers and the clients calls only the buyitem method which is directed towards the correct server who will sell his item.

    therefore if I have 3 client/servers, 2 clients can invoke the buy method on the 3rd server.

    I hope this makes it a bit clearer.

  8. #8
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    8

    Default

    Quote Originally Posted by mrhyman View Post
    Listen, I never asked anybody to write anything for me. I would suggest you refrain to reply to my post if you cannot understand my point here.
    Not in so many words, but you posted a code with no sync, with your assignment, and said you wanted help with sync, so I pointed you to the tutorial. You came back and said "I was looking for help with my code". IOW I was looking for somebody to add the missing pieces for me.

  9. #9
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    8

    Default

    Quote Originally Posted by mrhyman View Post
    Hi Norm,

    Sure I can do that:

    I have a client server program I have

    On the server there is the

    Balance Variable

    The Buyitem(Item,Quantity) method
    the Sellitem(item,Quantity) method

    The client can invoke

    Buyitem which looks like this:

    public buyitem(item,quantity){

    if (balance > quantity * item){
    //Ask seller's server to sell
    sellitem(item,quantity)
    balance = balance - quantity * seller.Getprice();

    }

    the sellitem method looks like

    public sellitem(item,quantity){

    balance = balance + Quantity * price

    }

    I need to make sure that the variable balance, quantity, etc on the server are synchronized with the correct calls.

    There can be multiple servers: each managing their own balance and quantity

    there exists the same number of clients as servers and the clients calls only the buyitem method which is directed towards the correct server who will sell his item.

    therefore if I have 3 client/servers, 2 clients can invoke the buy method on the 3rd server.

    I hope this makes it a bit clearer.

    Okay, and like said, what you had looked okay (not neccessarily the way I would do it, but acceptable), but that due to the description you posted in the first sentence of your first post, they were probably looking for you to use the newer Lock objects. Try it with those, the tutorial has a few examples.

  10. #10
    mrhyman is offline Member
    Join Date
    Oct 2008
    Posts
    8
    Rep Power
    0

    Default

    masijade, please I am here to lean and obtain valuable help. If you are simply going to refer me on and on to the Sun tutorial, please do not post again. You started on the wrong foot here thinking that I want people to do my stuffs. My program is done and I am here to gain experience from people who really have experience.

  11. #11
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    8

    Default

    Quote Originally Posted by mrhyman View Post
    masijade, please I am here to lean and obtain valuable help. If you are simply going to refer me on and on to the Sun tutorial, please do not post again. You started on the wrong foot here thinking that I want people to do my stuffs.
    I did not start out on the wrong foot here. You did. The way you worded your first post, and the first line of your second post (whether you meant it that way or not) was the classic "do this for me" request. That was the reason you were pointed to the tutorial, to make you do it for yourself. It was maybe only a gaffe on your part, but it was on your part.

    My program is done and I am here to gain experience from people who really have experience.
    Good for you. Maybe next time you will be a bit more careful with how you phrase your "questions".

  12. #12
    mrhyman is offline Member
    Join Date
    Oct 2008
    Posts
    8
    Rep Power
    0

    Default

    Good. If you're happy with your post. Please move on to another one.

  13. #13
    CaptainMorgan's Avatar
    CaptainMorgan is offline Moderator
    Join Date
    Dec 2007
    Location
    NewEngland, US
    Posts
    835
    Rep Power
    8

    Default

    mrhyman, masijade was simply pointing out the obvious we've seen here time and time again. I would suggest, as you're new, that you don't respond to posts you do not like. Otherwise, masijade's advice is spot on.

    I am not on either side as I don't beleive you were explicitly asking us to do your work. But one more peep out of either of you that is not on topic and I'll close this thread. Thus, if you wish to continue discussing sync'ing, stay on topic please.
    Vote for the new slogan to our beloved Java Forums! (closes on September 4, 2008)
    Want to voice your opinion on your IDE/Editor of choice? Vote now!
    Got a little Capt'n in you? (drink responsibly)

  14. #14
    mrhyman is offline Member
    Join Date
    Oct 2008
    Posts
    8
    Rep Power
    0

    Default

    Allright let's close this topic then. It seems there is a lot of misunderstanding here and we all have more valuable things to do. Shame on people who want stuffs to be done for them.

  15. #15
    mrhyman is offline Member
    Join Date
    Oct 2008
    Posts
    8
    Rep Power
    0

    Default

    I also suggest to close this thread. This is useless to other members as nothing has been solved here. Thanks.

  16. #16
    Join Date
    Oct 2008
    Posts
    4
    Rep Power
    0

    Red face please help me

    hello friends,

    this is my 1st post to all of u.
    I want to select text from jtext area and wants to change the color using jcolor chooser.
    how i can do it please provide me help:)

  17. #17
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,585
    Rep Power
    23

    Default

    sengupta_piyal06 - Create a new thread with your own subject.
    Your topic has NOTHNG to do with threads.

Similar Threads

  1. Replies: 8
    Last Post: 04-16-2012, 11:00 PM
  2. Are Local variables thread safe ?
    By samson in forum Threads and Synchronization
    Replies: 6
    Last Post: 12-21-2010, 02:34 PM
  3. The safe way to stop a thread
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-09-2008, 06:31 PM
  4. Struts framework. Is this thread safe?
    By JavaAl2 in forum Web Frameworks
    Replies: 1
    Last Post: 01-17-2008, 03:01 AM
  5. Local Variables for a static method - thread safe?
    By mikeg1z in forum Advanced Java
    Replies: 1
    Last Post: 11-16-2007, 01:06 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •