Implementation of thread Pool in java

In my previous post of thread pool (https://www.techartifact.com/blogs/2009/06/what-is-thread-pool-in-java.html),I have given the theoritcal knowledge.now I came with code.Hope it will help a lot.
Java’s implementation of thread pool is based on an executor. Executor is a generic concept modeled by this interface.

Package java.util.concurrent;
Public interface Executor {
Public void execute(Runnable task);
}

You simple have to create the task and pass it on execute() method of an appropriate executor.The pool is an instance of ThreadPoolExecutor class.this class implements ExecutorService Interface which tell how to put task and how to shut down.
Example of creation of thread pool

package java.util.concurrent;
public class ThreadPoolExecutor implements ExecutorService {
public ThreadPoolExecutor(int CorePoolSize,int maximumPoolSize,
                           long keepAliveTime,timeUnit unit,
                          BlockingQueue workQueue);
public ThreadPoolExecutor(int CorePoolSize,int maximumPoolSize,
                           long keepAliveTime,timeUnit unit,
                          BlockingQueue workQueue,
                           ThreadFactory threadfactory);
public ThreadPoolExecutor(int CorePoolSize,int maximumPoolSize,
                           long keepAliveTime,timeUnit unit,
                          BlockingQueue workQueue
                          RejectedExecutionHandler handler);
public ThreadPoolExecutor(int CorePoolSize,int maximumPoolSize,
                           long keepAliveTime,timeUnit unit,
                          BlockingQueue workQueue
                          ThreadFactory threadfactory
                           RejectedExecutionHandler handler);

CorePoolSize , keepAliveTime, are the part by which we can manage the thread pool.we can use a constructor to create the task and put them in thread pool

Now we will create the task and we will put into the threadpool.here it’s the example.

import java.util.concurrent.*;
 public class ThreadPoolTest {
public static void main(String[] args) {
int nTask =Integer.parseInt(args[0]);
long n = ling.parseLong(args[1]);
int tpSize =Integer.parseInt(args[2]);

ThreadPoolExecutor tpe = new ThreadPoolExecutor(tpSize,tpSize,50000L,
                             TimeUnit.MILLISECONDS,new LinkedBlockingQueue());
Task[] tasks = new Task[nTask];
for(int i =0;itasks[i]= new Task(n,"Task " + i);
tpe.execute(task[i]);
}
tpe.shutdown();
}
}

This is implementation of thread Pool.It is easy to use.Any suggestion would be welcome.

Pin it

Thread Pool in java

Threads are a very important Part of Java, but creating large numbers of threads can degrade the performance of application.By thread pools, which allow you to maintain fixed number of threads running assigned tasks to each of the threads.
Suppose A thing may have to visit tens of thousands of pages, and you will not create tens of thousands of thread because it is an additional problem. For overcome this problem we need a thread pool. Thread pool give some fixed number of thread and these thread will do task. Once a thread finish one task, then it is assigned to new task. We will not create a new thread always. This is way thread pool will work. Developer has to implement this feature.
In Java:
• A thread pool implementation is provided in the form of the ThreadPoolExecutor class, introduced in Java 1.5;
• you can put in different implementations of BlockingQueue to specify different queue behavior such as queue bounds or priority ordering

Implementation of Thread pool

This can be done by like this

 
   public class ThreadPool extends java.lang.Object implements ThreadPoolInt 

This class is generic implementation of thread pool which have following input
1) Size of the pool to be constructed
2) Name of the class which implements Runnable (which has a visible default constructor)

It constructs a thread pool with active threads that are waiting for activation. Once the threads have finished their task they will come back and wait for assigning a new task.

Benefits of Thread Pooling
• It saves the machine work of creating a new thread.
• Single thread can recycle again and again.
• The size of thread pool is given in starting .We can expand on the designs presented in this chapter to include a method to support growing the size of the pool at runtime if you need this kind of dynamic tuning.
• Response time can be quick.

Risks of using thread pools
The disadvantage of thread pool is that. Suppose if an task is rejected because the thread pool is empty. There will be high rejection rate.Or if task on wait state then waiting time would be too long. Sometime there is problem of deadlock also.

References
http://www.japisoft.com/jservices/javadoc/jason/service/pool/ThreadPool.html
http://java.sun.com/developer/Books/javaprogramming/threads/chap13.pdf

Jetspeed 2.2.0 release

Jetspeed 2.2.0

The Jetspeed 2.2.0 release is a major release of the Jetspeed-2 portal and is the first release of Jetspeed that is fully compliant with the Java Portlet Specification 2.0. With this new version of Jetspeed.

Jetspeed 2

See Latest jetspeed in action demo
Use the username admin connects using the password j2

New Features in Version 2.2.0

  • Portlet API 2.0 Support and Compliance
  • Passes Java Portlet 2.0 Test Compatibility Kit test suite
  • Inter-Portlet Communication via Events
  • New High Speed Preferences Database Storage
  • New Extensible Security Model with LDAP Synchronization
  • Improved Documentation including 5 New Guides (Users Guide, Admin Guide, Developers Guide, Build Guide, Deploy Guide)
  • New Improved Administrative Portlets
  • New Skins
  • New Maven-2 Custom Build
  • Updated Tutorial for New Maven-2 Custom Build
  • Enterprise (EAR) Deployments for Websphere 6.1
  • Multi-facted Authentication with Captcha and Personal Questions
  • Integrated with latest Pluto 2.0.0
  • Improved Single Signon Architecture (SSO)

Apache Portals Applications

Portals Team also co-releasing a new sub-project at Apache Portals: Apache Portals Applications.

Portal Applications

It has few portlet applications, that can be used out-of box.

Gems

A collection portlets that are beyond categorization into applications and are completely portable to the Portlet API (1.0 or 2.0) .

Demo

A portlet application dedicated to demonstrating different portal technologies like Portals Bridges .

RSS

A portlet application dedicated to RSS-based portlet development.

Database Browser

A portlet application dedicated to the development and database portlet development including scrollable lists and data entry forms.

Web Content

A portlet application dedicated to the development of Web Content rewriting and IFrame based web content.

Logging

Utilities used to setup and deploy logging for portlet and simple web applications.