1 /*
2 * Fsgrep is a simple Java application which allows a user to
3 * search all files in a directory structure for lines matching
4 * a given pattern. Its functionality is a combination of the
5 * Unix 'find' and 'grep' utilities.
6 * Visit [http://fsgrep.sourceforge.net/] for more information.
7 *
8 * Copyright (C) 2003-2006 Murali Krishnan [murali_ca_us@users.sourceforge.net]
9 *
10 * Fsgrep is free software; you can redistribute it and/or modify
11 * it under the terms of version 2 of the GNU General Public
12 * License as published by the Free Software Foundation.
13 *
14 * Fsgrep is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with Fsgrep (see the file named LICENSE.txt); if not, write
21 * to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
22 * Boston, MA 02111-1307 USA
23 */
24
25 package mk.fsgrep.util.thread;
26
27
28 import java.util.*;
29
30
31 /***
32 *
33 *
34 * @author Murali Krishnan
35 *
36 */
37 public class ThreadPool implements RunCallback {
38
39 //------------------------------------------------------------
40 //- Class Variables
41
42
43
44 //------------------------------------------------------------
45 //- Class Functions
46
47
48
49 //------------------------------------------------------------
50 //- Instance Variables
51
52 private LinkedList<ThreadRunner> _available = new LinkedList<ThreadRunner>();
53 private String _name = "UNDEFINED";
54 private int _size = 0;
55
56
57
58 //------------------------------------------------------------
59 //- Constructors
60
61 public ThreadPool(String pName, int pSize) {
62 _name = pName;
63 _size = pSize;
64
65 initialize();
66 }
67
68
69 public synchronized void initialize() {
70 for (int i = 0; i < getSize(); i++) {
71 String runnerName = getName() + "-" + i;
72 getAvailable().addLast(new ThreadRunner(runnerName, this));
73 }
74 }
75
76
77 //------------------------------------------------------------
78 //- Accessors
79
80 protected LinkedList<ThreadRunner> getAvailable() {return _available;}
81 public String getName() {return _name;}
82 public int getSize() {return _size;}
83
84
85
86 //------------------------------------------------------------
87 //- Settors
88
89
90
91 //------------------------------------------------------------
92 //- Private/Protected Utility Functions
93
94 protected synchronized ThreadRunner removeFirst() {
95 return getAvailable().isEmpty() ? null : getAvailable().removeFirst();
96 }
97
98 protected ThreadRunner getNextAvailable() {
99 ThreadRunner result = removeFirst();
100
101 while (result == null) {
102 try {
103 Thread.sleep(500);
104 } catch (InterruptedException ie) {
105 // Do nothing.
106 }
107
108 result = removeFirst();
109 }
110
111 return result;
112 }
113
114
115 protected synchronized int findNumberAvailable() {
116 int result = getAvailable().size();
117
118 return result;
119 }
120
121
122 //------------------------------------------------------------
123 //- Public Interface Functions
124
125 public String toString() {
126 String result = "ThreadPool[\"" + getName() + "\", " +
127 findNumberAvailable() + "/" + getSize() + "]";
128
129 return result;
130 }
131
132
133 public void run(Runnable runnable) {
134 ThreadRunner runner = getNextAvailable();
135 runner.execute(runnable);
136 }
137
138
139 public void waitForCompletion() {
140 while (findNumberAvailable() > 0) {
141 try {
142 Thread.sleep(250);
143 } catch (InterruptedException ie) {
144 // Do nothing.
145 }
146 }
147 }
148
149
150 //------------------------------------------------------------
151 //- Class Interface Functions
152
153 public synchronized void runFinished(ThreadRunner runner) {
154 getAvailable().addLast(runner);
155 }
156
157
158
159
160 //------------------------------------------------------------
161 //- Inner Classes
162
163
164
165 //------------------------------------------------------------
166 //- Main
167
168
169
170 }