views:

34

answers:

1

I get an ever increasing(like 1,2,3,4,5,6,7...) task count when I print s.getTaskCount(). I don't understand why.

public class MyTask implements Runnable
{
    public void run()
    {
       System.out.println("whatever....");
    }
 }


ScheduledThreadPoolExecutor s = new ScheduledThreadPoolExecutor(3);
s.scheduleAtFixedRate(new MyTask(), 0, 10, TimeUnit.SECONDS);


while(1>0)
{
    try
    {
        System.out.println("TASK COUNT: "+s.getTaskCount());
        Thread.sleep(60000);
    }
    catch(InterruptedException e)
    {
        System.out.println(e);
    }
}
+1  A: 

ScheduledThreadPoolExecutor.getTaskCount() states in the documentation:

Returns the approximate total number of tasks that have been scheduled for execution. Because the states of tasks and threads may change dynamically during computation, the returned value is only an approximation, but one that does not ever decrease across successive calls

So, getTaskCount() returns the total number of tasks that have been executed by the pool.

I'm not sure what your goal is, but if you want the number of tasks currently being executed, try getActiveCount().

James Van Huis
Returns the approximate total number of tasks that have been scheduled for execution. <- Should that return 1 or may be 2 but never 12 or 25 if i wait long enough?
Krolique
I'm trying to get an active count of all the tasks I've scheduled thought this executor or rather see how many tasks what are still being executed.
Krolique
No, it will continue to rise over time. It is the total number of tasks that have ever entered the queue. getActiveCount() will return the number that are currently executing.
James Van Huis
You can also directly monitor the task queue using getQueue()
James Van Huis
Well I'm doing something like this:
Krolique
private HashMap 10, TimeUnit.MINUTES));
Krolique
errr!sorry....
Krolique
What I am doing is keeping a map of all the ScheduledFuture<?> and removing things from that map and printing the size. However, you'd think that I should be able to get this from the actual execute itself
Krolique
Actually the getActiveCount() prints 0 most of the time as the task is non-trivial and will end execution way before I get to print AciveCount(). So its kind of useless for my task =)
Krolique
Perhaps you should start a new question with more details about what you are specifically trying to accomplish.
James Van Huis
perhaps, ty for you help
Krolique