1. /*
  2. * Copyright 1999-2004 The Apache Software Foundation.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /*
  17. * $Id: ThreadControllerWrapper.java,v 1.3 2004/02/17 04:21:14 minchau Exp $
  18. */
  19. package com.sun.org.apache.xml.internal.utils;
  20. /**
  21. * A utility class that wraps the ThreadController, which is used
  22. * by IncrementalSAXSource for the incremental building of DTM.
  23. */
  24. public class ThreadControllerWrapper
  25. {
  26. /** The ThreadController pool */
  27. static ThreadController m_tpool = new ThreadController();
  28. /**
  29. * Change the ThreadController that will be used to
  30. * manage the transform threads.
  31. *
  32. * @param tp A ThreadController object
  33. */
  34. public static void setThreadController(ThreadController tpool)
  35. {
  36. m_tpool = tpool;
  37. }
  38. public static Thread runThread(Runnable runnable, int priority)
  39. {
  40. return m_tpool.run(runnable, priority);
  41. }
  42. public static void waitThread(Thread worker, Runnable task)
  43. throws InterruptedException
  44. {
  45. m_tpool.waitThread(worker, task);
  46. }
  47. /**
  48. * Thread controller utility class for incremental SAX source. Must
  49. * be overriden with a derived class to support thread pooling.
  50. *
  51. * All thread-related stuff is in this class.
  52. */
  53. public static class ThreadController
  54. {
  55. /**
  56. * Will get a thread from the pool, execute the task
  57. * and return the thread to the pool.
  58. *
  59. * The return value is used only to wait for completion
  60. *
  61. *
  62. * NEEDSDOC @param task
  63. * @param priority if >0 the task will run with the given priority
  64. * ( doesn't seem to be used in xalan, since it's allways the default )
  65. * @returns The thread that is running the task, can be used
  66. * to wait for completion
  67. *
  68. * NEEDSDOC ($objectName$) @return
  69. */
  70. public Thread run(Runnable task, int priority)
  71. {
  72. Thread t = new Thread(task);
  73. t.start();
  74. // if( priority > 0 )
  75. // t.setPriority( priority );
  76. return t;
  77. }
  78. /**
  79. * Wait until the task is completed on the worker
  80. * thread.
  81. *
  82. * NEEDSDOC @param worker
  83. * NEEDSDOC @param task
  84. *
  85. * @throws InterruptedException
  86. */
  87. public void waitThread(Thread worker, Runnable task)
  88. throws InterruptedException
  89. {
  90. // This should wait until the transformThread is considered not alive.
  91. worker.join();
  92. }
  93. }
  94. }