Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Java 5.0: How does cache pool thread reuse threads?  RSS feed

 
Liang Anmian
Ranch Hand
Posts: 119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

The Java 5.0 API now has a new concurrency package. In the Executors class, there is a static method named newCacheThreadPool(). According to the API documentation, threads in this pool will be reused as much as possible.

However, there is something that I don't understand. How can it be possible to reuse a thread? After all, we know that threads can only be started once and only once. At first, I thought that the developers developed a special subclass of Thread which has a run() method that looks something like this:



I feel that this is the only way that a thread can be reused. Basically, the idea is that in the run() method, it enters some sort of an infinite loop, waiting for tasks to come in. Once it services a request, it waits for another task.

However, I was quickly proven wrong by myself when I realized that I can implement my own ThreadFactory. The Executors.newCachedThreadPool() method is overloaded with a second version that takes in a ThreadFactory object. Due to this reason, my assumption above is definitely wrong.

So does anyone know how the reusing is achieved? I peeped at the source code, and to be honest, I cannot make head or tail what it is doing.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving to the Threads forum...
 
James Carman
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You don't have to subclass Thread to tell a thread what to do. You can (and must) hand the ThreadFactory a Runnable object which tells the thread what to do. So, they hand it a custom Runnable object which does something similar to what you're talking about.
 
Henry Wong
author
Sheriff
Posts: 22830
119
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Liang Anmian:

I feel that this is the only way that a thread can be reused. Basically, the idea is that in the run() method, it enters some sort of an infinite loop, waiting for tasks to come in. Once it services a request, it waits for another task.

However, I was quickly proven wrong by myself when I realized that I can implement my own ThreadFactory. The Executors.newCachedThreadPool() method is overloaded with a second version that takes in a ThreadFactory object. Due to this reason, my assumption above is definitely wrong.

So does anyone know how the reusing is achieved? I peeped at the source code, and to be honest, I cannot make head or tail what it is doing.


The thread factory that you provide to the executor isn't so that you can create threads with the tasks already defined in the run() method. Tasks should still be passed as runnable (or callable) objects to the scheduling methods.

The thread factory is so that you can control the priority, daemon status, thread group, etc. of the threads in the thread pool.

Henry
 
Liang Anmian
Ranch Hand
Posts: 119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good gracious me. I guess I didn't explore the source code carefully. I didn't realize that the Runnable object being passed into the newThread() method of the ThreadFactory interface is actually not the task being passed in. Rather, it's actually a Worker object (which implements Runnable) that contains the task being passed into the Executor. Ok now I understand.

Thanks for the replies.
 
Iqbal Ashraf
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Liang,

I was searching with the same curiosity which you had regarding the reusability of threads in threadpool . But still I am not clear about it. Is it that these worker threads are daemon threads ? And hence resued ? It would be nice if you can give a detailed explanation.

Thanks & Regards,
Iqbal Ashraf
 
Henry Wong
author
Sheriff
Posts: 22830
119
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Iqbal Ashraf wrote:Hi Liang,

I was searching with the same curiosity which you had regarding the reusability of threads in threadpool . But still I am not clear about it. Is it that these worker threads are daemon threads ? And hence resued ? It would be nice if you can give a detailed explanation.

Thanks & Regards,
Iqbal Ashraf


When bumping a 5 year old thread, to ask a user a specific question, it is generally a good idea to look at the post history for that user. If you had, you would have noticed that Liang has not posted anything since December 2005. It is highly unlikely that he will see your question.


Anyway, in Java, "Daemon" threads have nothing to do with reusability. So, the threadpool may be using daemon threads, but that doesn't have anything to do with how they are reused. How the threads are reused have been described in the first post of this topic.

Henry
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!