Home » geronimo-2.2-source-release » org.apache.geronimo.concurrent.impl.thread » [javadoc | source]

    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.geronimo.concurrent.impl.thread;
   18   
   19   import java.util.ArrayList;
   20   import java.util.Collections;
   21   import java.util.List;
   22   
   23   import javax.util.concurrent.ManagedThreadFactory;
   24   
   25   import org.apache.commons.logging.Log;
   26   import org.apache.commons.logging.LogFactory;
   27   import org.apache.geronimo.concurrent.thread.ManagedThread;
   28   import org.apache.geronimo.concurrent.thread.ThreadLifecycleListener;
   29   
   30   public class TrackingManagedThreadFactory implements ManagedThreadFactory, ThreadLifecycleListener {
   31   
   32       private final static Log LOG = LogFactory.getLog(TrackingManagedThreadFactory.class);
   33       
   34       protected GeronimoManagedThreadFactory threadFactory;      
   35       protected List<ManagedThread> threads = 
   36           Collections.synchronizedList(new ArrayList<ManagedThread>());
   37       
   38       public TrackingManagedThreadFactory(GeronimoManagedThreadFactory threadFactory) {       
   39           this.threadFactory = threadFactory;       
   40           if (this.threadFactory == null) {
   41               throw new NullPointerException("threadFactory is null");
   42           }
   43       }
   44       
   45       public Thread newThread(Runnable runnable) {                
   46           ManagedThread thread = (ManagedThread)this.threadFactory.newThread(runnable);
   47           
   48           // set listener so that this class gets notifications of thread lifecycle events
   49           thread.setThreadLifecycleListener(this);
   50           
   51           this.threads.add(thread);        
   52           
   53           LOG.debug("Thread created: " + thread);
   54           
   55           return thread;
   56       }
   57       
   58       public void threadStarted(Thread thread) {       
   59           this.threadFactory.threadStarted(thread);
   60           
   61           LOG.debug("Thread started: " + thread);
   62       }
   63       
   64       public void threadStopped(Thread thread) {
   65           this.threads.remove(thread);
   66           this.threadFactory.threadStopped(thread);
   67           
   68           LOG.debug("Thread stopped: " + thread);
   69       }
   70       
   71       protected List<ManagedThread> getThreadList() {
   72           synchronized(this.threads) {
   73               return new ArrayList<ManagedThread>(this.threads);
   74           }
   75       }
   76       
   77       protected void interruptThreads() {
   78           synchronized(this.threads) {
   79               for (Thread thread : this.threads) {
   80                   thread.interrupt();               
   81               }
   82           }
   83       }
   84                      
   85   }

Home » geronimo-2.2-source-release » org.apache.geronimo.concurrent.impl.thread » [javadoc | source]