Java effective implementation of Thread

I have a file upload functionality, in which user can upload mutiple file at a same time, for good result I used Thread processing like this

Thread.start{
  // do file processing 'cause it is a long running process
}

Now the problem is, for each file upload system will create a new Thread so that will lead to system thrashing and someother issues, so now I am looking for a solution where I can create a Queue to store all the received files and create minimum number(say 5 nos) of Thread at a time and process it and again create a set of Thread and process.

So for I am looking into GPars, Java Thread and Queue and soon but no idea which is efficient way and what is the existing good solution

Upvotes: 0

Views: 109

Answers (1)

Seelenvirtuose
Seelenvirtuose

Reputation: 20628

You are looking for a thread pool or - in Java terms - for an Executor:

Executor executor = anExecutor();
executor.execute(aRunnable());

The method anExecutor should return a new Executor instance:

Executor anExecutor() {
    return Executors.newFixedThreadPool(42); // just an example ...
}

Upvotes: 2

Related Questions