[prev in list] [next in list] [prev in thread] [next in thread]
List: ant-dev
Subject: Re: Date concurrency
From: Andrey Pavlenko <andrey.a.pavlenko () googlemail ! com>
Date: 2010-08-18 14:13:46
Message-ID: AANLkTindZqAbU_Ceu2OSbHTyP+1b=ZhRvQ_7gWZVfSXO () mail ! gmail ! com
[Download RAW message or body]
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 <charleso@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
[prev in list] [next in list] [prev in thread] [next in thread]
Configure |
About |
News |
Add a list |
Sponsored by KoreLogic