I got very confused by reading Thread Pooling. I learnt the concept, how they actually works. But I confused in the part , how to code this.
I searched a lot on the net. Finally I got a blog, that have codes , given below,
CONDITION IS, NOT TO USE IN-BUILT CLASS
Code 1
public class ThreadPool {
private BlockingQueue taskQueue = null;
private List<PoolThread> threads = new ArrayList<PoolThread>();
private boolean isStopped = false;
public ThreadPool(int noOfThreads, int maxNoOfTasks){
taskQueue = new BlockingQueue(maxNoOfTasks);
for(int i=0; i<noOfThreads; i++){
threads.add(new PoolThread(taskQueue));
}
for(PoolThread thread : threads){
thread.start();
}
}
public void synchronized execute(Runnable task){
if(this.isStopped) throw
new IllegalStateException("ThreadPool is stopped");
this.taskQueue.enqueue(task);
}
public synchronized void stop(){
this.isStopped = true;
for(PoolThread thread : threads){
thread.stop();
}
}
}
Code 2
public class PoolThread extends Thread {
private BlockingQueue taskQueue = null;
private boolean isStopped = false;
public PoolThread(BlockingQueue queue){
taskQueue = queue;
}
public void run(){
while(!isStopped()){
try{
Runnable runnable = (Runnable) taskQueue.dequeue();
runnable.run();
} catch(Exception e){
//log or otherwise report exception,
//but keep pool thread alive.
}
}
}
public synchronized void stop(){
isStopped = true;
this.interrupt(); //break pool thread out of dequeue() call.
}
public synchronized void isStopped(){
return isStopped;
}
}
Code 3:-
public class BlockingQueue {
private List queue = new LinkedList();
private int limit = 10;
public BlockingQueue(int limit){
this.limit = limit;
}
public synchronized void enqueue(Object item)
throws InterruptedException {
while(this.queue.size() == this.limit) {
wait();
}
if(this.queue.size() == 0) {
notifyAll();
}
this.queue.add(item);
}
public synchronized Object dequeue()
throws InterruptedException{
while(this.queue.size() == 0){
wait();
}
if(this.queue.size() == this.limit){
notifyAll();
}
return this.queue.remove(0);
}
}
I tried to understand , what this code do. But I dont get the flow of this code. Can you help me to understand this code.
Mainly I have problems in **Code 2 :- run method**
Why execute method's argument are of Runnable type?
How input array given to this code??
Help me.
Thanks in advance.
public void run(){
while(!isStopped()){
Loop until the thread pool is stopped.
try{
Runnable runnable = (Runnable) taskQueue.dequeue();
Pull the head task off the task queue.
runnable.run();
Run the task.
} catch(Exception e){
//log or otherwise report exception,
//but keep pool thread alive.
Do nothing special if the task throws an exception, just don't pass it on.
}
}
}
Edit:
I now understand that this is a class project but I'll leave my answer for posterity.
If you are trying to use thread-pools under Java then all of this has been already implemented for you by the java.util.concurrent.*
classes. Other answers address and explain your specific code.
For example, this is what you need to setup a thread pool using the ExecutorService
code. Underneath the covers the ExecutorService
handles the threads and uses a LinkedBlockingQueue
. You define the MyJob
class which implements 'Runnable' and does the work that is run by the threads in the pool. It can be a short or a long running task depending on what you need.
// create a thread pool with 10 workers
ExecutorService threadPool = Executors.newFixedThreadPool(10);
// or you can create an open-ended thread pool
// ExecutorService threadPool = Executors.newCachedThreadPool();
// define your jobs somehow
for (MyJob job : jobsToDo) {
threadPool.submit(job);
}
// once we have submitted all jobs to the thread pool, it should be shutdown
threadPool.shutdown();
...
public class MyJob implements Runnable {
// you can construct your jobs and pass in context for them if necessary
public MyJob(String someContext) {
...
}
public void run() {
// process the job
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.