Results 1 to 17 of 17
Thread: Thread Safe Methods / Locks
- 10-07-2008, 04:41 PM #1
Member
- Join Date
- Oct 2008
- Posts
- 8
- Rep Power
- 0
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
}
- 10-07-2008, 05:55 PM #2
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
- 10-07-2008, 06:07 PM #3
Member
- Join Date
- Oct 2008
- Posts
- 8
- Rep Power
- 0
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;
}
}
- 10-07-2008, 07:48 PM #4
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
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).
- 10-07-2008, 07:53 PM #5
Member
- Join Date
- Oct 2008
- Posts
- 8
- Rep Power
- 0
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.
- 10-07-2008, 08:47 PM #6
Can you add some comments to your code to describe what it is supposed to do?
- 10-07-2008, 08:55 PM #7
Member
- Join Date
- Oct 2008
- Posts
- 8
- Rep Power
- 0
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.
- 10-07-2008, 09:48 PM #8
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
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.
- 10-07-2008, 09:51 PM #9
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
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-08-2008, 01:52 AM #10
Member
- Join Date
- Oct 2008
- Posts
- 8
- Rep Power
- 0
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.
- 10-08-2008, 07:34 AM #11
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
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.
Good for you. Maybe next time you will be a bit more careful with how you phrase your "questions".My program is done and I am here to gain experience from people who really have experience.
- 10-10-2008, 08:38 PM #12
Member
- Join Date
- Oct 2008
- Posts
- 8
- Rep Power
- 0
Good. If you're happy with your post. Please move on to another one.
- 10-10-2008, 08:53 PM #13
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)
- 10-10-2008, 08:55 PM #14
Member
- Join Date
- Oct 2008
- Posts
- 8
- Rep Power
- 0
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.
- 10-10-2008, 09:01 PM #15
Member
- Join Date
- Oct 2008
- Posts
- 8
- Rep Power
- 0
I also suggest to close this thread. This is useless to other members as nothing has been solved here. Thanks.
- 10-24-2008, 08:55 PM #16
Member
- Join Date
- Oct 2008
- Posts
- 4
- Rep Power
- 0
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:)
- 10-24-2008, 09:57 PM #17
Similar Threads
-
Trouble with static methods and boolean equals() methods with classes
By dreamingofgreen in forum New To JavaReplies: 8Last Post: 04-16-2012, 11:00 PM -
Are Local variables thread safe ?
By samson in forum Threads and SynchronizationReplies: 6Last Post: 12-21-2010, 02:34 PM -
The safe way to stop a thread
By Java Tip in forum java.langReplies: 0Last Post: 04-09-2008, 06:31 PM -
Struts framework. Is this thread safe?
By JavaAl2 in forum Web FrameworksReplies: 1Last Post: 01-17-2008, 03:01 AM -
Local Variables for a static method - thread safe?
By mikeg1z in forum Advanced JavaReplies: 1Last Post: 11-16-2007, 01:06 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks