|
|
Welcome to the Java Forums.
You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:
- have access to post topics
- communicate privately with other members (PM)
- not see advertisements between posts
- have the possibility to earn one of our surprises if you are an active member
- access many other special features that will be introduced later.
Registration is fast, simple and absolutely free so please, join our community today!
If you have any problems with the registration process or your account login, please contact us.
|
|

10-07-2008, 06:41 PM
|
|
Member
|
|
Join Date: Oct 2008
Posts: 8
|
|
|
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, 07:55 PM
|
|
Senior Member
|
|
Join Date: Jun 2008
Posts: 481
|
|
|
|
|

10-07-2008, 08:07 PM
|
|
Member
|
|
Join Date: Oct 2008
Posts: 8
|
|
|
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, 09:48 PM
|
|
Senior Member
|
|
Join Date: Jun 2008
Posts: 481
|
|
|
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, 09:53 PM
|
|
Member
|
|
Join Date: Oct 2008
Posts: 8
|
|
|
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 09:58 PM.
|
|

10-07-2008, 10:47 PM
|
 |
Senior Member
|
|
Join Date: Jun 2008
Location: Heredia, Costa Rica
Posts: 2,223
|
|
|
Can you add some comments to your code to describe what it is supposed to do?
|
|

10-07-2008, 10:55 PM
|
|
Member
|
|
Join Date: Oct 2008
Posts: 8
|
|
|
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, 11:48 PM
|
|
Senior Member
|
|
Join Date: Jun 2008
Posts: 481
|
|
Originally Posted by mrhyman
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.
|
|

10-07-2008, 11:51 PM
|
|
Senior Member
|
|
Join Date: Jun 2008
Posts: 481
|
|
Originally Posted by mrhyman
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-08-2008, 03:52 AM
|
|
Member
|
|
Join Date: Oct 2008
Posts: 8
|
|
|
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, 09:34 AM
|
|
Senior Member
|
|
Join Date: Jun 2008
Posts: 481
|
|
Originally Posted by mrhyman
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".
|
|

10-10-2008, 10:38 PM
|
|
Member
|
|
Join Date: Oct 2008
Posts: 8
|
|
|
Good. If you're happy with your post. Please move on to another one.
|
|

10-10-2008, 10:53 PM
|
 |
Moderator
|
|
Join Date: Dec 2007
Location: NewEngland, US
Posts: 841
|
|
|
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.
__________________
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. to our beloved Java Forums! (closes on September 4, 2008)
Want to voice your opinion on your IDE/Editor of choice? To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. !
Got a little Capt'n in you? (drink responsibly)
|
|

10-10-2008, 10:55 PM
|
|
Member
|
|
Join Date: Oct 2008
Posts: 8
|
|
|
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, 11:01 PM
|
|
Member
|
|
Join Date: Oct 2008
Posts: 8
|
|
|
I also suggest to close this thread. This is useless to other members as nothing has been solved here. Thanks.
|
|

10-24-2008, 10:55 PM
|
|
Member
|
|
Join Date: Oct 2008
Posts: 4
|
|
|
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, 11:57 PM
|
 |
Senior Member
|
|
Join Date: Jun 2008
Location: Heredia, Costa Rica
Posts: 2,223
|
|
|
sengupta_piyal06 - Create a new thread with your own subject.
Your topic has NOTHNG to do with threads.
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|