Hi,
dieser Quelltext gibt folgendes aus:
Nun verstehe ich nicht, wo der Code-Abschnitt ist, der sagt, dass immer nur 4 Tasks gleichzeitig laufen können und falls einer fertig ist, gleich der nächste Task ausgeführt wird.
Liebe Grüße
Real
dieser Quelltext gibt folgendes aus:
Task 0: start
Task 1: start
Task 2: start
Task 3: start
Task 0: end
Task 4: start
Task 1: end
Task 5: start
Task 2: end
Task 6: start
Task 3: end
Task 7: start
Task 4: end
Task 5: end
Task 6: end
Task 7: end
Code:
import java.util.LinkedList;
/*A thread pool is a group of a limited number of threads that
are used to execute tasks.
*/
public class ThreadPool extends ThreadGroup{
private boolean isAlive;
private LinkedList taskQueue;
private int threadID;
private static int threadPoolID;
public ThreadPool(int numThreads){
super("ThreadPool "+ (threadPoolID++));
setDaemon(true);
isAlive = true;
taskQueue = new LinkedList();
for(byte i = 0; i < numThreads; i++){
new PooledThread().start();
}
}
public synchronized void runTask(Runnable task){
if(!isAlive)
throw new IllegalArgumentException();
if(task != null){
taskQueue.add(task);
notify();
}
}
protected synchronized Runnable getTask() throws InterruptedException{
while(taskQueue.size() == 0){
if(!isAlive){
return null;
}
wait();
}
return (Runnable) taskQueue.removeFirst();
}
/* Closes this ThreadPool and return ummediately. All threads are stopped
, and waiting tasks are not executed. Once a ThreadPool is closed, no more
tasks can be run in this ThreadPool */
public synchronized void close(){
if(isAlive){
isAlive = false;
taskQueue.clear();
interrupt();
}
}
/*Closes this ThreadPool and waits for all running threads to finish.
Any waiting tasks are executed */
public void join(){
//notify all waitings Threads that this ThreadPool is no longer alive
synchronized(this){
isAlive = false;
notifyAll();
}
//wait for all Threads to finish
Thread threads[] = new Thread[activeCount()];
int count = enumerate(threads);
for(byte i = 0; i < count; i++){
try{
threads[i].join();
}catch(InterruptedException ex){}
}
}
private class PooledThread extends Thread{
public PooledThread(){
super(ThreadPool.this, "Pooled Thread-" + (threadID++));
}
public void run(){
while(!isInterrupted()){
//get a task to run
Runnable task = null;
try{
task = getTask();
}catch(InterruptedException ex){}
//if getTask() returned null or was interrupted, close this thread
//by returning.
if(task == null)
return;
//run the task, and eat anay exceptions it throws
try{
task.run();
}catch(Throwable t){
uncaughtException(this, t);
}
}
}
}
}
Code:
public class ThreadPoolTest {
public static void main(String[] args) {
if(args.length != 2){
System.out.println("Test the Thread Pool task.");
System.out.println("Usage: java ThreadPoolTest numTask numThreads");
return ;
}
int numTasks = Integer.parseInt(args[0]); // Wert 8 wird übergeben
int numThreads = Integer.parseInt(args[1]); // Wert 4 wird übergeben
//create the thread pool
ThreadPool threadPool = new ThreadPool(numThreads);
//run example Task
for( int i = 0; i < numTasks; i++)
threadPool.runTask(createTask(i));
//close the pool und wait for all tasks to finish.
threadPool.join();
}
/*Creates a simple Runnable that prints an ID, waits 500 milliseconds,
then prints the ID again. */
private static Runnable createTask(final int taskID){
return new Runnable(){
public void run(){
System.out.println("Task "+ taskID + ": start");
//simulate a long-running task
try{
Thread.sleep(500);
}catch(InterruptedException ex){}
System.out.println("Task "+ taskID + ": end");
}
};
}
}
Nun verstehe ich nicht, wo der Code-Abschnitt ist, der sagt, dass immer nur 4 Tasks gleichzeitig laufen können und falls einer fertig ist, gleich der nächste Task ausgeführt wird.
Liebe Grüße
Real