filmov
tv
85 What is ThreadSafe in java? | Adv Java Servlet programming Tutorial

Показать описание
#ThreadSafety #In #Java? #Advance #Java #Servlet #Programming #Tutorial
- If multiple threads acting on one object/one variable simultaneously/concurrently then that variable/object is not "ThreadSafe". To achieve ThreadSafety we need to work with 'Synchronization' concept.
- Our servlet program is one instance multiple threads component so, instance variables of our servlet-class are not thread-safe by default, because multiple threads may act on single copy of instance-variable simultaneously.
eg:
public class TestSrv extends HttpServlet{
int a; //'a' instance variable , its not thread safe obj/variable
public void service(-,-)/doXxx(-,-)throws SE,IOE{
int b; //'b' local variable //thread safe obj/variable
-------
-------
}
}
Local variables are placed in service(-,-) or doXxx(-,-) methods are ThreadSafe variables by default because every Thread gets its own copy of local variable.
- If multiple threads acting on one object/one variable simultaneously/concurrently then that variable/object is not "ThreadSafe". To achieve ThreadSafety we need to work with 'Synchronization' concept.
- Our servlet program is one instance multiple threads component so, instance variables of our servlet-class are not thread-safe by default, because multiple threads may act on single copy of instance-variable simultaneously.
eg:
public class TestSrv extends HttpServlet{
int a; //'a' instance variable , its not thread safe obj/variable
public void service(-,-)/doXxx(-,-)throws SE,IOE{
int b; //'b' local variable //thread safe obj/variable
-------
-------
}
}
Local variables are placed in service(-,-) or doXxx(-,-) methods are ThreadSafe variables by default because every Thread gets its own copy of local variable.