Illustrate the use of synchronized keywords in Java _java

Source: Internet
Author: User
Tags mutex

The Synchronized keyword, as the name suggests, is used to synchronize mutually exclusive functions.

Here is a concise note of how it is used and what it means:

1. When synchronized modifies this or Non-static method or is an instance, the synchronized lock is added to this or the instance object reference. For example, A,b is an instantiated object with the main class, a invokes the Synchronized method, and B invokes the synchronized method without forming a mutex. But the method of a object invocation of different threads being synchronized 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 a different object can form a mutex.

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 Controller * @version 1.0 Copyright July 29, 2013 PM 3:38:30 * * Public abstract class Tokenbasecontroller extend 
 
  s Basecontroller {private static map<string, string> Springmvc_token = new hashmap<string, string> (); 
    /** generates a unique value of 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_m 
        Vc_tokenname); 
    token = new BigInteger (165, New Random ()). ToString (). 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 session ..."); 
  } return token; /** Verify that the token value of the form and the token value in the session are consistent */public boolean validtoken (HttpServletRequest request) {String Inputt 
 
    Oken = Getinputtoken (request); 
      if (Inputtoken = = null) {_log.warn ("token is isn't 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 isn't 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.getparam 
    Etermap (); 
      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];  } 
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.