Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 213d16955932cb5b87e7ae6868fdeaebd2ee16a5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/*
 * Copyright (c) 2013 Eike Stepper (Berlin, Germany) and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Eike Stepper - initial API and implementation
 */
package org.eclipse.net4j.util.concurrent;

import org.eclipse.net4j.internal.util.bundle.OM;
import org.eclipse.net4j.util.om.OMPlatform;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Map;
import java.util.TimerTask;
import java.util.WeakHashMap;

/**
 * @author Eike Stepper
 * @since 3.3
 */
public abstract class TrackableTimerTask extends TimerTask
{
  /**
   * The boolean value of the system property <code>org.eclipse.net4j.util.concurrent.TrackTimerTasks</code>.
   */
  public static final boolean TRACK_TIMER_TASKS = Boolean
      .parseBoolean(OMPlatform.INSTANCE.getProperty("org.eclipse.net4j.util.concurrent.TrackTimerTasks", "false"));

  private static final Map<TrackableTimerTask, ConstructionInfo> CONSTRUCTION_INFOS = TRACK_TIMER_TASKS
      ? new WeakHashMap<TrackableTimerTask, ConstructionInfo>() : null;

  protected TrackableTimerTask()
  {
    if (TRACK_TIMER_TASKS)
    {
      synchronized (CONSTRUCTION_INFOS)
      {
        CONSTRUCTION_INFOS.put(this, new ConstructionInfo());
      }
    }
  }

  @Override
  public boolean cancel()
  {
    if (TRACK_TIMER_TASKS)
    {
      synchronized (CONSTRUCTION_INFOS)
      {
        CONSTRUCTION_INFOS.remove(this);
      }
    }

    return super.cancel();
  }

  public static Collection<Exception> getConstructionStackTraces(long minLifeTimeMillis)
  {
    if (!TRACK_TIMER_TASKS)
    {
      return Collections.emptyList();
    }

    long maxTimeStamp = System.currentTimeMillis() - minLifeTimeMillis;
    Collection<Exception> result = new ArrayList<Exception>();

    synchronized (CONSTRUCTION_INFOS)
    {
      for (ConstructionInfo constructionInfo : CONSTRUCTION_INFOS.values())
      {
        if (constructionInfo.timeStamp < maxTimeStamp)
        {
          result.add(constructionInfo.stackTrace);
        }
      }
    }

    return result;
  }

  public static void logConstructionStackTraces(long minLifeTimeMillis)
  {
    if (TRACK_TIMER_TASKS)
    {
      Collection<Exception> constructionStackTraces = getConstructionStackTraces(minLifeTimeMillis);
      if (!constructionStackTraces.isEmpty())
      {
        for (Exception exception : constructionStackTraces)
        {
          OM.LOG.info(exception);
        }
      }
    }
  }

  /**
   * @author Eike Stepper
   */
  private final class ConstructionInfo
  {
    public final long timeStamp = System.currentTimeMillis();

    public final Exception stackTrace = getStackTrace();

    private Exception getStackTrace()
    {
      try
      {
        throw new Exception("The timer task " + TrackableTimerTask.this + " has been constructed here:");
      }
      catch (Exception ex)
      {
        return ex;
      }
    }
  }
}

Back to the top