Java/__Base

ThreadpoolExecutor

javamix 2008. 6. 2. 08:08
ThreadpoolExecutor

출처: http://programmingexamples.wikidot.com/threadpoolexecutor

public class ThreadPoolExecutor extends AbstractExecutorService

An ExecutorService that executes each submitted task using one of possibly several pooled threads, normally configured using Executors factory methods.
Thread pools address two different problems: they usually provide improved performance when executing large numbers of asynchronous tasks, due to reduced per-task invocation overhead, and they provide a means of bounding and managing the resources, including threads, consumed when executing a collection of tasks. Each ThreadPoolExecutor also maintains some basic statistics, such as the number of completed tasks.[1]

import java.util.concurrent.*;
import java.util.*;

class MyThreadPoolExecutor
{
int poolSize = 2;

int maxPoolSize = 2;

long keepAliveTime = 10;

ThreadPoolExecutor threadPool = null;

final ArrayBlockingQueue<Runnable> queue = new ArrayBlockingQueue<Runnable>(
5);

public MyThreadPoolExecutor()
{
threadPool = new ThreadPoolExecutor(poolSize, maxPoolSize,
keepAliveTime, TimeUnit.SECONDS, queue);

}

public void runTask(Runnable task)
{
// System.out.println("Task count.."+threadPool.getTaskCount() );
// System.out.println("Queue Size before assigning the
// task.."+queue.size() );
threadPool.execute(task);
// System.out.println("Queue Size after assigning the
// task.."+queue.size() );
// System.out.println("Pool Size after assigning the
// task.."+threadPool.getActiveCount() );
// System.out.println("Task count.."+threadPool.getTaskCount() );
System.out.println("Task count.." + queue.size());

}

public void shutDown()
{
threadPool.shutdown();
}

public static void main(String args[])
{
MyThreadPoolExecutor mtpe = new MyThreadPoolExecutor();
// start first one
mtpe.runTask(new Runnable()
{
public void run()
{
for (int i = 0; i < 10; i++)
{
try
{
System.out.println("First Task");
Thread.currentThread().sleep(1000);
} catch (InterruptedException ie)
{
}
}
}
});
// start second one
/*
* try{ Thread.currentThread().sleep(500); }catch(InterruptedException
* ie){}
*/

mtpe.runTask(new Runnable()
{
public void run()
{
for (int i = 0; i < 10; i++)
{
try
{
System.out.println("Second Task");
Thread.currentThread().sleep(1000);
} catch (InterruptedException ie)
{
}
}
}
});
// start third one
/*
* try{ Thread.currentThread().sleep(500); }catch(InterruptedException
* ie){}
*/

mtpe.runTask(new Runnable()
{
public void run()
{
for (int i = 0; i < 10; i++)
{
try
{
System.out.println("Third Task");
Thread.currentThread().sleep(1000);
} catch (InterruptedException ie)
{
}
}
}
});
// start third one
/*
* try{ Thread.currentThread().sleep(500); }catch(InterruptedException
* ie){}
*/

mtpe.runTask(new Runnable()
{
public void run()
{
for (int i = 0; i < 10; i++)
{
try
{
System.out.println("Third Task");
Thread.currentThread().sleep(1000);
} catch (InterruptedException ie)
{
}
}
}
});
// start fourth one
/*
* try{ Thread.currentThread().sleep(500); }catch(InterruptedException
* ie){}
*/

mtpe.runTask(new Runnable()
{
public void run()
{
for (int i = 0; i < 10; i++)
{
try
{
System.out.println("Fourth Task");
Thread.currentThread().sleep(1000);
} catch (InterruptedException ie)
{
}
}
}
});
// start fifth one
/*
* try{ Thread.currentThread().sleep(500); }catch(InterruptedException
* ie){}
*/

mtpe.runTask(new Runnable()
{
public void run()
{
for (int i = 0; i < 10; i++)
{
try
{
System.out.println("Fifth Task");
Thread.currentThread().sleep(1000);
} catch (InterruptedException ie)
{
}
}
}
});
// start Sixth one
/*
* try{ Thread.currentThread().sleep(500); }catch(InterruptedException
* ie){}
*/

mtpe.runTask(new Runnable()
{
public void run()
{
for (int i = 0; i < 10; i++)
{
try
{
System.out.println("Sixth Task");
Thread.currentThread().sleep(1000);
} catch (InterruptedException ie)
{
}
}
}
});
mtpe.shutDown();
}

}