1 /************************************************************
2 * Copyright *
3 * Portions of this software are Copyright (c) 1993 - 2002, *
4 * Chad Z. Hower (Kudzu) and the Indy Pit Crew *
5 * - http://www.nevrona.com/Indy/ *
6 ************************************************************/
7 package org.indy;
8
9 /***
10 * Provides a skeletal implementation of the {@link ThreadManager} interface
11 * that clients can use as the basis for their own implementations.
12 *
13 * @author OTG
14 * @version 1.0
15 */
16 public abstract class AbstractPeerThreadManager extends BaseComponent
17 implements PeerThreadManager {
18 private PeerThreadFactory threadFactory = new DefaultPeerThreadFactory();
19 private IndyThread.Priority threadPriority;
20
21 /***
22 * Sets the {@link ThreadFactory} to use for this manager.
23 *
24 * @param factory
25 */
26 public void setThreadFactory(PeerThreadFactory factory) {
27 if (factory == null) {
28 throw new NullPointerException();
29 }
30
31 threadFactory = factory;
32 }
33
34 /***
35 * Returns the {@link ThreadFactory} instance used by this <code>ThreadManager</code>
36 *
37 *@return The <code>ThreadFactory</code>
38 */
39 public PeerThreadFactory getThreadFactory() {
40 return threadFactory;
41 }
42
43 /***
44 * Returns a new thread from the manager. Clients must provide an implementation of this method.
45 * The exact behaviour of this will depend on the implementation.
46 *
47 *@return A thread.
48 */
49 public abstract PeerThread getThread();
50
51 /***
52 * A conviencie method for descendents to create new threads with their priority set.
53 * Uses the associated {@link ThreadFactory} instance to create a new thread and sets its priority.
54 *
55 * @return A new {@link PeerThread} instance from the {@link ThreadFacotry}
56 */
57 protected PeerThread createNewThread() {
58 if (threadFactory == null) {
59 throw new NullPointerException();
60 }
61
62 PeerThread result = threadFactory.newThread();
63
64 if (threadPriority != null) {
65 result.setPriority(threadPriority);
66 }
67
68 return result;
69 }
70
71 /***
72 * Returns a thread to this <code>ThreadManager</code>.
73 * Descendents need to provide their own implementation of this method.
74 *
75 * @param thread The thread to return to the <code>ThreadManager</code>
76 */
77 public abstract void releaseThread(PeerThread thread)
78 throws InterruptedException;
79
80 /***
81 * This is a no-op implementation as a convinience for descendents that provide no special termination functions.
82 * For details of the general contract of this method see {@link ThreadManager}.
83 *
84 * @see ThreadManager
85 */
86
87 //public void terminateThreads() { }
88
89 /***
90 * Returns the <code>Priority</code> for threads provided by this <code>ThreadManager</code>
91 *
92 * @return The priority for threads returned by this manager.
93 */
94 public IndyThread.Priority getPriority() {
95 return threadPriority;
96 }
97
98 /***
99 * Sets the <code>Priority</code> for threads returned by this manager.
100 *
101 * @param p The new <code>Priority</code>
102 */
103 public void setPriority(IndyThread.Priority p) {
104 threadPriority = p;
105 }
106 }
This page was automatically generated by Maven