You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@tuscany.apache.org by Antonio Mirarchi <an...@gmail.com> on 2009/09/20 06:00:11 UTC

Synchronized Method

Hi, i have a question about invocation method between components; i have
three component A,B,C. The component A have two remotable and OneWay methods
Increments and Decrements, Increments call a private method inc to
increments private variable in A implementation, and Decrements  call a
private method dec to decrements the same variable, if B  and C call one of
this method at same time is it correct to define the private method inc and
dec as synchronized to obtain a synchronized access to the private variable
? Thank for your answer

Service A

@Remotable
public interface Aservice{

@OneWay
   void Increments();
@OneWay
   void Decrements();
}
/--------------------------------------------------------------/
@Scope("COMPOSITE")
@EagerInit
public class AserviceImpl implements Aservice{

private int i;

@Init
 public void start() { i=0;}

private void inc(){i++;} //<------this may be  synchronized?

private void dec(){i--;}   //<------this may be synchronized?


public void Increments(){inc();}
public void Decrements(){dec();}
}


Service B

@Remotable
public interface Bservice{
   void Bmethod();
}
/--------------------------------------------------------------/
@Scope("COMPOSITE")

public class BserviceImpl implements Bservice{
@Reference
Aservice aservice;

public void Bmethod(){
.
.
aservice.Increments();
.
.
}

}

Service C

@Remotable
public interface Cservice{
   void Cmethod();
}
/--------------------------------------------------------------/
@Scope("COMPOSITE")

public class CserviceImpl implements Cservice{
@Reference
Aservice aservice;

public void Cmethod(){
.
.
aservice.Decrements();
.
.
}

}

RE: Synchronized Method

Posted by "Phillips, Chad" <Ch...@gdit.com>.
If both inc/dec were synchronized you'd be fine since they would be synchronized on the same object (this); so, you would never have an increment and a decrement happening at the same time.  If you're using Java 1.5+ you could also use the java.util.concurrent.atomic.AtomicInteger class to accomplish this as well w/o explicit synchronization or additional private methods.

________________________________
From: Antonio Mirarchi [mailto:antonio.mirarchi@gmail.com]
Sent: Monday, September 21, 2009 03:40
To: user@tuscany.apache.org
Subject: Re: Synchronized Method

Thank for your answer, but if don't mark Increments and Decrements with synchronize, is there a problem of accessing to the same variable at the same time from two different method ?
2009/9/21 Simon Nash <na...@apache.org>>
Antonio Mirarchi wrote:
Hi, i have a question about invocation method between components; i have three component A,B,C. The component A have two remotable and OneWay methods Increments and Decrements, Increments call a private method inc to increments private variable in A implementation, and Decrements  call a private method dec to decrements the same variable, if B  and C call one of this method at same time is it correct to define the private method inc and dec as synchronized to obtain a synchronized access to the private variable  ? Thank for your answer
Hi Antonio,
There shouldn't be any problem with making the private methods
synchronized.  Because AServiceImpl is composite scoped, the Tuscany
runtime will not synchronize calls to Increments() and Decrements().

 Simon

Service A

@Remotable
public interface Aservice{

@OneWay
  void Increments();
@OneWay
  void Decrements();
}
/--------------------------------------------------------------/
@Scope("COMPOSITE")
@EagerInit
public class AserviceImpl implements Aservice{

private int i;

@Init
 public void start() { i=0;}

private void inc(){i++;} //<------this may be  synchronized?

private void dec(){i--;}   //<------this may be synchronized?


public void Increments(){inc();}
public void Decrements(){dec();}
}


Service B

@Remotable
public interface Bservice{
  void Bmethod();
}
/--------------------------------------------------------------/
@Scope("COMPOSITE")

public class BserviceImpl implements Bservice{
@Reference
Aservice aservice;

public void Bmethod(){
.
.
aservice.Increments();
.
.
}

}

Service C

@Remotable
public interface Cservice{
  void Cmethod();
}
/--------------------------------------------------------------/
@Scope("COMPOSITE")

public class CserviceImpl implements Cservice{
@Reference
Aservice aservice;

public void Cmethod(){
.
.
aservice.Decrements();
.
.
}

}



Re: Synchronized Method

Posted by Antonio Mirarchi <an...@gmail.com>.
Thank for your answer, but if don't mark Increments and Decrements with
synchronize, is there a problem of accessing to the same variable at the
same time from two different method ?

2009/9/21 Simon Nash <na...@apache.org>

