|
| 1 | +/** |
| 2 | + * The MIT License |
| 3 | + * Copyright (c) 2016 Thomas Bauer |
| 4 | + * |
| 5 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 | + * of this software and associated documentation files (the "Software"), to deal |
| 7 | + * in the Software without restriction, including without limitation the rights |
| 8 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 | + * copies of the Software, and to permit persons to whom the Software is |
| 10 | + * furnished to do so, subject to the following conditions: |
| 11 | + * |
| 12 | + * The above copyright notice and this permission notice shall be included in |
| 13 | + * all copies or substantial portions of the Software. |
| 14 | + * |
| 15 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 18 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 21 | + * THE SOFTWARE. |
| 22 | + */ |
| 23 | + |
| 24 | +package com.iluwatar.tls; |
| 25 | + |
| 26 | +import java.util.ArrayList; |
| 27 | +import java.util.Calendar; |
| 28 | +import java.util.Collections; |
| 29 | +import java.util.Date; |
| 30 | +import java.util.List; |
| 31 | + |
| 32 | +/** |
| 33 | + * |
| 34 | + * This App shows an example for problems with global thread variables. A global |
| 35 | + * thread variable is a variable defined as class variable of a |
| 36 | + * XyzRunnable-Class. In this example in the class |
| 37 | + * {@link DateFormatUglyRunnable} |
| 38 | + * <p> |
| 39 | + * Example use case: A well known problem with threads are non thread-safe Java |
| 40 | + * classes. One example is the class SimpleDateFormat. |
| 41 | + * <p> |
| 42 | + * In the example an instance of SimpleDateFormat is created in the constructor |
| 43 | + * of the Runnable. The constructor initializes a class variable with the |
| 44 | + * instance. The Runnable only does convert a string date to a date format using |
| 45 | + * the instance of SimpleDateFormat. It does this 5 times. |
| 46 | + * <p> |
| 47 | + * In the example 4 threads are started to do the same. If you are lucky |
| 48 | + * everything works well. But if you try more often you will happen to see |
| 49 | + * Exceptions. And these Exceptions arise on arbitrary points of the run. |
| 50 | + * <p> |
| 51 | + * The reason for this exceptions is: The threads try to use internal instance |
| 52 | + * variables of the SimpleDateFormat instance at the same time (the date is |
| 53 | + * stored internal as member variable during parsing and may be overwritten by |
| 54 | + * another thread during a parse is still running) |
| 55 | + * <p> |
| 56 | + * And even without Exceptions the run may not deliver the correct results. |
| 57 | + * All date values should be the same after the run. Check it |
| 58 | + * |
| 59 | + */ |
| 60 | +public class AppUgly { |
| 61 | + // A list to collect the date values created in the the threads |
| 62 | + static List<Date> dateList = Collections.synchronizedList(new ArrayList<Date>()); |
| 63 | + // A list to collect Exceptions thrown in the threads |
| 64 | + static List<String> exceptionList = Collections.synchronizedList(new ArrayList<String>()); |
| 65 | + |
| 66 | + /** |
| 67 | + * Program entry point |
| 68 | + * |
| 69 | + * @param args |
| 70 | + * command line args |
| 71 | + */ |
| 72 | + public static void main(String[] args) { |
| 73 | + int counterDateValues = 0; |
| 74 | + int counterExceptions = 0; |
| 75 | + |
| 76 | + // Prepare the Runnable |
| 77 | + DateFormatUglyRunnable runnableDf = new DateFormatUglyRunnable("dd/MM/yyyy", "15/12/2015"); |
| 78 | + |
| 79 | + // 4 threads using the same Runnable |
| 80 | + Thread t1 = new Thread(runnableDf); |
| 81 | + Thread t2 = new Thread(runnableDf); |
| 82 | + Thread t3 = new Thread(runnableDf); |
| 83 | + Thread t4 = new Thread(runnableDf); |
| 84 | + t1.start(); |
| 85 | + t2.start(); |
| 86 | + t3.start(); |
| 87 | + t4.start(); |
| 88 | + try { |
| 89 | + t1.join(); |
| 90 | + t2.join(); |
| 91 | + t3.join(); |
| 92 | + t4.join(); |
| 93 | + } catch (InterruptedException e) { |
| 94 | + // Action not coded here |
| 95 | + } |
| 96 | + for (Date dt : dateList) { |
| 97 | + // a correct run should deliver 20 times 15.12.2015 |
| 98 | + counterDateValues++; |
| 99 | + Calendar cal = Calendar.getInstance(); |
| 100 | + cal.setTime(dt); |
| 101 | + // Formatted output of the date value: DD.MM.YYYY |
| 102 | + System.out.println(cal.get(Calendar.DAY_OF_MONTH) + "." + cal.get(Calendar.MONTH) + "." + cal.get(Calendar.YEAR)); |
| 103 | + } |
| 104 | + for (String ex : exceptionList) { |
| 105 | + // a correct run shouldn't deliver any exception |
| 106 | + counterExceptions++; |
| 107 | + System.out.println(ex); |
| 108 | + } |
| 109 | + System.out.println("The List dateList contains " + counterDateValues + " date values"); |
| 110 | + System.out.println("The List exceptionList contains " + counterExceptions + " exceptions"); |
| 111 | + } |
| 112 | +} |
0 commit comments