Merge pull request #529 from thomasoss/master
Thread Local Storage issue #77
This commit is contained in:
commit
854101bb49
3
pom.xml
3
pom.xml
@ -50,6 +50,7 @@
|
||||
</properties>
|
||||
<modules>
|
||||
<module>abstract-factory</module>
|
||||
<module>tls</module>
|
||||
<module>builder</module>
|
||||
<module>factory-method</module>
|
||||
<module>prototype</module>
|
||||
@ -469,4 +470,4 @@
|
||||
</plugins>
|
||||
</reporting>
|
||||
|
||||
</project>
|
||||
</project>
|
||||
|
22
tls/README.md
Normal file
22
tls/README.md
Normal file
@ -0,0 +1,22 @@
|
||||
---
|
||||
layout: pattern
|
||||
title: Thread Local Storage
|
||||
folder: tls
|
||||
permalink: /patterns/tls/
|
||||
pumlid:
|
||||
categories: Concurrency
|
||||
tags:
|
||||
- Java
|
||||
- Difficulty-Intermediate
|
||||
---
|
||||
|
||||
## Intent
|
||||
Securing variables global to a thread against being spoiled by other threads. That is needed if you use class variables or static variables in your Callable object or Runnable object that are not read-only.
|
||||
|
||||

|
||||
|
||||
## Applicability
|
||||
Use the Thread Local Storage in any of the following situations
|
||||
|
||||
* when you use class variables in your Callable / Runnalbe object that are not read-only and you use the same Callable instance in more than one thread running in parallel
|
||||
* when you use static variables in your Callable / Runnable object that are not read-only and more than one instances of the Callable / Runnalbe may run in parallel threads.
|
BIN
tls/etc/tls.png
Normal file
BIN
tls/etc/tls.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 34 KiB |
79
tls/etc/tls.ucls
Normal file
79
tls/etc/tls.ucls
Normal file
@ -0,0 +1,79 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<class-diagram version="1.1.13" icons="true" always-add-relationships="false" generalizations="true" realizations="true"
|
||||
associations="true" dependencies="false" nesting-relationships="true" router="FAN">
|
||||
<class id="1" language="java" name="com.iluwatar.tls.DateFormatCallable" project="PatternIluwatar"
|
||||
file="/PatternIluwatar/src/main/java/com/iluwatar/tls/DateFormatCallable.java" binary="false" corner="BOTTOM_RIGHT">
|
||||
<position height="-1" width="-1" x="503" y="227"/>
|
||||
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
|
||||
sort-features="false" accessors="true" visibility="true">
|
||||
<attributes public="true" package="true" protected="true" private="true" static="true"/>
|
||||
<operations public="true" package="true" protected="true" private="true" static="true"/>
|
||||
</display>
|
||||
</class>
|
||||
<interface id="2" language="java" name="java.util.concurrent.Callable" project="Intros"
|
||||
file="C:/interne/Programme/jdk8u60x64/jre/lib/rt.jar" binary="true" corner="BOTTOM_RIGHT">
|
||||
<position height="-1" width="-1" x="501" y="52"/>
|
||||
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
|
||||
sort-features="false" accessors="true" visibility="true">
|
||||
<attributes public="true" package="true" protected="true" private="true" static="true"/>
|
||||
<operations public="true" package="true" protected="true" private="true" static="true"/>
|
||||
</display>
|
||||
</interface>
|
||||
<class id="3" language="java" name="java.lang.ThreadLocal" project="Intros"
|
||||
file="C:/interne/Programme/jdk8u60x64/jre/lib/rt.jar" binary="true" corner="BOTTOM_RIGHT">
|
||||
<position height="-1" width="-1" x="111" y="284"/>
|
||||
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
|
||||
sort-features="false" accessors="true" visibility="true">
|
||||
<attributes public="true" package="true" protected="true" private="true" static="true"/>
|
||||
<operations public="true" package="true" protected="true" private="true" static="true"/>
|
||||
</display>
|
||||
</class>
|
||||
<class id="4" language="java" name="com.iluwatar.tls.App" project="PatternIluwatar"
|
||||
file="/PatternIluwatar/src/main/java/com/iluwatar/tls/App.java" binary="false" corner="BOTTOM_RIGHT">
|
||||
<position height="-1" width="-1" x="845" y="228"/>
|
||||
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
|
||||
sort-features="false" accessors="true" visibility="true">
|
||||
<attributes public="true" package="true" protected="true" private="true" static="true"/>
|
||||
<operations public="true" package="true" protected="true" private="true" static="true"/>
|
||||
</display>
|
||||
</class>
|
||||
<class id="5" language="java" name="com.iluwatar.tls.Result" project="PatternIluwatar"
|
||||
file="/PatternIluwatar/src/main/java/com/iluwatar/tls/Result.java" binary="false" corner="BOTTOM_RIGHT">
|
||||
<position height="-1" width="-1" x="673" y="459"/>
|
||||
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
|
||||
sort-features="false" accessors="true" visibility="true">
|
||||
<attributes public="true" package="true" protected="true" private="true" static="true"/>
|
||||
<operations public="true" package="true" protected="true" private="true" static="true"/>
|
||||
</display>
|
||||
</class>
|
||||
<dependency id="6">
|
||||
<end type="SOURCE" refId="1"/>
|
||||
<end type="TARGET" refId="5"/>
|
||||
</dependency>
|
||||
<association id="7">
|
||||
<end type="SOURCE" refId="1" navigable="false">
|
||||
<attribute id="8" name="df"/>
|
||||
<multiplicity id="9" minimum="0" maximum="1"/>
|
||||
</end>
|
||||
<end type="TARGET" refId="3" navigable="true"/>
|
||||
<display labels="true" multiplicity="true"/>
|
||||
</association>
|
||||
<realization id="10">
|
||||
<end type="SOURCE" refId="1"/>
|
||||
<end type="TARGET" refId="2"/>
|
||||
</realization>
|
||||
<dependency id="11">
|
||||
<end type="SOURCE" refId="4"/>
|
||||
<end type="TARGET" refId="5"/>
|
||||
</dependency>
|
||||
<dependency id="12">
|
||||
<end type="SOURCE" refId="4"/>
|
||||
<end type="TARGET" refId="1"/>
|
||||
</dependency>
|
||||
<classifier-display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
|
||||
sort-features="false" accessors="true" visibility="true">
|
||||
<attributes public="true" package="true" protected="true" private="true" static="true"/>
|
||||
<operations public="true" package="true" protected="true" private="true" static="true"/>
|
||||
</classifier-display>
|
||||
<association-display labels="true" multiplicity="true"/>
|
||||
</class-diagram>
|
23
tls/etc/tls.urm.puml
Normal file
23
tls/etc/tls.urm.puml
Normal file
@ -0,0 +1,23 @@
|
||||
@startuml
|
||||
package com.iluwatar.tls {
|
||||
class App {
|
||||
+ App()
|
||||
+ main(args : String[]) {static}
|
||||
- printAndCountDates(res : Result) : int {static}
|
||||
- printAndCountExceptions(res : Result) : int {static}
|
||||
}
|
||||
class DateFormatCallable {
|
||||
- dateValue : String
|
||||
- df : ThreadLocal<DateFormat>
|
||||
+ DateFormatCallable(inDateFormat : String, inDateValue : String)
|
||||
+ call() : Result
|
||||
}
|
||||
class Result {
|
||||
- dateList : List<Date>
|
||||
- exceptionList : List<String>
|
||||
+ Result()
|
||||
+ getDateList() : List<Date>
|
||||
+ getExceptionList() : List<String>
|
||||
}
|
||||
}
|
||||
@enduml
|
43
tls/pom.xml
Normal file
43
tls/pom.xml
Normal file
@ -0,0 +1,43 @@
|
||||
<?xml version="1.0"?>
|
||||
<!--
|
||||
|
||||
The MIT License
|
||||
Copyright (c) 2014-2016 Ilkka Seppälä
|
||||
2016 adapted for tls Thomas Bauer
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
|
||||
-->
|
||||
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>com.iluwatar</groupId>
|
||||
<artifactId>java-design-patterns</artifactId>
|
||||
<version>1.15.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<artifactId>tls</artifactId>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
146
tls/src/main/java/com/iluwatar/tls/App.java
Normal file
146
tls/src/main/java/com/iluwatar/tls/App.java
Normal file
@ -0,0 +1,146 @@
|
||||
/**
|
||||
* The MIT License
|
||||
* Copyright (c) 2016 Thomas Bauer
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
package com.iluwatar.tls;
|
||||
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.Future;
|
||||
|
||||
/**
|
||||
* 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
|
||||
*/
|
||||
public class App {
|
||||
/**
|
||||
* Program entry point
|
||||
*
|
||||
* @param args
|
||||
* command line args
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
int counterDateValues = 0;
|
||||
int counterExceptions = 0;
|
||||
|
||||
// Create a callable
|
||||
DateFormatCallable callableDf = new DateFormatCallable("dd/MM/yyyy", "15/12/2015");
|
||||
// start 4 threads, each using the same Callable instance
|
||||
ExecutorService executor = Executors.newCachedThreadPool();
|
||||
|
||||
Future<Result> futureResult1 = executor.submit(callableDf);
|
||||
Future<Result> futureResult2 = executor.submit(callableDf);
|
||||
Future<Result> futureResult3 = executor.submit(callableDf);
|
||||
Future<Result> futureResult4 = executor.submit(callableDf);
|
||||
try {
|
||||
Result[] result = new Result[4];
|
||||
result[0] = futureResult1.get();
|
||||
result[1] = futureResult2.get();
|
||||
result[2] = futureResult3.get();
|
||||
result[3] = futureResult4.get();
|
||||
|
||||
// Print results of thread executions (converted dates and raised exceptions)
|
||||
// and count them
|
||||
for (int i = 0; i < result.length; i++) {
|
||||
counterDateValues = counterDateValues + printAndCountDates(result[i]);
|
||||
counterExceptions = counterExceptions + printAndCountExceptions(result[i]);
|
||||
}
|
||||
|
||||
// a correct run should deliver 20 times 15.12.2015
|
||||
// and a correct run shouldn't deliver any exception
|
||||
System.out.println("The List dateList contains " + counterDateValues + " date values");
|
||||
System.out.println("The List exceptionList contains " + counterExceptions + " exceptions");
|
||||
|
||||
} catch (Exception e) {
|
||||
System.out.println("Abnormal end of program. Program throws exception: " + e);
|
||||
}
|
||||
executor.shutdown();
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
int counter = 0;
|
||||
for (Date dt : res.getDateList()) {
|
||||
counter++;
|
||||
Calendar cal = Calendar.getInstance();
|
||||
cal.setTime(dt);
|
||||
// Formatted output of the date value: DD.MM.YYYY
|
||||
System.out.println(
|
||||
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
|
||||
* @return number of dates
|
||||
*/
|
||||
private static int printAndCountExceptions(Result res) {
|
||||
// a correct run shouldn't deliver any exception
|
||||
int counter = 0;
|
||||
for (String ex : res.getExceptionList()) {
|
||||
counter++;
|
||||
System.out.println(ex);
|
||||
}
|
||||
return counter;
|
||||
}
|
||||
}
|
98
tls/src/main/java/com/iluwatar/tls/DateFormatCallable.java
Normal file
98
tls/src/main/java/com/iluwatar/tls/DateFormatCallable.java
Normal file
@ -0,0 +1,98 @@
|
||||
/**
|
||||
* The MIT License
|
||||
* Copyright (c) 2016 Thomas Bauer
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
package com.iluwatar.tls;
|
||||
|
||||
import java.text.DateFormat;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.concurrent.Callable;
|
||||
|
||||
/**
|
||||
* 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}
|
||||
*
|
||||
* @author Thomas Bauer, 2017
|
||||
*/
|
||||
public class DateFormatCallable implements Callable<Result> {
|
||||
// class variables (members)
|
||||
private ThreadLocal<DateFormat> df; //TLTL
|
||||
// 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"
|
||||
*/
|
||||
public DateFormatCallable(String inDateFormat, String inDateValue) {
|
||||
final String idf = inDateFormat; //TLTL
|
||||
this.df = new ThreadLocal<DateFormat>() { //TLTL
|
||||
@Override //TLTL
|
||||
protected DateFormat initialValue() { //TLTL
|
||||
return new SimpleDateFormat(idf); //TLTL
|
||||
} //TLTL
|
||||
}; //TLTL
|
||||
// this.df = new SimpleDateFormat(inDateFormat); //NTLNTL
|
||||
this.dateValue = inDateValue;
|
||||
}
|
||||
|
||||
/**
|
||||
* @see java.util.concurrent.Callable#call()
|
||||
*/
|
||||
@Override
|
||||
public Result call() {
|
||||
System.out.println(Thread.currentThread() + " started executing...");
|
||||
Result result = new Result();
|
||||
|
||||
// Convert date value to date 5 times
|
||||
for (int i = 1; i <= 5; i++) {
|
||||
try {
|
||||
// this is the statement where it is important to have the
|
||||
// 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
|
||||
} catch (Exception e) {
|
||||
// write the Exception to a list and continue work
|
||||
result.getExceptionList().add(e.getClass() + ": " + e.getMessage());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
System.out.println(Thread.currentThread() + " finished processing part of the thread");
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
40
tls/src/main/java/com/iluwatar/tls/Result.java
Normal file
40
tls/src/main/java/com/iluwatar/tls/Result.java
Normal file
@ -0,0 +1,40 @@
|
||||
/*
|
||||
* Fiducia IT AG, All rights reserved. Use is subject to license terms.
|
||||
*/
|
||||
|
||||
package com.iluwatar.tls;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Result object that will be returned by the Callable {@link DateFormatCallable}
|
||||
* used in {@link App}
|
||||
*
|
||||
* @author Thomas Bauer, 2017
|
||||
*/
|
||||
public class Result {
|
||||
// A list to collect the date values created in one thread
|
||||
private List<Date> dateList = new ArrayList<Date>();
|
||||
|
||||
// A list to collect Exceptions thrown in one threads (should be none in
|
||||
// this example)
|
||||
private List<String> exceptionList = new ArrayList<String>();
|
||||
|
||||
/**
|
||||
*
|
||||
* @return List of date values collected within an thread execution
|
||||
*/
|
||||
public List<Date> getDateList() {
|
||||
return dateList;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @return List of exceptions thrown within an thread execution
|
||||
*/
|
||||
public List<String> getExceptionList() {
|
||||
return exceptionList;
|
||||
}
|
||||
}
|
40
tls/src/test/java/com/iluwatar/tls/AppTest.java
Normal file
40
tls/src/test/java/com/iluwatar/tls/AppTest.java
Normal file
@ -0,0 +1,40 @@
|
||||
/**
|
||||
* The MIT License
|
||||
* Copyright (c) 2016 Thomas Bauer
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
package com.iluwatar.tls;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* Tests that thread local storage example runs without errors.
|
||||
*
|
||||
* @author Thomas Bauer, January 2017
|
||||
*
|
||||
*/
|
||||
public class AppTest {
|
||||
@Test
|
||||
public void test() throws Exception {
|
||||
String[] args = {};
|
||||
App.main(args);
|
||||
}
|
||||
}
|
144
tls/src/test/java/com/iluwatar/tls/DateFormatCallableTest.java
Normal file
144
tls/src/test/java/com/iluwatar/tls/DateFormatCallableTest.java
Normal file
@ -0,0 +1,144 @@
|
||||
/**
|
||||
* The MIT License
|
||||
* Copyright (c) 2016 Thomas Bauer
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
package com.iluwatar.tls;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.Future;
|
||||
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
/**
|
||||
*
|
||||
* Test of the Callable
|
||||
*
|
||||
* In this test {@link DateFormatCallable} is tested with only one thread (i.e. without concurrency situation)
|
||||
* <p>
|
||||
* After a successful run 5 date values should be in the result object. All dates should have
|
||||
* the same value (15.11.2015). To avoid problems with time zone not the date instances themselves
|
||||
* are compared by the test. For the test the dates are converted into string format DD.MM.YYY
|
||||
* <p>
|
||||
* Additionally the number of list entries are tested for both the list with the date values
|
||||
* and the list with the exceptions
|
||||
*
|
||||
* @author Thomas Bauer, January 2017
|
||||
*
|
||||
*/
|
||||
public class DateFormatCallableTest {
|
||||
|
||||
// Class variables used in setup() have to be static because setup() has to be static
|
||||
/**
|
||||
* Result object given back by DateFormatCallable
|
||||
* -- Array with converted date values
|
||||
* -- Array with thrown exceptions
|
||||
*/
|
||||
static Result result;
|
||||
|
||||
/**
|
||||
* The date values created by the run of of DateFormatRunnalbe. List will be filled in the setup() method
|
||||
*/
|
||||
static List<String> createdDateValues = new ArrayList<String>();
|
||||
|
||||
/**
|
||||
* Expected number of date values in the date value list created by the run of DateFormatRunnalbe
|
||||
*/
|
||||
int expectedCounterDateValues = 5;
|
||||
|
||||
/**
|
||||
* Expected number of exceptions in the exception list created by the run of DateFormatRunnalbe.
|
||||
*/
|
||||
int expectedCounterExceptions = 0;
|
||||
|
||||
/**
|
||||
* Expected content of the list containing the date values created by the run of DateFormatRunnalbe
|
||||
*/
|
||||
List<String> expectedDateValues = Arrays.asList("15.11.2015", "15.11.2015", "15.11.2015", "15.11.2015", "15.11.2015");
|
||||
|
||||
/**
|
||||
* Run Callable and prepare results for usage in the test methods
|
||||
*/
|
||||
@BeforeClass
|
||||
public static void setup() {
|
||||
// Create a callable
|
||||
DateFormatCallable callableDf = new DateFormatCallable("dd/MM/yyyy", "15/12/2015");
|
||||
// start thread using the Callable instance
|
||||
ExecutorService executor = Executors.newCachedThreadPool();
|
||||
Future<Result> futureResult = executor.submit(callableDf);
|
||||
try {
|
||||
result = futureResult.get();
|
||||
createdDateValues = convertDatesToString(result);
|
||||
} catch (Exception e) {
|
||||
fail("Setup failed: " + e);
|
||||
}
|
||||
executor.shutdown();
|
||||
}
|
||||
|
||||
private static List<String> convertDatesToString(Result res) {
|
||||
// Format date value as DD.MM.YYYY
|
||||
if (res == null || res.getDateList() == null || res.getDateList().size() == 0) {
|
||||
return null;
|
||||
}
|
||||
List<String> returnList = new ArrayList<String>();
|
||||
|
||||
for (Date dt : res.getDateList()) {
|
||||
Calendar cal = Calendar.getInstance();
|
||||
cal.setTime(dt);
|
||||
returnList.add(cal.get(Calendar.DAY_OF_MONTH) + "." + cal.get(Calendar.MONTH) + "." + cal.get(Calendar.YEAR));
|
||||
}
|
||||
return returnList;
|
||||
}
|
||||
|
||||
/**
|
||||
* Test date values after the run of DateFormatRunnalbe. A correct run should deliver 5 times 15.12.2015
|
||||
*/
|
||||
@Test
|
||||
public void testDateValues() {
|
||||
assertEquals(expectedDateValues, createdDateValues);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test number of dates in the list after the run of DateFormatRunnalbe. A correct run should deliver 5 date values
|
||||
*/
|
||||
@Test
|
||||
public void testCounterDateValues() {
|
||||
assertEquals(expectedCounterDateValues, result.getDateList().size());
|
||||
}
|
||||
|
||||
/**
|
||||
* Test number of Exceptions in the list after the run of DateFormatRunnalbe. A correct run should deliver
|
||||
* no exceptions
|
||||
*/
|
||||
@Test
|
||||
public void testCounterExceptions() {
|
||||
assertEquals(expectedCounterExceptions, result.getExceptionList().size());
|
||||
}
|
||||
}
|
@ -0,0 +1,127 @@
|
||||
/**
|
||||
* The MIT License
|
||||
* Copyright (c) 2016 Thomas Bauer
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
package com.iluwatar.tls;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.Future;
|
||||
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
/**
|
||||
*
|
||||
* Test of the Callable
|
||||
*
|
||||
* In this test {@link DateFormatCallable} is tested with only one thread (i.e. without concurrency situation)
|
||||
* <p>
|
||||
* An incorrect formatted date is passed to the Callable
|
||||
* After a successful run 0 date values and 5 exceptions should be in the result object.
|
||||
*
|
||||
* @author Thomas Bauer, January 2017
|
||||
*
|
||||
*/
|
||||
public class DateFormatCallableTestIncorrectDateFormat {
|
||||
|
||||
// Class variables used in setup() have to be static because setup() has to be static
|
||||
/**
|
||||
* Result object given back by DateFormatCallable
|
||||
* -- Array with converted date values
|
||||
* -- Array with thrown exceptions
|
||||
*/
|
||||
static Result result;
|
||||
|
||||
/**
|
||||
* The date values created by the run of DateFormatRunnalbe. List will be filled in the setup() method
|
||||
*/
|
||||
static List<String> createdExceptions = new ArrayList<String>();
|
||||
|
||||
/**
|
||||
* Expected number of date values in the date value list created by the run of DateFormatRunnalbe
|
||||
*/
|
||||
int expectedCounterDateValues = 0;
|
||||
|
||||
/**
|
||||
* Expected number of exceptions in the exception list created by the run of DateFormatRunnalbe.
|
||||
*/
|
||||
int expectedCounterExceptions = 5;
|
||||
|
||||
/**
|
||||
* Expected content of the list containing the exceptions created by the run of DateFormatRunnalbe
|
||||
*/
|
||||
List<String> expectedExceptions = Arrays.asList("class java.text.ParseException: Unparseable date: \"15.12.2015\"",
|
||||
"class java.text.ParseException: Unparseable date: \"15.12.2015\"",
|
||||
"class java.text.ParseException: Unparseable date: \"15.12.2015\"",
|
||||
"class java.text.ParseException: Unparseable date: \"15.12.2015\"",
|
||||
"class java.text.ParseException: Unparseable date: \"15.12.2015\"");
|
||||
|
||||
/**
|
||||
* Run Callable and prepare results for usage in the test methods
|
||||
*/
|
||||
@BeforeClass
|
||||
public static void setup() {
|
||||
// Create a callable. Pass a string date value not matching the format string
|
||||
DateFormatCallable callableDf = new DateFormatCallable("dd/MM/yyyy", "15.12.2015");
|
||||
// start thread using the Callable instance
|
||||
ExecutorService executor = Executors.newCachedThreadPool();
|
||||
Future<Result> futureResult = executor.submit(callableDf);
|
||||
try {
|
||||
result = futureResult.get();
|
||||
} catch (Exception e) {
|
||||
fail("Setup failed: " + e);
|
||||
}
|
||||
executor.shutdown();
|
||||
}
|
||||
|
||||
/**
|
||||
* Test Exceptions after the run of DateFormatRunnalbe. A correct run should deliver 5 times the
|
||||
* same exception
|
||||
*/
|
||||
@Test
|
||||
public void testExecptions() {
|
||||
assertEquals(expectedExceptions, result.getExceptionList());
|
||||
}
|
||||
|
||||
/**
|
||||
* Test number of dates in the list after the run of DateFormatRunnalbe. A correct run should deliver no date values
|
||||
*/
|
||||
@Test
|
||||
public void testCounterDateValues() {
|
||||
assertEquals(expectedCounterDateValues, result.getDateList().size());
|
||||
}
|
||||
|
||||
/**
|
||||
* Test number of Exceptions in the list after the run of DateFormatRunnalbe. A correct run should
|
||||
* deliver 5 exceptions
|
||||
*/
|
||||
@Test
|
||||
public void testCounterExceptions() {
|
||||
assertEquals(expectedCounterExceptions, result.getExceptionList().size());
|
||||
}
|
||||
}
|
@ -0,0 +1,164 @@
|
||||
/**
|
||||
* The MIT License
|
||||
* Copyright (c) 2016 Thomas Bauer
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
package com.iluwatar.tls;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.Future;
|
||||
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
/**
|
||||
*
|
||||
* Test of the Callable
|
||||
*
|
||||
* In this test {@link DateFormatCallable} is used by 4 threads in parallel
|
||||
* <p>
|
||||
* After a successful run 5 date values should be in the result object of each thread. All dates
|
||||
* should have the same value (15.11.2015). To avoid problems with time zone not the date instances
|
||||
* themselves are compared by the test. For the test the dates are converted into string format DD.MM.YYY
|
||||
* <p>
|
||||
* Additionally the number of list entries are tested for both the list with the date values
|
||||
* and the list with the exceptions
|
||||
*
|
||||
* @author Thomas Bauer, January 2017
|
||||
*
|
||||
*/
|
||||
public class DateFormatCallableTestMultiThread {
|
||||
|
||||
// Class variables used in setup() have to be static because setup() has to be static
|
||||
/**
|
||||
* Result object given back by DateFormatCallable, one for each thread
|
||||
* -- Array with converted date values
|
||||
* -- Array with thrown exceptions
|
||||
*/
|
||||
static Result[] result = new Result[4];
|
||||
|
||||
/**
|
||||
* The date values created by the run of of DateFormatRunnalbe. List will be filled in the setup() method
|
||||
*/
|
||||
@SuppressWarnings("serial")
|
||||
static class StringArrayList extends ArrayList<String> {
|
||||
/* nothing needed here */
|
||||
}
|
||||
static List<String>[] createdDateValues = new StringArrayList[4];
|
||||
|
||||
/**
|
||||
* Expected number of date values in the date value list created by each thread
|
||||
*/
|
||||
int expectedCounterDateValues = 5;
|
||||
|
||||
/**
|
||||
* Expected number of exceptions in the exception list created by each thread
|
||||
*/
|
||||
int expectedCounterExceptions = 0;
|
||||
|
||||
/**
|
||||
* Expected content of the list containing the date values created by each thread
|
||||
*/
|
||||
List<String> expectedDateValues = Arrays.asList("15.11.2015", "15.11.2015", "15.11.2015", "15.11.2015", "15.11.2015");
|
||||
|
||||
/**
|
||||
* Run Callable and prepare results for usage in the test methods
|
||||
*/
|
||||
@BeforeClass
|
||||
public static void setup() {
|
||||
// Create a callable
|
||||
DateFormatCallable callableDf = new DateFormatCallable("dd/MM/yyyy", "15/12/2015");
|
||||
// start thread using the Callable instance
|
||||
ExecutorService executor = Executors.newCachedThreadPool();
|
||||
Future<Result> futureResult1 = executor.submit(callableDf);
|
||||
Future<Result> futureResult2 = executor.submit(callableDf);
|
||||
Future<Result> futureResult3 = executor.submit(callableDf);
|
||||
Future<Result> futureResult4 = executor.submit(callableDf);
|
||||
try {
|
||||
result[0] = futureResult1.get();
|
||||
result[1] = futureResult2.get();
|
||||
result[2] = futureResult3.get();
|
||||
result[3] = futureResult4.get();
|
||||
for (int i = 0; i < result.length; i++) {
|
||||
createdDateValues[i] = convertDatesToString(result[i]);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
fail("Setup failed: " + e);
|
||||
}
|
||||
executor.shutdown();
|
||||
}
|
||||
|
||||
private static List<String> convertDatesToString(Result res) {
|
||||
// Format date value as DD.MM.YYYY
|
||||
if (res == null || res.getDateList() == null || res.getDateList().size() == 0) {
|
||||
return null;
|
||||
}
|
||||
List<String> returnList = new StringArrayList();
|
||||
|
||||
for (Date dt : res.getDateList()) {
|
||||
Calendar cal = Calendar.getInstance();
|
||||
cal.setTime(dt);
|
||||
returnList.add(cal.get(Calendar.DAY_OF_MONTH) + "." + cal.get(Calendar.MONTH) + "." + cal.get(Calendar.YEAR));
|
||||
}
|
||||
return returnList;
|
||||
}
|
||||
|
||||
/**
|
||||
* Test date values after the run of DateFormatRunnalbe. A correct run should deliver 5 times 15.12.2015
|
||||
* by each thread
|
||||
*/
|
||||
@Test
|
||||
public void testDateValues() {
|
||||
for (int i = 0; i < createdDateValues.length; i++) {
|
||||
assertEquals(expectedDateValues, createdDateValues[i]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Test number of dates in the list after the run of DateFormatRunnalbe. A correct run should
|
||||
* deliver 5 date values by each thread
|
||||
*/
|
||||
@Test
|
||||
public void testCounterDateValues() {
|
||||
for (int i = 0; i < result.length; i++) {
|
||||
assertEquals(expectedCounterDateValues, result[i].getDateList().size());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Test number of Exceptions in the list after the run of DateFormatRunnalbe. A correct run should
|
||||
* deliver no exceptions
|
||||
*/
|
||||
@Test
|
||||
public void testCounterExceptions() {
|
||||
for (int i = 0; i < result.length; i++) {
|
||||
assertEquals(expectedCounterExceptions, result[i].getExceptionList().size());
|
||||
}
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user