spring - Is this method thread safe according to Java -
if 2 threads accessing method on server, thread safe? threads coming gwt timer.
public userdto getuserfromsession() { userdto user = null; httpservletrequest httpservletrequest = this.getthreadlocalrequest(); httpsession session = httpservletrequest.getsession(); object userobj = session.getattribute("user"); if (userobj != null && userobj instanceof userdto) { user = (userdto) userobj; } return user; }
a method thread safe if doesn't access external (to method) shared variables.
the problem in code on line of code:
httpservletrequest httpservletrequest = this.getthreadlocalrequest();
because this.getthreadlocalrequest()
seems access shared variable. sure post whole class, can see not thread safe.
also after comment explain getthreadlocalrequest
method returns httpservletrequest
safely code remains not thread safe.
infact httpsession
not thread safe according article: session can change during code execution. example can return user after invalidation of session. imagine steps:
thread 1 thread 2 ---------------------------------------------- -------------- object userobj = session.getattribute("user"); session.invalidate(); if (userobj != null && userobj instanceof userdto) { user = (userdto) userobj; } return user;
at end return user if session invalidated thread.
Comments
Post a Comment