androidmanifest
androidmanifest

Reputation: 289

ScheduledExecutorService running in main thread

I have 2 ScheduledExecutorService like this:

private ScheduledExecutorService writeToLog = Executors.newSingleThreadScheduledExecutor();

writeToLog.scheduleAtFixedRate(
                new Runnable() {
                    @Override
                    public void run() {
                        writeToLog();
                    }
                },
                0, 1, TimeUnit.SECONDS
        );

but after run this executor when I check how many thread is running:

 int nbRunning = 0;
        for (Thread t : Thread.getAllStackTraces().keySet()) {
            if (t.getState()==Thread.State.RUNNABLE) nbRunning++;
        }

I still have 7 thread, before and after this method this same number of running thread. What is wrong here ? I should have 9 thread

Upvotes: 1

Views: 782

Answers (1)

Dinesh
Dinesh

Reputation: 1086

The reason behind this the thread calling the writeToLog is not in runnable state. After execution of write log, the scheduled thread will enter in the Timed_WAITING state. But you have put a check on runnable state. That is the reason you are not getting the count increment.

Upvotes: 1

Related Questions