> Antonio Mirarchi wrote:
>
>> Hi, i have a question about invocation method between components; i have
>> three component A,B,C. The component A have two remotable and OneWay methods
>> Increments and Decrements, Increments call a private method inc to
>> increments private variable in A implementation, and Decrements  call a
>> private method dec to decrements the same variable, if B  and C call one of
>> this method at same time is it correct to define the private method inc and
>> dec as synchronized to obtain a synchronized access to the private variable
>>  ? Thank for your answer
>>
>>  Hi Antonio,
> There shouldn't be any problem with making the private methods
> synchronized.  Because AServiceImpl is composite scoped, the Tuscany
> runtime will not synchronize calls to Increments() and Decrements().
>
>  Simon
>
>
>  Service A
>>
>> @Remotable
>> public interface Aservice{
>>
>> @OneWay
>>   void Increments();
>> @OneWay
>>   void Decrements();
>> }
>> /--------------------------------------------------------------/
>> @Scope("COMPOSITE")
>> @EagerInit
>> public class AserviceImpl implements Aservice{
>>
>> private int i;
>>
>> @Init
>>  public void start() { i=0;}
>>
>> private void inc(){i++;} //<------this may be  synchronized?
>>
>> private void dec(){i--;}   //<------this may be synchronized?
>>
>>
>> public void Increments(){inc();}
>> public void Decrements(){dec();}
>> }
>>
>>
>> Service B
>>
>> @Remotable
>> public interface Bservice{
>>   void Bmethod();
>> }
>> /--------------------------------------------------------------/
>> @Scope("COMPOSITE")
>>
>> public class BserviceImpl implements Bservice{
>> @Reference
>> Aservice aservice;
>>
>> public void Bmethod(){
>> .
>> .
>> aservice.Increments();
>> .
>> .
>> }
>>
>> }
>>
>> Service C
>>
>> @Remotable
>> public interface Cservice{
>>   void Cmethod();
>> }
>> /--------------------------------------------------------------/
>> @Scope("COMPOSITE")
>>
>> public class CserviceImpl implements Cservice{
>> @Reference
>> Aservice aservice;
>>
>> public void Cmethod(){
>> .
>> .
>> aservice.Decrements();
>> .
>> .
>> }
>>
>> }
>>
>>
>

Re: Synchronized Method

Posted by Simon Nash <na...@apache.org>.
Antonio Mirarchi wrote:
> Hi, i have a question about invocation method between components; i have 
> three component A,B,C. The component A have two remotable and OneWay 
> methods Increments and Decrements, Increments call a private method inc 
> to increments private variable in A implementation, and Decrements  call 
> a private method dec to decrements the same variable, if B  and C call 
> one of this method at same time is it correct to define the private 
> method inc and dec as synchronized to obtain a synchronized access to 
> the private variable  ? Thank for your answer
> 
Hi Antonio,
There shouldn't be any problem with making the private methods
synchronized.  Because AServiceImpl is composite scoped, the Tuscany
runtime will not synchronize calls to Increments() and Decrements().

   Simon

> Service A
> 
> @Remotable
> public interface Aservice{
> 
> @OneWay
>    void Increments();
> @OneWay
>    void Decrements();
> }
> /--------------------------------------------------------------/
> @Scope("COMPOSITE")
> @EagerInit
> public class AserviceImpl implements Aservice{
> 
> private int i;
> 
> @Init
>  public void start() { i=0;}
> 
> private void inc(){i++;} //<------this may be  synchronized?
> 
> private void dec(){i--;}   //<------this may be synchronized?
> 
> 
> public void Increments(){inc();}
> public void Decrements(){dec();}
> }
> 
> 
> Service B
> 
> @Remotable
> public interface Bservice{
>    void Bmethod();
> }
> /--------------------------------------------------------------/
> @Scope("COMPOSITE")
> 
> public class BserviceImpl implements Bservice{
> @Reference
> Aservice aservice;
> 
> public void Bmethod(){
> .
> .
> aservice.Increments();
> .
> .
> }
> 
> }
> 
> Service C
> 
> @Remotable
> public interface Cservice{
>    void Cmethod();
> }
> /--------------------------------------------------------------/
> @Scope("COMPOSITE")
> 
> public class CserviceImpl implements Cservice{
> @Reference
> Aservice aservice;
> 
> public void Cmethod(){
> .
> .
> aservice.Decrements();
> .
> .
> }
> 
> }
>