Results 1 to 1 of 1
- 11-10-2007, 04:47 PM #1
Member
- Join Date
- Nov 2007
- Posts
- 2
- Rep Power
- 0
is synchronization on method passing local variables as parameters needed
i have the following code
i was wondering do i need to add synchronization to checkCookie() when i call it to keep "cookies" and "user" thread safe when passing them the method or are they ok because they are local variables not instance variables.Java Code:public void doGet (HttpServletRequest req, HttpServletResponse res) throws ServletException,IOException { res.setContentType ("text/html"); PrintWriter out = res.getWriter(); //check to see if the session cookie has been created and that user has been fully authenticated //if the cookie doesnt exist they have just logged in and must be forwarded to cookieAuthentication //or the user doesnt have cookies enabled //no need to synchronize because local variables being used Cookie[] cookies = req.getCookies(); String user = req.getRemoteUser(); boolean authenticated = checkCookie(out, cookies, user); if (authenticated == true) //user authenticated continue { //create html welcome page createWelcome(out, user); } else { res.sendRedirect("login"); //cookie not created, send to login servlet (cookieAuthentication.java) } } private boolean checkCookie(PrintWriter out, Cookie[] cookies, String user) { boolean val = false; if (cookies!=null) //if cookies retrieved check them { for (int i=0; i<cookies.length; i++) { if (cookies[i].getName().equals(user)) { //if cookie name is the same as logged in user //then cookie has been created, return true val = true; } } } return val; }
i.e in the doGet change
toJava Code:boolean authenticated = checkCookie(out, cookies, user);
Java Code:synchronized (this) { boolean authenticated = checkCookie(out, cookies, user); }
Similar Threads
-
Are Local variables thread safe ?
By samson in forum Threads and SynchronizationReplies: 6Last Post: 12-21-2010, 02:34 PM -
[SOLVED] Passing parameters in Eclipse
By DonCash in forum EclipseReplies: 2Last Post: 04-08-2008, 04:46 PM -
[SOLVED] Why import isnt needed whn ref is used without storing to local variable
By N_i_X in forum Advanced JavaReplies: 2Last Post: 03-31-2008, 05:11 AM -
JNI - passing and returning parameters by value
By java_to_c in forum New To JavaReplies: 0Last Post: 01-31-2008, 08:49 PM -
Local Variables for a static method - thread safe?
By mikeg1z in forum Advanced JavaReplies: 1Last Post: 11-16-2007, 01:06 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks