~ubuntu-branches/ubuntu/trusty/ehcache/trusty

« back to all changes in this revision

Viewing changes to src/main/java/net/sf/ehcache/constructs/nonstop/NonstopThreadPool.java

  • Committer: Package Import Robot
  • Author(s): Emmanuel Bourg
  • Date: 2013-05-06 14:53:07 UTC
  • mfrom: (1.1.7) (2.1.8 sid)
  • Revision ID: package-import@ubuntu.com-20130506145307-v5bhw5yu70re00l3
Tags: 2.6.7-1
* Team upload.
* New upstream release

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/**
2
 
 *  Copyright 2003-2010 Terracotta, Inc.
 
2
 *  Copyright Terracotta, Inc.
3
3
 *
4
4
 *  Licensed under the Apache License, Version 2.0 (the "License");
5
5
 *  you may not use this file except in compliance with the License.
20
20
import java.lang.ref.WeakReference;
21
21
import java.util.HashSet;
22
22
import java.util.Map;
 
23
import java.util.Map.Entry;
23
24
import java.util.Set;
24
25
import java.util.WeakHashMap;
25
 
import java.util.Map.Entry;
26
26
import java.util.concurrent.Callable;
27
27
import java.util.concurrent.Future;
28
28
import java.util.concurrent.FutureTask;
30
30
import java.util.concurrent.ThreadFactory;
31
31
import java.util.concurrent.atomic.AtomicReference;
32
32
 
 
33
import net.sf.ehcache.util.lang.VicariousThreadLocal;
 
34
 
33
35
/**
34
36
 * A thread pool that creates another thread pool per requesting thread.
35
37
 *
46
48
    private final Object workersLock = new Object();
47
49
    private final AtomicReference<State> state = new AtomicReference<State>(State.RUNNING);
48
50
    private final ReferenceQueue<Thread> gcedThreadsReferenceQueue = new ReferenceQueue<Thread>();
49
 
    private final ThreadLocal<WorkerThreadLocal> workerThreadLocal = new ThreadLocal<WorkerThreadLocal>() {
 
51
    private final VicariousThreadLocal<WorkerThreadLocal> workerThreadLocal = new VicariousThreadLocal<WorkerThreadLocal>() {
50
52
        @Override
51
53
        protected WorkerThreadLocal initialValue() {
52
54
            synchronized (workersLock) {
149
151
    }
150
152
 
151
153
    /**
 
154
     * Get the stack trace details of the nonstop thread for the current thread
 
155
     */
 
156
    public StackTraceElement[] getNonstopThreadStackTrace() {
 
157
        return workerThreadLocal.get().getStackTrace();
 
158
    }
 
159
 
 
160
    /**
152
161
     * Private class
153
162
     *
154
163
     * @author Abhishek Sanoujam
165
174
            this.appThreadReference = new WeakWorkerReference(this.worker, Thread.currentThread(), gcedThreadsReferenceQueue);
166
175
        }
167
176
 
 
177
        public StackTraceElement[] getStackTrace() {
 
178
            return worker.getStackTrace();
 
179
        }
 
180
 
168
181
        public void shutdownNow() {
169
182
            worker.shutdownNow();
170
183
        }
171
184
 
172
185
        public <T> Future<T> submit(Callable<T> task) {
173
 
            FutureTask<T> ftask = new FutureTask<T>(task);
 
186
            final ClassLoader tccl = Thread.currentThread().getContextClassLoader();
 
187
            FutureTask<T> ftask = new FutureTask<T>(task) {
 
188
                @Override
 
189
                public void run() {
 
190
                    ClassLoader prev = Thread.currentThread().getContextClassLoader();
 
191
                    Thread.currentThread().setContextClassLoader(tccl);
 
192
                    try {
 
193
                        super.run();
 
194
                    } finally {
 
195
                        Thread.currentThread().setContextClassLoader(prev);
 
196
                    }
 
197
                }
 
198
            };
174
199
            worker.addTask(ftask);
175
200
            return ftask;
176
201
        }
183
208
     *
184
209
     */
185
210
    private static class Worker implements Runnable {
 
211
        private static final StackTraceElement[] EMPTY_STACK_TRACE = new StackTraceElement[0];
186
212
        private final WorkerTaskHolder workerTaskHolder;
187
213
        private volatile boolean shutdown;
188
214
        private volatile Thread workerThread;
192
218
            this.workerTaskHolder = new WorkerTaskHolder();
193
219
        }
194
220
 
 
221
        public StackTraceElement[] getStackTrace() {
 
222
            if (workerThread == null) {
 
223
                return EMPTY_STACK_TRACE;
 
224
            }
 
225
            return workerThread.getStackTrace();
 
226
        }
 
227
 
195
228
        public void run() {
196
229
            workerThread = Thread.currentThread();
197
230
            while (!shutdown) {