This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes How to understand - ScheduledThreadPoolExecutor ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How to understand - ScheduledThreadPoolExecutor ?" Watch "How to understand - ScheduledThreadPoolExecutor ?" New topic
Author

How to understand - ScheduledThreadPoolExecutor ?

David S Hansen
Ranch Hand

Joined: Mar 07, 2013
Posts: 30
All the tutorials I found on google do not explain the basics or theory of ScheduledThreadPoolExecutor and related classes. They merely show some code with barely useful comments. Looks like no developer really needs this class.
The API docs are not helpful either. The class confuses me. It is not intuitive like Timer and TimerTask where one simply creates a task and a time to do that task.

Can someone tell me if any good books, articles and examples are out there for this API ?

This is what i read (and did not like) so far -

http://tutorials.jenkov.com/java-util-concurrent/scheduledexecutorservice.html

http://www.java2s.com/Tutorial/Java/0160__Thread/JDK15providesamechanismtocreateapoolascheduledtask.htm

http://dumb-coder.blogspot.com/2012/10/timer-vs-scheduledthreadpoolexecutor.html

http://massapi.com/class/sc/ScheduledThreadPoolExecutor.html



Jayesh A Lalwani
Bartender

Joined: Jan 17, 2008
Posts: 2067
    
  22

The API doc seems straightforward to me. What are you having trouble with?
David S Hansen
Ranch Hand

Joined: Mar 07, 2013
Posts: 30
Jayesh A Lalwani wrote:The API doc seems straightforward to me. What are you having trouble with?


You are a pro. I am not. Basic questions like -

ScheduledThreadPoolExecutor - What is the ThreadPool ? Why have a ThreadPool ? What does the Executor part mean ?
Those are just some of my questions. I'd like to understand things fully before I use them.


Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18141
    
  39

David S Hansen wrote:
Jayesh A Lalwani wrote:The API doc seems straightforward to me. What are you having trouble with?


You are a pro. I am not. Basic questions like -

ScheduledThreadPoolExecutor - What is the ThreadPool ? Why have a ThreadPool ? What does the Executor part mean ?
Those are just some of my questions. I'd like to understand things fully before I use them.


A Timer, depending on which one you mean, either starts one thread (java.util.Timer) or uses the GUI thread (javax.swing.Timer). The executor starts a pool of threads -- that you can control. For example, if you know that you have twenty five tasks, and you know that you have resources to allow five to run at the same time, perhaps you can configure it to run with a pool of five threads.

Executor just means something that executes tasks.

Henry

Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Jayesh A Lalwani
Bartender

Joined: Jan 17, 2008
Posts: 2067
    
  22

Ahh so you need to understand what a ThreadPoolExecutor is! .

Story time!! I have a backyard. A very nice backyard it is. I have squirrels and birds. My wife wanted to look at the pretty birds. So, she put up a bird feeder. Unfortunately, the bird feeder was too close to the house. As soon as she filled the feeder with seed, 2 squirrels would come up. One of them will take a mouthful of seeds, start eating it. While one is eating the seeds, the other will take a mouthful, and start eating it. As soon as one of them finished a mouthful, it would take another mouthful, go back to eat it. Back-forth..back-forth till the feeder was empty

So, she put the feeder up away from the house so the squirrels couldn;t get to it. The birds started coming in now. However, the birds don;t come as soon as she puts up the seeds. They only come in the mornings. The work the same way. One of them hops on, takes a mouthful, flits away. Another one hops on, takes a mouthful, flits away. On and on they go until the seed is over. The only differrence between the birds and the squirrels (aside from the birds looking pretty) is that the birds only come during certain times of the day

Now back to java.
A pool of threads is simply a collection of threads that can do some work. The ThreadPoolExecutor is a class provided by java that encapsulates a pool of threads. You can create a task, submit it to the queue of tasks. The threads in the pool will take a task from the head of the queue and execute it. A task will always go to one thread. WHen a thread is done, it goes back to the queue and picks the next task. On and on the threads go until you run out of tasks. THen they will sit idle and wait for more tasks to be submitted. The ThreadPoolExecutor is like the squirrels. Take a task. Eat it up. Back forth. Back forth.. till all the tasks are gone. Not so good for my backyard, but quite handy when you want to get a lot of work done using limited number of workers.

This is a classic Publisher-Consumer pattern. You might want to read up Publisher-Consumer. You might want to read the ThreadPoolExecutor API docs to get a more detailed understanding

A ScheduledThreadPoolExecutor is like the birds. It's just like the ThreadPoolExecutor. The only differrence is instead of a task being picked as soon as it's added to the queue, it's picked only when it's scheduled to be picked up. It's still a Publisher-Consumer pattern, except that things happen only at scheduled times.



David S Hansen
Ranch Hand

Joined: Mar 07, 2013
Posts: 30
Jayesh A Lalwani wrote:Ahh so you need to understand what a ThreadPoolExecutor is! .

Story time!! I have a backyard. A very nice backyard it is.---------------- at scheduled times.



That is the kind of explanation I was looking for. It all makes sense now.
Thank you very much indeed.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18141
    
  39

Jayesh A Lalwani wrote:
So, she put the feeder up away from the house so the squirrels couldn;t get to it. The birds started coming in now. However, the birds don;t come as soon as she puts up the seeds. They only come in the mornings. The work the same way. One of them hops on, takes a mouthful, flits away. Another one hops on, takes a mouthful, flits away. On and on they go until the seed is over. The only differrence between the birds and the squirrels (aside from the birds looking pretty) is that the birds only come during certain times of the day


Great story... and not adding anything related to the topic here... but I thought I should mention...


Certain parts have hawks. You have to be careful to not move the feeder too far away from the house (or any structure that can be used to hide). Putting a bird feeder out in the open can cause two types of feeding to take place -- feeding seeds to the song birds and feeding song birds to the hawks... Interesting tidbit that I learned from my brother-in-law (who had this problem).

Henry
Jayesh A Lalwani
Bartender

Joined: Jan 17, 2008
Posts: 2067
    
  22

Hmm.. I did hear a hawk last fall. It would be cool to see the hawk land on the deck. Right now the feeder is on the deck a little from the wall of the house.

Soo.. continuing the analogy.. Hawk = Garbage collector?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to understand - ScheduledThreadPoolExecutor ?
 
Similar Threads
Sending SMS after 5 minuts intervals...
execution of application at a specific time
Is there scheduled threads available in JEE 5?
Reding File from a remote server after every 2 minutes
How to check if a timer is still running or not ???