mercredi 13 janvier 2016

Best Design Pattern for handling general methods which will be accessed by multiple threads at a time

I am new to Java.

I tried to create static method which is accessed by Multiple methods of different classes at a time.

As it is static its gets locked and I am getting different results and performance problem.

Is there any best approach to make these methods accessible to multiple methods at a time.

Is there any best way to handle this in Spring Framework

  public class TestUtil {

  public static SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd");

   public static java.sql.Date getLastDayOfMonth(String month,intyear) 
    throws ParseException{      
    switch (month) {
    case "jan": 
             return new java.sql.Date(f.parse(year+"-1-31").getTime());
    case "feb": if(isLeapYear(year))
             return new java.sql.Date(f.parse(year+"-2-29").getTime());
             else
              return new java.sql.Date(f.parse(year+"-2-28").getTime());
    case "mar":                
             return new java.sql.Date(f.parse(year+"-3-31").getTime());

    case "apr": 
             return new java.sql.Date(f.parse(year+"-4-30").getTime());
    case "may": 
             return new java.sql.Date(f.parse(year+"-5-31").getTime());
    case "jun": 
             return new java.sql.Date(f.parse(year+"-6-30").getTime());
    case "jul": 
             return new java.sql.Date(f.parse(year+"-7-31").getTime());
    case "aug": 
             return new java.sql.Date(f.parse(year+"-8-31").getTime());
    case "sep": 
             return new java.sql.Date(f.parse(year+"-9-30").getTime());
    case "oct": 
             return new java.sql.Date(f.parse(year+"-10-31").getTime());
    case "nov": 
             return new java.sql.Date(f.parse(year+"-11-30").getTime());
    case "dec": 
             return new java.sql.Date(f.parse(year+"-12-31").getTime());        
    default: month = "Invalid month";
    return null;  
}           
}
}

Aucun commentaire:

Enregistrer un commentaire