0% found this document useful (0 votes)
80 views5 pages

Thread Pool Excecutors

The document discusses thread pool executors in Java. It defines a thread pool executor as an executor service that executes submitted tasks using pooled threads, configured using factory methods. ThreadPoolExecutor maintains basic statistics like completed tasks. Programmers can configure core and maximum pool sizes, keep alive time, and a work queue when constructing a ThreadPoolExecutor. The code sample shows creating a thread pool executor with a fixed core and max pool size of 2, keep alive time of 10 seconds, and a bounded work queue. It submits 6 runnable tasks to the pool and shuts it down after task completion.

Uploaded by

Abdul Gafur
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
80 views5 pages

Thread Pool Excecutors

The document discusses thread pool executors in Java. It defines a thread pool executor as an executor service that executes submitted tasks using pooled threads, configured using factory methods. ThreadPoolExecutor maintains basic statistics like completed tasks. Programmers can configure core and maximum pool sizes, keep alive time, and a work queue when constructing a ThreadPoolExecutor. The code sample shows creating a thread pool executor with a fixed core and max pool size of 2, keep alive time of 10 seconds, and a bounded work queue. It submits 6 runnable tasks to the pool and shuts it down after task completion.

Uploaded by

Abdul Gafur
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

1. ExecutorService executor = Executors.

newFixedThreadPool(5);
2. for (int i = 0; i < 10; i++) {
3. Runnable worker = new WorkerThread("" + i);
4. executor.execute(worker);
5. }
6. executor.shutdown();
7. while (!executor.isTerminated()) { }
8.
9. System.out.println("Finished all threads");
10. }




public class ThreadPoolExecutor extends AbstractExecutorService
An ExecutorService that executes each submitted task using one of possibly several
pooled threads, normally configured using Executorsfactory 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]
Programmers can configure the creation for a thread pool using ThreadPoolExecutor
constructor. The following parameters can be configure:
corePoolSize: This value (core pool size) tells how many threads will be
created before implementation (execution policy) starts looking for existing
free thread.
maximumPoolSize: The maximum pool size is the upper bound on how
many pool threads can be active at once.
keepAliveTime: If the pool has more than corePoolSize threads and there
are more tasks to execute then implementation (execution policy) will
terminate all excess threads (maximumPoolSize coreThreadSize) which are
idle for more than keepAliveTime.
workQueue: This is a BlockingQueue used for holding tasks awaiting
execution. There is direct relation between work queue and pool sizing.


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.sleep(1000);
} catch (InterruptedException ie)
{
}
}
}
});
// start second one
/*
* try{ Thread.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.sleep(1000);
} catch (InterruptedException ie)
{
}
}
}
});
// start third one
/*
* try{ Thread.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.sleep(1000);
} catch (InterruptedException ie)
{
}
}
}
});
// start fourth one
/*
* try{ Thread.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.sleep(1000);
} catch (InterruptedException ie)
{
}
}
}
});
// start fifth one
/*
* try{ Thread.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.sleep(1000);
} catch (InterruptedException ie)
{
}
}
}
});
// start Sixth one
/*
* try{ Thread.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.sleep(1000);
} catch (InterruptedException ie)
{
}
}
}
});
mtpe.shutDown();
}

}


http://livetvchannelsfree.com/maatv.html

You might also like