001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one
003     * or more contributor license agreements. See the NOTICE file
004     * distributed with this work for additional information
005     * regarding copyright ownership. The ASF licenses this file
006     * to you under the Apache License, Version 2.0 (the  "License");
007     * you may not use this file except in compliance with the License.
008     * You may obtain a copy of the License at
009     *
010     *     http://www.apache.org/licenses/LICENSE-2.0
011     *
012     * Unless required by applicable law or agreed to in writing, software
013     * distributed under the License is distributed on an "AS IS" BASIS,
014     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015     * See the License for the specific language governing permissions and
016     * limitations under the License.
017     */
018    /*
019     * $Id: ThreadControllerWrapper.java 468655 2006-10-28 07:12:06Z minchau $
020     */
021    package org.apache.xml.utils;
022    
023    /**
024     * A utility class that wraps the ThreadController, which is used
025     * by IncrementalSAXSource for the incremental building of DTM.
026     */
027    public class ThreadControllerWrapper
028    {
029      
030      /** The ThreadController pool   */
031      private static ThreadController m_tpool = new ThreadController();
032      
033      public static Thread runThread(Runnable runnable, int priority)
034      {
035        return m_tpool.run(runnable, priority);
036      }
037      
038      public static void waitThread(Thread worker, Runnable task)
039        throws InterruptedException
040      {
041        m_tpool.waitThread(worker, task);
042      }
043      
044      /**
045       * Thread controller utility class for incremental SAX source. Must 
046       * be overriden with a derived class to support thread pooling.
047       *
048       * All thread-related stuff is in this class.
049       */
050      public static class ThreadController
051      {
052    
053        /**
054         * Will get a thread from the pool, execute the task
055         *  and return the thread to the pool.
056         *
057         *  The return value is used only to wait for completion
058         *
059         *
060         * NEEDSDOC @param task
061         * @param priority if >0 the task will run with the given priority
062         *  ( doesn't seem to be used in xalan, since it's allways the default )
063         * @return  The thread that is running the task, can be used
064         *          to wait for completion
065         */
066        public Thread run(Runnable task, int priority)
067        {
068    
069          Thread t = new Thread(task);
070    
071          t.start();
072    
073          //       if( priority > 0 )
074          //      t.setPriority( priority );
075          return t;
076        }
077    
078        /**
079         *  Wait until the task is completed on the worker
080         *  thread.
081         *
082         * NEEDSDOC @param worker
083         * NEEDSDOC @param task
084         *
085         * @throws InterruptedException
086         */
087        public void waitThread(Thread worker, Runnable task)
088                throws InterruptedException
089        {
090    
091          // This should wait until the transformThread is considered not alive.
092          worker.join();
093        }
094      }
095     
096    }