Resolves checkstyle errors for template-method thread-pool throttling tls tolerant-reader (#1073)
* Reduces checkstyle errors in template-method * Reduces checkstyle errors in thread-pool * Reduces checkstyle errors in throttling * Reduces checkstyle errors in tls * Reduces checkstyle errors in tolerant-reader
This commit is contained in:
		
				
					committed by
					
						 Ilkka Seppälä
						Ilkka Seppälä
					
				
			
			
				
	
			
			
			
						parent
						
							9c8ad4485b
						
					
				
				
					commit
					b92eb5229d
				
			| @@ -23,58 +23,52 @@ | ||||
|  | ||||
| package com.iluwatar.tls; | ||||
|  | ||||
| import org.slf4j.Logger; | ||||
| import org.slf4j.LoggerFactory; | ||||
|  | ||||
| import java.util.Calendar; | ||||
| import java.util.Date; | ||||
| import java.util.concurrent.ExecutorService; | ||||
| import java.util.concurrent.Executors; | ||||
| import java.util.concurrent.Future; | ||||
| import org.slf4j.Logger; | ||||
| import org.slf4j.LoggerFactory; | ||||
|  | ||||
| /** | ||||
|  * ThreadLocal pattern | ||||
|  * <p> | ||||
|  * This App shows how to create an isolated space per each thread. In this | ||||
|  * example the usage of SimpleDateFormat is made to be thread-safe. This is an | ||||
|  * example of the ThreadLocal pattern. | ||||
|  * <p> | ||||
|  * By applying the ThreadLocal pattern you can keep track of application | ||||
|  * instances or locale settings throughout the handling of a request. The | ||||
|  * ThreadLocal class works like a static variable, with the exception that it is | ||||
|  * only bound to the current thread! This allows us to use static variables in a | ||||
|  * thread-safe way. | ||||
|  * <p> | ||||
|  * In Java, thread-local variables are implemented by the ThreadLocal class | ||||
|  * object. ThreadLocal holds a variable of type T, which is accessible via get/set | ||||
|  * methods. | ||||
|  * <p> | ||||
|  * SimpleDateFormat is one of the basic Java classes and is not thread-safe. If | ||||
|  * you do not isolate the instance of SimpleDateFormat per each thread then | ||||
|  * problems arise.  | ||||
|  * <p> | ||||
|  * App converts the String date value 15/12/2015 to the Date format using the | ||||
|  * Java class SimpleDateFormat. It does this 20 times using 4 threads, each doing  | ||||
|  * it 5 times. With the usage of as ThreadLocal in DateFormatCallable everything  | ||||
|  * runs well. But if you comment out the ThreadLocal variant (marked with "//TLTL")  | ||||
|  * and comment in the non ThreadLocal variant (marked with "//NTLNTL") you can  | ||||
|  * see what will happen without the ThreadLocal. Most likely you will get incorrect  | ||||
|  * date values and / or exceptions. | ||||
|  * <p> | ||||
|  * This example clearly show what will happen when using non thread-safe classes | ||||
|  * in a thread. In real life this may happen one in of 1.000 or 10.000 conversions | ||||
|  * and those are really hard to find errors. | ||||
|  *  | ||||
|  * @author Thomas Bauer, 2017  | ||||
|  * | ||||
|  * <p>This App shows how to create an isolated space per each thread. In this example the usage of | ||||
|  * SimpleDateFormat is made to be thread-safe. This is an example of the ThreadLocal pattern. | ||||
|  * | ||||
|  * <p>By applying the ThreadLocal pattern you can keep track of application instances or locale | ||||
|  * settings throughout the handling of a request. The ThreadLocal class works like a static | ||||
|  * variable, with the exception that it is only bound to the current thread! This allows us to use | ||||
|  * static variables in a thread-safe way. | ||||
|  * | ||||
|  * <p>In Java, thread-local variables are implemented by the ThreadLocal class object. ThreadLocal | ||||
|  * holds a variable of type T, which is accessible via get/set methods. | ||||
|  * | ||||
|  * <p>SimpleDateFormat is one of the basic Java classes and is not thread-safe. If you do not | ||||
|  * isolate the instance of SimpleDateFormat per each thread then problems arise. | ||||
|  * | ||||
|  * <p>App converts the String date value 15/12/2015 to the Date format using the Java class | ||||
|  * SimpleDateFormat. It does this 20 times using 4 threads, each doing it 5 times. With the usage of | ||||
|  * as ThreadLocal in DateFormatCallable everything runs well. But if you comment out the ThreadLocal | ||||
|  * variant (marked with "//TLTL") and comment in the non ThreadLocal variant (marked with | ||||
|  * "//NTLNTL") you can see what will happen without the ThreadLocal. Most likely you will get | ||||
|  * incorrect date values and / or exceptions. | ||||
|  * | ||||
|  * <p>This example clearly show what will happen when using non thread-safe classes in a thread. In | ||||
|  * real life this may happen one in of 1.000 or 10.000 conversions and those are really hard to find | ||||
|  * errors. | ||||
|  * | ||||
|  * @author Thomas Bauer, 2017 | ||||
|  */ | ||||
| public class App { | ||||
|  | ||||
|   private static final Logger LOGGER = LoggerFactory.getLogger(App.class); | ||||
|  | ||||
|   /** | ||||
|    * Program entry point | ||||
|    *  | ||||
|    * @param args | ||||
|    *          command line args | ||||
|    * Program entry point. | ||||
|    * | ||||
|    * @param args command line args | ||||
|    */ | ||||
|   public static void main(String[] args) { | ||||
|     int counterDateValues = 0; | ||||
| @@ -115,9 +109,9 @@ public class App { | ||||
|   } | ||||
|  | ||||
|   /** | ||||
|    * Print result (date values) of a thread execution and count dates | ||||
|    *  | ||||
|    * @param res  contains results of a thread execution | ||||
|    * Print result (date values) of a thread execution and count dates. | ||||
|    * | ||||
|    * @param res contains results of a thread execution | ||||
|    */ | ||||
|   private static int printAndCountDates(Result res) { | ||||
|     // a correct run should deliver 5 times 15.12.2015 per each thread | ||||
| @@ -128,15 +122,16 @@ public class App { | ||||
|       cal.setTime(dt); | ||||
|       // Formatted output of the date value: DD.MM.YYYY | ||||
|       LOGGER.info( | ||||
|           cal.get(Calendar.DAY_OF_MONTH) + "." + cal.get(Calendar.MONTH) + "." + +cal.get(Calendar.YEAR)); | ||||
|           cal.get(Calendar.DAY_OF_MONTH) + "." + cal.get(Calendar.MONTH) + "." + +cal | ||||
|               .get(Calendar.YEAR)); | ||||
|     } | ||||
|     return counter; | ||||
|   } | ||||
|  | ||||
|   /** | ||||
|    * Print result (exceptions) of a thread execution and count exceptions | ||||
|    *  | ||||
|    * @param res  contains results of a thread execution | ||||
|    * Print result (exceptions) of a thread execution and count exceptions. | ||||
|    * | ||||
|    * @param res contains results of a thread execution | ||||
|    * @return number of dates | ||||
|    */ | ||||
|   private static int printAndCountExceptions(Result res) { | ||||
|   | ||||
| @@ -23,25 +23,23 @@ | ||||
|  | ||||
| package com.iluwatar.tls; | ||||
|  | ||||
| import org.slf4j.Logger; | ||||
| import org.slf4j.LoggerFactory; | ||||
|  | ||||
| import java.text.DateFormat; | ||||
| import java.text.SimpleDateFormat; | ||||
| import java.util.concurrent.Callable; | ||||
| import org.slf4j.Logger; | ||||
| import org.slf4j.LoggerFactory; | ||||
|  | ||||
| /** | ||||
|  * DateFormatCallable converts string dates to a date format using | ||||
|  * SimpleDateFormat. The date format and the date value will be passed to the | ||||
|  * Callable by the constructor. The constructor creates a instance of | ||||
|  * SimpleDateFormat and stores it in a ThreadLocal class variable. For the | ||||
|  * complete description of the example see {@link App} | ||||
|  *  | ||||
|  * You can comment out the code marked with //TLTL and comment in the  | ||||
|  * code marked //NTLNTL. Then you can see what will happen if you do not | ||||
|  * use the ThreadLocal. For details see the description of {@link App} | ||||
|  * DateFormatCallable converts string dates to a date format using SimpleDateFormat. The date format | ||||
|  * and the date value will be passed to the Callable by the constructor. The constructor creates a | ||||
|  * instance of SimpleDateFormat and stores it in a ThreadLocal class variable. For the complete | ||||
|  * description of the example see {@link App}. | ||||
|  * | ||||
|  * @author Thomas Bauer, 2017  | ||||
|  * <p>You can comment out the code marked with //TLTL and comment in the code marked //NTLNTL. Then | ||||
|  * you can see what will happen if you do not use the ThreadLocal. For details see the description | ||||
|  * of {@link App} | ||||
|  * | ||||
|  * @author Thomas Bauer, 2017 | ||||
|  */ | ||||
| public class DateFormatCallable implements Callable<Result> { | ||||
|  | ||||
| @@ -51,15 +49,13 @@ public class DateFormatCallable implements Callable<Result> { | ||||
|   // private DateFormat df;                 //NTLNTL | ||||
|  | ||||
|   private String dateValue; // for dateValue Thread Local not needed | ||||
|    | ||||
|  | ||||
|  | ||||
|   /** | ||||
|    * The date format and the date value are passed to the constructor | ||||
|    *  | ||||
|    * @param inDateFormat | ||||
|    *          string date format string, e.g. "dd/MM/yyyy" | ||||
|    * @param inDateValue | ||||
|    *          string date value, e.g. "21/06/2016" | ||||
|    * The date format and the date value are passed to the constructor. | ||||
|    * | ||||
|    * @param inDateFormat string date format string, e.g. "dd/MM/yyyy" | ||||
|    * @param inDateValue  string date value, e.g. "21/06/2016" | ||||
|    */ | ||||
|   public DateFormatCallable(String inDateFormat, String inDateValue) { | ||||
|     final String idf = inDateFormat;                 //TLTL | ||||
| @@ -73,9 +69,6 @@ public class DateFormatCallable implements Callable<Result> { | ||||
|     this.dateValue = inDateValue; | ||||
|   } | ||||
|  | ||||
|   /** | ||||
|    * @see java.util.concurrent.Callable#call() | ||||
|    */ | ||||
|   @Override | ||||
|   public Result call() { | ||||
|     LOGGER.info(Thread.currentThread() + " started executing..."); | ||||
| @@ -88,7 +81,7 @@ public class DateFormatCallable implements Callable<Result> { | ||||
|         // instance of SimpleDateFormat locally | ||||
|         // Create the date value and store it in dateList | ||||
|         result.getDateList().add(this.df.get().parse(this.dateValue));   //TLTL | ||||
| //      result.getDateList().add(this.df.parse(this.dateValue));           //NTLNTL | ||||
|         // result.getDateList().add(this.df.parse(this.dateValue));           //NTLNTL | ||||
|       } catch (Exception e) { | ||||
|         // write the Exception to a list and continue work | ||||
|         result.getExceptionList().add(e.getClass() + ": " + e.getMessage()); | ||||
|   | ||||
| @@ -32,10 +32,10 @@ import java.util.Date; | ||||
| import java.util.List; | ||||
|  | ||||
| /** | ||||
|  * Result object that will be returned by the Callable {@link DateFormatCallable}  | ||||
|  * used in {@link App} | ||||
|  * Result object that will be returned by the Callable {@link DateFormatCallable} used in {@link | ||||
|  * App}. | ||||
|  * | ||||
|  * @author Thomas Bauer, 2017  | ||||
|  * @author Thomas Bauer, 2017 | ||||
|  */ | ||||
| public class Result { | ||||
|   // A list to collect the date values created in one thread | ||||
| @@ -44,9 +44,10 @@ public class Result { | ||||
|   // A list to collect Exceptions thrown in one threads (should be none in | ||||
|   // this example) | ||||
|   private List<String> exceptionList = new ArrayList<String>(); | ||||
|    | ||||
|  | ||||
|   /** | ||||
|    *  | ||||
|    * Get list of date values collected within a thread execution. | ||||
|    * | ||||
|    * @return List of date values collected within an thread execution | ||||
|    */ | ||||
|   public List<Date> getDateList() { | ||||
| @@ -54,7 +55,8 @@ public class Result { | ||||
|   } | ||||
|  | ||||
|   /** | ||||
|    *  | ||||
|    * Get list of exceptions thrown within a thread execution. | ||||
|    * | ||||
|    * @return List of exceptions thrown within an thread execution | ||||
|    */ | ||||
|   public List<String> getExceptionList() { | ||||
|   | ||||
		Reference in New Issue
	
	Block a user