KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > thoughtworks > xstream > converters > basic > ThreadSafeSimpleDateFormat


1 package com.thoughtworks.xstream.converters.basic;
2
3 import java.text.DateFormat JavaDoc;
4 import java.text.ParseException JavaDoc;
5 import java.text.SimpleDateFormat JavaDoc;
6 import java.util.Date JavaDoc;
7
8 /**
9  * Wrapper around java.text.SimpleDateFormat that can
10  * be called by multiple threads concurrently.
11  * <p/>
12  * <p>SimpleDateFormat has a high overhead in creating
13  * and is not thread safe. To make best use of resources,
14  * the ThreadSafeSimpleDateFormat provides a dynamically
15  * sizing pool of instances, each of which will only
16  * be called by a single thread at a time.</p>
17  * <p/>
18  * <p>The pool has a maximum capacity, to limit overhead.
19  * If all instances in the pool are in use and another is
20  * required, it shall block until one becomes available.</p>
21  *
22  * @author Joe Walnes
23  */

24 public class ThreadSafeSimpleDateFormat {
25
26     private String JavaDoc formatString;
27     private DateFormat JavaDoc[] pool;
28     private int nextAvailable = 0;
29
30     public ThreadSafeSimpleDateFormat(String JavaDoc format, int initialPoolSize, int maxPoolSize) {
31         this.formatString = format;
32         nextAvailable = -1;
33         pool = new DateFormat JavaDoc[maxPoolSize];
34         for (int i = 0; i < initialPoolSize; i++) {
35             putInPool(createNew());
36         }
37     }
38
39     public String JavaDoc format(Date JavaDoc date) {
40         DateFormat JavaDoc format = fetchFromPool();
41         try {
42             return format.format(date);
43         } finally {
44             putInPool(format);
45         }
46     }
47
48     public Date JavaDoc parse(String JavaDoc date) throws ParseException JavaDoc {
49         DateFormat JavaDoc format = fetchFromPool();
50         try {
51             return format.parse(date);
52         } finally {
53             putInPool(format);
54         }
55     }
56
57     private DateFormat JavaDoc fetchFromPool() {
58         DateFormat JavaDoc result;
59         synchronized (pool) {
60             while (nextAvailable < 0) {
61                 try {
62                     pool.wait();
63                 } catch (InterruptedException JavaDoc e) {
64                     throw new RuntimeException JavaDoc("Interrupted whilst waiting " +
65                             "for a free item in the pool : " + e.getMessage());
66                 }
67             }
68             result = pool[nextAvailable];
69             nextAvailable--;
70         }
71         if (result == null) {
72             result = createNew();
73             putInPool(result);
74         }
75         return result;
76     }
77
78     private void putInPool(DateFormat JavaDoc format) {
79         synchronized (pool) {
80             nextAvailable++;
81             pool[nextAvailable] = format;
82             pool.notify();
83         }
84     }
85
86     private DateFormat JavaDoc createNew() {
87         return new SimpleDateFormat JavaDoc(formatString);
88     }
89
90 }
91
Popular Tags