Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Re: Volatile?

Reply
Thread Tools

Re: Volatile?

 
 
hh
Guest
Posts: n/a
 
      07-01-2003
Knute Johnson wrote:
> Knute Johnson wrote:
>
>> Thanks everybody for the replies. I think I'm going to synchronize my
>> int variable. The problem I'm having that is caused by this is very
>> intermittent but I think that is do to luck.
>>

>
> So now I've come up on some architecture issues. My public volatile int
> is in one class and I access it from another class. Both classes modify
> the int. If I synchronize on "this" in its class and on the reference
> variable from the other class, is it synchronized? Thanks very much.
>
> class a {
> public volatile int x;
>
> public void run() {
> synchronized (this) {
> x = 1234;
> }
> }
> }
>
> class b {
> a aa = new a();
>
> public void run() {
> synchronized (aa) {
> aa.x = 4321;
> }
> }
> }
>



class a
{
private int x;

public synchronized void setX(int value)
{
x = value;
}

public void run()
{
setX(1234);

..
..

 
Reply With Quote
 
 
 
 
Knute Johnson
Guest
Posts: n/a
 
      07-01-2003
hh wrote:
> Knute Johnson wrote:
>
>> Knute Johnson wrote:
>>
>>> Thanks everybody for the replies. I think I'm going to synchronize
>>> my int variable. The problem I'm having that is caused by this is
>>> very intermittent but I think that is do to luck.
>>>

>>
>> So now I've come up on some architecture issues. My public volatile
>> int is in one class and I access it from another class. Both classes
>> modify the int. If I synchronize on "this" in its class and on the
>> reference variable from the other class, is it synchronized? Thanks
>> very much.
>>
>> class a {
>> public volatile int x;
>>
>> public void run() {
>> synchronized (this) {
>> x = 1234;
>> }
>> }
>> }
>>
>> class b {
>> a aa = new a();
>>
>> public void run() {
>> synchronized (aa) {
>> aa.x = 4321;
>> }
>> }
>> }
>>

>
>
> class a
> {
> private int x;
>
> public synchronized void setX(int value)
> {
> x = value;
> }
>
> public void run()
> {
> setX(1234);
>
> ..
> ..
>


I appreciate the response but this doesn't address my question. I want
to know if something in class a synchronized on "this" will be
synchronized with an instance of class a that is referenced from class b.

--

Knute Johnson
email s/nospam/knute/
Molon labe...

 
Reply With Quote
 
 
 
 
Brian Kildea
Guest
Posts: n/a
 
      07-02-2003

"Knute Johnson" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> hh wrote:
> > Knute Johnson wrote:
> >
> >> Knute Johnson wrote:
> >>
> >>> Thanks everybody for the replies. I think I'm going to synchronize
> >>> my int variable. The problem I'm having that is caused by this is
> >>> very intermittent but I think that is do to luck.
> >>>
> >>
> >> So now I've come up on some architecture issues. My public volatile
> >> int is in one class and I access it from another class. Both classes
> >> modify the int. If I synchronize on "this" in its class and on the
> >> reference variable from the other class, is it synchronized? Thanks
> >> very much.
> >>
> >> class a {
> >> public volatile int x;
> >>
> >> public void run() {
> >> synchronized (this) {
> >> x = 1234;
> >> }
> >> }
> >> }
> >>
> >> class b {
> >> a aa = new a();
> >>
> >> public void run() {
> >> synchronized (aa) {
> >> aa.x = 4321;
> >> }
> >> }
> >> }
> >>

> >
> >
> > class a
> > {
> > private int x;
> >
> > public synchronized void setX(int value)
> > {
> > x = value;
> > }
> >
> > public void run()
> > {
> > setX(1234);
> >
> > ..
> > ..
> >

>
> I appreciate the response but this doesn't address my question. I want
> to know if something in class a synchronized on "this" will be
> synchronized with an instance of class a that is referenced from class b.
>
> --
>
> Knute Johnson
> email s/nospam/knute/
> Molon labe...
>


As long as you synchronize the setX method on this, all should be well. You
do not need to synchronize on aa in class b before calling setX from class
b, although this should work, and have no ill effect.

Brian


 
Reply With Quote
 
Xavier Tarrago
Guest
Posts: n/a
 
      07-02-2003
I dont know but anyway, it seems to be a bad practice to lock an object fron
outside.

class a {
public volatile int x;

public void run() {
synchronized (this) {
x = 1234;
}
}
public synchronized void setX( x_) {
x = x_;
}
}

class b {
a aa = new a();

public void run() {
aa.setX(4321);
}
}
}



"Knute Johnson" <(E-Mail Removed)> a écrit dans le message news:
http://www.velocityreviews.com/forums/(E-Mail Removed)...
> Knute Johnson wrote:
> > Thanks everybody for the replies. I think I'm going to synchronize my
> > int variable. The problem I'm having that is caused by this is very
> > intermittent but I think that is do to luck.
> >

>
> So now I've come up on some architecture issues. My public volatile int
> is in one class and I access it from another class. Both classes modify
> the int. If I synchronize on "this" in its class and on the reference
> variable from the other class, is it synchronized? Thanks very much.
>
> class a {
> public volatile int x;
>
> public void run() {
> synchronized (this) {
> x = 1234;
> }
> }
> }
>
> class b {
> a aa = new a();
>
> public void run() {
> synchronized (aa) {
> aa.x = 4321;
> }
> }
> }
>
> --
>
> Knute Johnson
> email s/nospam/knute/
> Molon labe...
>



 
Reply With Quote
 
Gordon Beaton
Guest
Posts: n/a
 
      07-02-2003
On Tue, 01 Jul 2003 16:34:36 -0700, Knute Johnson wrote:
> I appreciate the response but this doesn't address my question. I
> want to know if something in class a synchronized on "this" will be
> synchronized with an instance of class a that is referenced from
> class b.


It doesn't matter how you refer to it. If "this" and the instance of a
referenced from b are in fact the same object, then yes, they will be
mutually synchronized.

That said, the better solution is to use accessor methods inside a,
and provide the necessary synchronization there instead of requiring
that users of the class know about its synchronization needs.

/gordon

--
[ do not send me private copies of your followups ]
g o r d o n . b e a t o n @ e r i c s s o n . c o m
 
Reply With Quote
 
Knute Johnson
Guest
Posts: n/a
 
      07-02-2003
Steve Horsley wrote:
> On Tue, 01 Jul 2003 09:56:56 -0700, Knute Johnson wrote:
>
>
>>Knute Johnson wrote:
>>
>>>Thanks everybody for the replies. I think I'm going to synchronize my
>>>int variable. The problem I'm having that is caused by this is very
>>>intermittent but I think that is do to luck.
>>>
>>>

>>
>>So now I've come up on some architecture issues. My public volatile int
>>is in one class and I access it from another class. Both classes modify
>>the int. If I synchronize on "this" in its class and on the reference
>>variable from the other class, is it synchronized? Thanks very much.
>>
>>class a {
>> public volatile int x;
>>
>> public void run() {
>> synchronized (this) {
>> x = 1234;
>> }
>> }
>> }
>>}
>>class b {
>> a aa = new a();
>>
>> public void run() {
>> synchronized (aa) {
>> aa.x = 4321;
>> }
>> }
>> }
>>}

>
>
> Sometimes you have to synchronize on a foreign Object, for instance if you
> are performing a series of operations on it and don't want it accessed in
> mid-sequence by another thread. In this simple case though, I would be
> inclined to make a.x private and fit class a with public synchronized
> accessor methods getX() and setX(int). Always use those synchronized
> methods even from inside class a's code and you're bomb-proof.
>
> Steve


Thanks Steve and everybody for the replies. I've sort of gotten myself
into a pickle with this app. I need to be able to increment and
decrement my int as well as use it as a limit in a for loop from outside
of its class. The accessor methods won't really work for that. Maybe
it would make more sense to take it out of its class and put it in a
globals class and synchronize on that class for access. This is
starting to sound like one of those bizarre situations that are in the
Multi-Threading book.

Thanks,


--

Knute Johnson
email s/nospam/knute/
Molon labe...

 
Reply With Quote
 
Keeger
Guest
Posts: n/a
 
      07-02-2003
Knute Johnson <(E-Mail Removed)> wrote in message news:<(E-Mail Removed)>...
> Steve Horsley wrote:
> > On Tue, 01 Jul 2003 09:56:56 -0700, Knute Johnson wrote:
> >
> >
> >>Knute Johnson wrote:
> >>
> >>>Thanks everybody for the replies. I think I'm going to synchronize my
> >>>int variable. The problem I'm having that is caused by this is very
> >>>intermittent but I think that is do to luck.
> >>>
> >>>
> >>
> >>So now I've come up on some architecture issues. My public volatile int
> >>is in one class and I access it from another class. Both classes modify
> >>the int. If I synchronize on "this" in its class and on the reference
> >>variable from the other class, is it synchronized? Thanks very much.
> >>
> >>class a {
> >> public volatile int x;
> >>
> >> public void run() {
> >> synchronized (this) {
> >> x = 1234;
> >> }
> >> }
> >> }
> >>}
> >>class b {
> >> a aa = new a();
> >>
> >> public void run() {
> >> synchronized (aa) {
> >> aa.x = 4321;
> >> }
> >> }
> >> }
> >>}

> >
> >
> > Sometimes you have to synchronize on a foreign Object, for instance if you
> > are performing a series of operations on it and don't want it accessed in
> > mid-sequence by another thread. In this simple case though, I would be
> > inclined to make a.x private and fit class a with public synchronized
> > accessor methods getX() and setX(int). Always use those synchronized
> > methods even from inside class a's code and you're bomb-proof.
> >
> > Steve

>
> Thanks Steve and everybody for the replies. I've sort of gotten myself
> into a pickle with this app. I need to be able to increment and
> decrement my int as well as use it as a limit in a for loop from outside
> of its class. The accessor methods won't really work for that. Maybe
> it would make more sense to take it out of its class and put it in a
> globals class and synchronize on that class for access. This is
> starting to sound like one of those bizarre situations that are in the
> Multi-Threading book.
>
> Thanks,


maybe you should provide us with the exact problem you are trying to
solve. It seems to me you're running into more of a design issue than
implementation.
 
Reply With Quote
 
Knute Johnson
Guest
Posts: n/a
 
      07-03-2003
Keeger wrote:
>
> maybe you should provide us with the exact problem you are trying to
> solve. It seems to me you're running into more of a design issue than
> implementation.


You are right I wouldn't have these problems if the architecture wasn't
totally fubared. This job was 7/8ths done and the client wanted it to
work completely differently and didn't want to pay more for it. I was
stuck having to make do with a lot of existing code and stuff scattered
all over in the silly thing. The really hard part is trying to get it
to not work correctly. I've been chasing around this synchronization
bug for weeks and have had it crop up about 4 times. The app is in use
ten or twelve hours a day and once in a while somebody can make it go south.

I have however learned a lot about volatile and synchronization that I
didn't before I started this.

--

Knute Johnson
email s/nospam/knute/
Molon labe...

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off




Advertisments