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

Common terminology used in oracle ADF

Entity object – ADF entity object are business components that encapsulate the business model, including data, rules, and persistence behavior, for items that are used in your application. Entity object definitions map to single objects in the data source. In the vast majority of cases, these are tables, views, synonyms, or snapshots in a database. It can allow to do DML operation. Advanced programmers can base entity objects on objects from other data sources, such as spreadsheets, XML files, or flat text files.

ADF view object – are business components that collect data from the data source.It represent the single table..It can also be used To create LOV. View object must have process for retrieving data from the data source.Data source is database and process is Sql query. Oracle ADF Business Components can automatically use JDBC to pass this query to the database and receive the result. Like entity attributes, the values of view attributes can be read or changed using the methods getAttribute() and setAttribute() in the ViewRowImpl class or by using generated getters and setters in a custom view row class

View Link – is the relation between the view object on based of a some common view attribute of each view object. Individual instances of view objects can also be related by individual instances of view links, which create a master-detail relationship between the query result sets.

ADF Application Module – are business components that represent particular application tasks. The application module definition provides a data model for the task by aggregating
the view object and view link instances required for the task. It also provides services that help the client accomplish the task

You can use application module in two different ways:
• As a service object, in which case each instance of the MVC application has access to one
instance of the application module. These root-level application module instances control ADF
BC transaction objects, which in turn control the entity and view caches.
• As a reusable object for nesting, in which case you can create a data model and service methods
on it and then nest one of its instances in other application module definitions. Those application
module definitions can, in turn, access the nested module’s methods and data model. Nested
application modules share the root-level application module’s transaction.

SetActionListener – The setActionListener tag is a declarative way to allow an action source ( , , etc.) to set a value before navigation. It is perhaps most useful in conjunction with the “processScope” EL scope provided b ADF Faces, as it makes it possible to pass details from one page to another without writing any Java code. This tag can be used both with ADF Faces commands and JSF standard tags.
Exmaple of this can be as follows. Suppose we have a table “employee”.We want to fetch the salary of an employee of some particular row and want to send this salary in
Next page in process scope or request scope etc.So using this we can do this.
It have two attributes :
From – the source of the value; can be an EL expression or a constant value
To – the target for the value; must be an EL expression

 
        <af:setActionListener      from="#{row.salary}"
                                  to="#{processScope.salary1}"/>              

This setactionListener will pick value of salary of that row and store this value into salary1 variable.So anyone can use this salary
As processScope.salary1 . It is very simple to use. And very useful.

References – oracle ADF development guidelines (pdf)
– www.oracle.com

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