001: /* Copyright (c) 2001-2005, The HSQL Development Group
002: * All rights reserved.
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, are permitted provided that the following conditions are met:
006: *
007: * Redistributions of source code must retain the above copyright notice, this
008: * list of conditions and the following disclaimer.
009: *
010: * Redistributions in binary form must reproduce the above copyright notice,
011: * this list of conditions and the following disclaimer in the documentation
012: * and/or other materials provided with the distribution.
013: *
014: * Neither the name of the HSQL Development Group nor the names of its
015: * contributors may be used to endorse or promote products derived from this
016: * software without specific prior written permission.
017: *
018: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
019: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
020: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
021: * ARE DISCLAIMED. IN NO EVENT SHALL HSQL DEVELOPMENT GROUP, HSQLDB.ORG,
022: * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
023: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
024: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
025: * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
026: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
027: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
028: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
029: */
030:
031: package org.hsqldb.lib;
032:
033: /**
034: * The default HSQLDB thread factory implementation. This factory can be
035: * used to wrap other thread factories using the setImpl method, but, by
036: * default simply produces new, vanilla thread objects constructed with
037: * the supplied runnable object.
038: *
039: * @author boucherb@users
040: * @version 1.7.2
041: * @since 1.7.2
042: */
043: class HsqlThreadFactory implements ThreadFactory {
044:
045: /**
046: * The factory implementation. Typically, this will be the
047: * HsqlThreadFactory object itself.
048: */
049: protected ThreadFactory factory;
050:
051: /**
052: * Constructs a new HsqlThreadFactory that uses itself as the factory
053: * implementation.
054: */
055: public HsqlThreadFactory() {
056: this (null);
057: }
058:
059: /**
060: * Constructs a new HsqlThreadFactory whose retrieved threads come from the
061: * specified ThreadFactory object or from this factory implementation, if'
062: * the specified implementation is null.
063: *
064: * @param f the factory implementation this factory uses
065: */
066: public HsqlThreadFactory(ThreadFactory f) {
067: setImpl(f);
068: }
069:
070: /**
071: * Retreives a thread instance for running the specified Runnable
072: * @param r The runnable that the retrieved thread handles
073: * @return the requested thread inatance
074: */
075: public Thread newThread(Runnable r) {
076: return factory == this ? new Thread(r) : factory.newThread(r);
077: }
078:
079: /**
080: * Sets the factory implementation that this factory will use to
081: * produce threads. If the specified argument, f, is null, then
082: * this factory uses itself as the implementation.
083: *
084: * @param f the factory implementation that this factory will use
085: * to produce threads
086: * @return the previously installed factory implementation
087: */
088: public synchronized ThreadFactory setImpl(ThreadFactory f) {
089:
090: ThreadFactory old;
091:
092: old = factory;
093: factory = (f == null) ? this : f;
094:
095: return old;
096: }
097:
098: /**
099: * Retrieves the factory implementation that this factory is using
100: * to produce threads.
101: *
102: * @return the factory implementation that this factory is using to produce
103: * threads.
104: */
105: public synchronized ThreadFactory getImpl() {
106: return factory;
107: }
108: }
|