1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17 package org.apache.tomcat.util.threads;
18
19 import org.apache.juli.logging.Log;
20 import org.apache.juli.logging.LogFactory;
21
22 /**
23 * A Thread implementation that records the time at which it was created.
24 *
25 */
26 public class TaskThread extends Thread {
27
28 private static final Log log = LogFactory.getLog(TaskThread.class);
29 private final long creationTime;
30
31 public TaskThread(ThreadGroup group, Runnable target, String name) {
32 super(group, new WrappingRunnable(target), name);
33 this.creationTime = System.currentTimeMillis();
34 }
35
36 public TaskThread(ThreadGroup group, Runnable target, String name,
37 long stackSize) {
38 super(group, new WrappingRunnable(target), name, stackSize);
39 this.creationTime = System.currentTimeMillis();
40 }
41
42 /**
43 * @return the time (in ms) at which this thread was created
44 */
45 public final long getCreationTime() {
46 return creationTime;
47 }
48
49 /**
50 * Wraps a {@link Runnable} to swallow any {@link StopPooledThreadException}
51 * instead of letting it go and potentially trigger a break in a debugger.
52 */
53 private static class WrappingRunnable implements Runnable {
54 private Runnable wrappedRunnable;
55 WrappingRunnable(Runnable wrappedRunnable) {
56 this.wrappedRunnable = wrappedRunnable;
57 }
58 @Override
59 public void run() {
60 try {
61 wrappedRunnable.run();
62 } catch(StopPooledThreadException exc) {
63 //expected : we just swallow the exception to avoid disturbing
64 //debuggers like eclipse's
65 log.debug("Thread exiting on purpose", exc);
66 }
67 }
68
69 }
70
71 }
72