(I am using Java!) I have two threads t1 and t2. Each thread reads from a Socket inputstream by invoking a public static function as follows:
public static byte[] readSocket(DataInputStream dis, Logger log) throws IOException
{
byte[] byteArray = new byte[100];
if(dis.read(byteArray, 0, 100)!=-1)
{
return byteArray;
}
}
Will there be a synchronization problem if
- the inputstream is from different sockets (i.e. t1 reads from a different socket and t2 from another).
- the inputstream is same for both threads t1 and t2.
It will not cause a data race. If you are calling the same method on different threads, each thread has a different copy of the method on its own stack. Therefore, local variables inside the method exist on each thread’s stack with different copies.
So, your only problem can appear if the
DataInputStreamwhich is an external parameter is shared or not. If not, then you have no problems. The fact that the method is static does not influence in any way what I’ve said above.