01: /*_############################################################################
02: _##
03: _## SNMP4J - DefaultThreadFactory.java
04: _##
05: _## Copyright (C) 2003-2008 Frank Fock and Jochen Katz (SNMP4J.org)
06: _##
07: _## Licensed under the Apache License, Version 2.0 (the "License");
08: _## you may not use this file except in compliance with the License.
09: _## You may obtain a copy of the License at
10: _##
11: _## http://www.apache.org/licenses/LICENSE-2.0
12: _##
13: _## Unless required by applicable law or agreed to in writing, software
14: _## distributed under the License is distributed on an "AS IS" BASIS,
15: _## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16: _## See the License for the specific language governing permissions and
17: _## limitations under the License.
18: _##
19: _##########################################################################*/
20:
21: package org.snmp4j.util;
22:
23: import org.snmp4j.util.*;
24:
25: public class DefaultThreadFactory implements ThreadFactory {
26:
27: public DefaultThreadFactory() {
28: }
29:
30: /**
31: * Creates a new thread of execution for the supplied task.
32: *
33: * @param name the name of the execution thread.
34: * @param task the task to be executed in the new thread.
35: * @return the <code>WorkerTask</code> wrapper to control start and
36: * termination of the thread.
37: */
38: public WorkerTask createWorkerThread(String name, WorkerTask task,
39: boolean daemon) {
40: WorkerThread wt = new WorkerThread(name, task);
41: wt.setDaemon(daemon);
42: return wt;
43: }
44:
45: public class WorkerThread extends Thread implements WorkerTask {
46:
47: private WorkerTask task;
48: private boolean started = false;
49:
50: public WorkerThread(String name, WorkerTask task) {
51: super (task, name);
52: this .task = task;
53: }
54:
55: public void terminate() {
56: task.terminate();
57: }
58:
59: public void run() {
60: if (!started) {
61: started = true;
62: super.start();
63: } else {
64: super.run();
65: }
66: }
67: }
68: }
|