Home >Java >javaTutorial >Give an example to explain the usage of synchronized keyword in Java
The synchronized keyword, as its name suggests, is used for synchronization and mutual exclusion.
Here is a brief description of its usage and meaning:
1. When synchronized modifies this or a non-static method or an instance, the synchronized lock is added to this or the instance object reference. For example, a and b are both instantiated objects of the Main class. If a calls a synchronized method, and b calls a synchronized method, there is no mutual exclusion. However, the synchronized methods called by objects a from different threads are mutually exclusive.
public synchronized void method(){ //…. } public void method() { synchronized (this){ //….. } }
2. Different from 1, when synchronized modifies the class name.class or static method, even if they are different objects, mutual exclusion will occur
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; /** * 附带token的控制器 * @version 1.0 Copyright 2013年7月29日 下午3:38:30 */ public abstract class TokenBaseController extends BaseController { private static Map<String, String> springmvc_token = new HashMap<String, String>(); /** 生成一个唯一值的token */ 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; } /** 验证表单token值和session中的token值是否一致 */ 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; } /** 获取表单中token值 */ 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]; } }
For more examples to explain the usage of the synchronized keyword in Java, please pay attention to the PHP Chinese website for related articles!