You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@ant.apache.org by Charles O'Farrell <ch...@gmail.com> on 2010/08/17 06:11:50 UTC

Date concurrency

Hi Ivy Devs,

Sorry to bother you, but we've run into some annoying issues with Ivy
and concurrency (using ant parallel). For some unbelievably stupid
reason SimpleDateFormat isn't thread safe.

http://download-llnw.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html#synchronization

Please find below a patch which illustrates one possible fix. I can
imagine you might want to do something completely different, this is
more of a show of good faith. Please let me know if you need me to
raise a Bug and/or submit a different patch.

Cheers,

Charles

diff --git a/src/java/org/apache/ivy/Ivy.java b/src/java/org/apache/ivy/Ivy.java
index 3d9d8cd..608b364 100644
--- a/src/java/org/apache/ivy/Ivy.java
+++ b/src/java/org/apache/ivy/Ivy.java
@@ -24,6 +24,7 @@ import java.net.URL;
 import java.text.ParseException;
 import java.text.SimpleDateFormat;
 import java.util.Collection;
+import java.util.Date;
 import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
@@ -129,8 +130,24 @@ public class Ivy {

     private static final int KILO = 1024;

-    public static final SimpleDateFormat DATE_FORMAT = new
SimpleDateFormat("yyyyMMddHHmmss");
-
+    public static final ThreadSafeDateFormat DATE_FORMAT = new
ThreadSafeDateFormat("yyyyMMddHHmmss");
+
+    public static class ThreadSafeDateFormat {
+        private final String format;
+
+        public ThreadSafeDateFormat(String format) {
+            this.format = format;
+        }
+
+        public Date parse(String date) throws ParseException {
+            return new SimpleDateFormat(format).parse(date);
+        }
+
+        public String format(Date date) {
+            return new SimpleDateFormat(format).format(date);
+        }
+    }
+
     /**
      * the current version of Ivy, as displayed on the console when
      * Ivy is initialized

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@ant.apache.org
For additional commands, e-mail: dev-help@ant.apache.org


Re: Date concurrency

Posted by Andrey Pavlenko <an...@googlemail.com>.
I would suggest to use ThreadLocal instead of overriding SimpleDateFormat.
Something like this:

    private static final ThreadLocal LOCAL_DATE_FORMAT = new ThreadLocal() {
        protected Object initialValue() {
            return new SimpleDateFormat("yyyyMMddHHmmss");
        };
    };

    public static SimpleDateFormat getDateFormat() {
        return (SimpleDateFormat) LOCAL_DATE_FORMAT.get();
    }



On Tue, Aug 17, 2010 at 8:11 AM, Charles O'Farrell <ch...@gmail.com> wrote:
> Hi Ivy Devs,
>
> Sorry to bother you, but we've run into some annoying issues with Ivy
> and concurrency (using ant parallel). For some unbelievably stupid
> reason SimpleDateFormat isn't thread safe.
>
> http://download-llnw.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html#synchronization
>
> Please find below a patch which illustrates one possible fix. I can
> imagine you might want to do something completely different, this is
> more of a show of good faith. Please let me know if you need me to
> raise a Bug and/or submit a different patch.
>
> Cheers,
>
> Charles
>
> diff --git a/src/java/org/apache/ivy/Ivy.java b/src/java/org/apache/ivy/Ivy.java
> index 3d9d8cd..608b364 100644
> --- a/src/java/org/apache/ivy/Ivy.java
> +++ b/src/java/org/apache/ivy/Ivy.java
> @@ -24,6 +24,7 @@ import java.net.URL;
>  import java.text.ParseException;
>  import java.text.SimpleDateFormat;
>  import java.util.Collection;
> +import java.util.Date;
>  import java.util.Iterator;
>  import java.util.List;
>  import java.util.Map;
> @@ -129,8 +130,24 @@ public class Ivy {
>
>     private static final int KILO = 1024;
>
> -    public static final SimpleDateFormat DATE_FORMAT = new
> SimpleDateFormat("yyyyMMddHHmmss");
> -
> +    public static final ThreadSafeDateFormat DATE_FORMAT = new
> ThreadSafeDateFormat("yyyyMMddHHmmss");
> +
> +    public static class ThreadSafeDateFormat {
> +        private final String format;
> +
> +        public ThreadSafeDateFormat(String format) {
> +            this.format = format;
> +        }
> +
> +        public Date parse(String date) throws ParseException {
> +            return new SimpleDateFormat(format).parse(date);
> +        }
> +
> +        public String format(Date date) {
> +            return new SimpleDateFormat(format).format(date);
> +        }
> +    }
> +
>     /**
>      * the current version of Ivy, as displayed on the console when
>      * Ivy is initialized
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: dev-unsubscribe@ant.apache.org
> For additional commands, e-mail: dev-help@ant.apache.org
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@ant.apache.org
For additional commands, e-mail: dev-help@ant.apache.org