001: /*
002: * MCS Media Computer Software Copyright (c) 2006 by MCS
003: * -------------------------------------- Created on 11.04.2006 by w.klaas
004: *
005: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
006: * use this file except in compliance with the License. You may obtain a copy of
007: * the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
013: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
014: * License for the specific language governing permissions and limitations under
015: * the License.
016: */
017: package de.mcs.utils;
018:
019: import java.util.ArrayList;
020: import java.util.Queue;
021: import java.util.concurrent.LinkedBlockingQueue;
022:
023: /**
024: * this class is intend to implement a thread pool with the possibility to start
025: * and stop all threads and to interact with some other parts of the program.
026: *
027: * @author w.klaas
028: */
029: public class ThreadPool extends Thread {
030:
031: /** the thread pool. */
032: private ArrayList<WorkingThread> pool;
033:
034: /** the working queue. */
035: private Queue<Runnable> queue;
036:
037: /** maximal count of threads. */
038: private int maxCount;
039:
040: /**
041: * instancing the thread pool. With max count of parallel threads.
042: *
043: * @param maxcount
044: * maximal count of threads
045: */
046: public ThreadPool(final int maxcount) {
047: this .maxCount = maxcount;
048: this .pool = new ArrayList<WorkingThread>(maxCount);
049: this .queue = new LinkedBlockingQueue<Runnable>(100);
050: }
051:
052: /**
053: * Adding a new working part to the queue.
054: *
055: * @param work
056: * the runnable to start.
057: * @return <code>true</code> if the work could be added, otherwise
058: * <code>false</code>
059: */
060: public final boolean addWorkingPart(final Runnable work) {
061: return queue.offer(work);
062: }
063:
064: /**
065: * removing a working part from the queue.
066: *
067: * @param work
068: * the runnable to remove.
069: * @return <code>true</code> if the work could be removed, otherwise
070: * <code>false</code>
071: */
072: public final boolean removeWorkingPart(final Runnable work) {
073: return queue.remove(work);
074: }
075:
076: /**
077: * @see java.lang.Thread#run()
078: */
079: @Override
080: public final void run() {
081: while (!isInterrupted()) {
082: if (!queue.isEmpty()) {
083: Runnable run = queue.poll();
084: try {
085: WorkingThread thread = getNextFreeThread();
086: thread.setRunnable(run);
087: thread.start();
088: } catch (InterruptedException e) {
089: // e.printStackTrace();
090: }
091: } else {
092: try {
093: sleep(100);
094: } catch (InterruptedException e) {
095: // e.printStackTrace();
096: }
097: }
098: }
099: }
100:
101: /**
102: * getting the next thread that should be startet.
103: *
104: * @return WorkingThread
105: * @throws InterruptedException
106: * if the process ahould be stoped.
107: */
108: private WorkingThread getNextFreeThread()
109: throws InterruptedException {
110: // zunächst mal schauen ob die maxcount nicht geändert worden ist und
111: // demnach dann die Anzahl der Working Threads anpassen
112: while (maxCount < pool.size()) {
113: // jetzt warten bis die Anzahl wieder stimmt.
114: WorkingThread thread = getFreeThread();
115: pool.remove(thread);
116: thread = null;
117: }
118: while (maxCount > pool.size()) {
119: WorkingThread thread = new WorkingThread();
120: pool.add(thread);
121: }
122: return getFreeThread();
123: }
124:
125: /**
126: * getting the next thread that should be startet.
127: *
128: * @return WorkingThread
129: * @throws InterruptedException
130: * if the process ahould be stoped.
131: */
132: private WorkingThread getFreeThread() throws InterruptedException {
133: while (true) {
134: for (WorkingThread thread : pool) {
135: if (thread.isFree()) {
136: thread.setBusy();
137: return thread;
138: }
139: }
140: sleep(50);
141: }
142: }
143: }
|