M
Med
Hi,
Could someone please tell me if I undrestand the concept of "Thread Saftey"
correctly in Multi-threaded web application (asp.net 2) using following
examples:
1. Class A is NOT a thread safe class:
public sealed class A
{
private static int _iCustomerId;
//Property Example
public static int CustomerId
{
set { _iCustomerId = value; }
get { return _iCustomerId; }
}
//Public method example
public static DataSet GetCustomerDetails(int iCustomerId)
{
_iCustomerId = iCustomerId;
DataSet ds = Select * from customer where CustomerId = iCustomerId
from database....
return ds;
}
}
2. Class B IS a thread safe class:
public sealed class B
{
//We do not use class-level private/protected variables here.
//Also not using any properties with class-level private/protected
variables.
//Public method example
public static DataSet GetCustomerDetails(int iCustomerId)
{
DataSet ds = Select * from customer where CustomerId = iCustomerId
from database....
return ds;
}
}
So, A class is "Thread Safe" if:
1. As long as no "class-level private/protected variables" are used.
2.Instantiate a new class instead of using static methods/properties.
Am I understanding it correctly?
Thanks for your time
Kind Regards
Med
Could someone please tell me if I undrestand the concept of "Thread Saftey"
correctly in Multi-threaded web application (asp.net 2) using following
examples:
1. Class A is NOT a thread safe class:
public sealed class A
{
private static int _iCustomerId;
//Property Example
public static int CustomerId
{
set { _iCustomerId = value; }
get { return _iCustomerId; }
}
//Public method example
public static DataSet GetCustomerDetails(int iCustomerId)
{
_iCustomerId = iCustomerId;
DataSet ds = Select * from customer where CustomerId = iCustomerId
from database....
return ds;
}
}
2. Class B IS a thread safe class:
public sealed class B
{
//We do not use class-level private/protected variables here.
//Also not using any properties with class-level private/protected
variables.
//Public method example
public static DataSet GetCustomerDetails(int iCustomerId)
{
DataSet ds = Select * from customer where CustomerId = iCustomerId
from database....
return ds;
}
}
So, A class is "Thread Safe" if:
1. As long as no "class-level private/protected variables" are used.
2.Instantiate a new class instead of using static methods/properties.
Am I understanding it correctly?
Thanks for your time
Kind Regards
Med