do I need synchronized for java bean method?
2 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Zhang_Zhang
Posted On:   Wednesday, June 8, 2005 09:30 PM

			
in today's web application, we use a lot of java beans at web tier like:
private String abc;
public String getAbc(){
return abc;
}
public void setAbc(String a){
abc = a;
}

realized there is multi thread safe issue in java servlet, do I need to put synchronized modifier for each method?
I never seen any example do that. why?

Re: do I need synchronized for java bean method?

Posted By:   george_bhai  
Posted On:   Monday, June 13, 2005 02:37 AM

Instead of putting synchronised keyword for every method u can instantiate the java bean locally inside a method.that will act as a local varibale and which is thread safe..


class Example{

public void methodThreadSafe(){

MyBean bean = new MyBean();//this access is thread safe.
//Do something on bean instance.
}
}


If u will declare bean instance as instance variable then u can loose thread saftey.

Re: do I need synchronized for java bean method?

Posted By:   Christopher_Koenigsberg  
Posted On:   Thursday, June 9, 2005 09:43 AM

usually each thread of the servlet creates its own beans, so they are not running multi-threaded (the beans).


Make sure your servlet does not keep any beans as member fields.
About | Sitemap | Contact