View Javadoc
1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
4    *
5    * This program and the accompanying materials are dual-licensed under
6    * either the terms of the Eclipse Public License v1.0 as published by
7    * the Eclipse Foundation
8    *
9    *   or (per the licensee's choosing)
10   *
11   * under the terms of the GNU Lesser General Public License version 2.1
12   * as published by the Free Software Foundation.
13   */
14  package ch.qos.logback.classic.issue.lbclassic36;
15  
16  import java.text.SimpleDateFormat;
17  //import org.joda.time.format.DateTimeFormat;
18  //import org.joda.time.format.DateTimeFormatter;
19  
20  import ch.qos.logback.core.contention.RunnableWithCounterAndDone;
21  
22  /**
23   * A runnable which behaves differently depending on the desired locking model.
24   * 
25   * @author Ralph Goers
26   * @author Ceki Gulcu
27   */
28  public class SelectiveDateFormattingRunnable extends RunnableWithCounterAndDone {
29  
30      public static final String ISO8601_PATTERN = "yyyy-MM-dd HH:mm:ss,SSS";
31  
32      enum FormattingModel {
33          SDF, JODA;
34      }
35  
36      FormattingModel model;
37      static long CACHE = 0;
38  
39      static SimpleDateFormat SDF = new SimpleDateFormat(ISO8601_PATTERN);
40  
41      // static final DateTimeFormatter JODA = DateTimeFormat
42      // .forPattern(ISO8601_PATTERN);
43  
44      SelectiveDateFormattingRunnable(FormattingModel model) {
45          this.model = model;
46      }
47  
48      public void run() {
49          switch (model) {
50          case SDF:
51              sdfRun();
52              break;
53          case JODA:
54              jodaRun();
55              break;
56          }
57      }
58  
59      void sdfRun() {
60  
61          for (;;) {
62              synchronized (SDF) {
63                  long now = System.currentTimeMillis();
64                  if (CACHE != now) {
65                      CACHE = now;
66                      SDF.format(now);
67                  }
68              }
69              counter++;
70              if (done) {
71                  return;
72              }
73          }
74      }
75  
76      void jodaRun() {
77          for (;;) {
78              long now = System.currentTimeMillis();
79              if (isCacheStale(now)) {
80                  // JODA.print(now);
81              }
82              counter++;
83              if (done) {
84                  return;
85              }
86          }
87      }
88  
89      private static boolean isCacheStale(long now) {
90          // synchronized (JODA) {
91          // if (CACHE != now) {
92          // CACHE = now;
93          // return true;
94          // }
95          // }
96          return false;
97      }
98  
99  }