As the name suggests, the synchronized keyword is used to synchronize mutual exclusion.
Here I will briefly record its usage and significance:
1. When synchronized modify this or a non-static method or an instance, the synchronized lock is added to this or instance object reference. For example, a and b are both instantiated objects of the Main class. A calls the synchronized method, and b calls the synchronized method, and does not form a mutually exclusive. However, the synchronized method calls of a object from different threads is mutually exclusive.
public synchronized void method(){ //…. } public void method() { synchronized (this){ //….. } }2. Unlike 1, when synchronized modifies the class name.class or static method, even different objects will form mutually exclusives.
Class Main { public synchronized static void method1(){ //…. } public void method2(){ synchronized(Main.class) // } } 3.eg:
package com.asiainfolinkage.ems.web.controller.base; import java.math.BigInteger; import java.util.Date; import java.util.HashMap; import java.util.Map; import java.util.Random; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession; /** * Controller with token* @version 1.0 Copyright July 29, 2013 at 3:38:30 pm */ public abstract class TokenBaseController extends BaseController { private static Map<String, String> springmvc_token = new HashMap<String, String>(); /** Generate a token with a unique value */ public String generateGUID(HttpSession session) { String token = ""; Date date = new Date(); synchronized (springmvc_token) { try { Object obj = session.getAttribute(Constants.SPRING_MVC_TOKENNAME); if (obj != null) springmvc_token = (Map<String, String>) session.getAttribute(Constants.SPRING_MVC_TOKENNAME); token = new BigInteger(165, new Random()).toString(36).toUpperCase(); springmvc_token.put(Constants.DEFAULT_TOKEN_NAME + "." + token, token); session.setAttribute(Constants.SPRING_MVC_TOKENNAME, springmvc_token); Constants.TOKEN_VALUE = token; } catch (IllegalStateException e) { _log.error("generateGUID() mothod find bug, by token session..."); } } return token; } /** Verify whether the form token value and the token value in the session are consistent*/ public boolean validToken(HttpServletRequest request) { String inputToken = getInputToken(request); if (inputToken == null) { _log.warn("token is not valid!inputToken is NULL"); return false; } HttpSession session = request.getSession(); Map<String, String> tokenMap = (Map<String, String>) session.getAttribute(Constants.SPRING_MVC_TOKENNAME); if (tokenMap == null || tokenMap.size() < 1) { _log.warn("token is not valid!sessionToken is NULL"); return false; } String sessionToken = tokenMap.get(Constants.DEFAULT_TOKEN_NAME + "." + inputToken); if (!inputToken.equals(sessionToken)) { _log.warn("token is not valid!inputToken='" + inputToken + "',sessionToken = '" + sessionToken + "'"); return false; } tokenMap.remove(Constants.DEFAULT_TOKEN_NAME + "." + inputToken); session.setAttribute(Constants.SPRING_MVC_TOKENNAME, tokenMap); return true; } /** Get the token value in the form*/ private String getInputToken(HttpServletRequest request) { Map params = request.getParameterMap(); if (!params.containsKey(Constants.DEFAULT_TOKEN_NAME)) { _log.warn("Could not find token name in params."); return null; } String[] tokens = (String[]) (String[]) params.get(Constants.DEFAULT_TOKEN_NAME); if ((tokens == null) || (tokens.length < 1)) { _log.warn("Got a null or empty token name."); return null; } return tokens[0]; } }