Javamex

Java discussion forum to accompany the Javamex web site.

I have this dispute with my co-workers, i just wanted to know your opinion, maybe it can also help other people as well.

I have scheduler - ScheduledThreadPoolExecuter , one core thread
It wakes up every second to do some work. The work is to go over some Tasks (lets say 50) , and check their state ( ready , not ready ) if ready it submit them to some other threadpools. The state is a AtomicBoolean, so the check is quite easy.
So they say it's a lot of work for the CPU and waking up every second consumes a lot of CPU time , "lets make it better wake up every 15 seconds or so".
The work the cpu does on these tasks every minute sounds to me like a raindrop in a sea and scheduling a my runnable every minute is an eternity for the CPU (for instance if the executor has only one task, even if it has more than one task scheduled

Thanks in advance
Roman

Views: 146

Reply to This

Replies to This Discussion

Hi Roman,

Sorry for not replying sooner -- been a bit chokka these last couple of days.

So I think you're essentialy right-- or at least, performing a short, cooperative task every second isn't so bad. The thing that can be bad is if you have a process that either takes a large chunk of CPU each time (obviously), or more subtly, one that hits a lot of memory, thus thrashing the CPU cache and flushing out memory that is being accessed by other threads/processes.

The only thing I would say is that if you really need the thread to know about state changes down to the second, it sounds like maybe you should be using some other construct to notify your thread-- e.g. making each task add a "status change" item to a blocking queue when it's done, and have your status-checking thread simply sit waiting on the queue. (See the web site section on BlockingQueue, for example.)

The other thing is that the CPU usage shouldn't be a mystery: you can just measure it. Also, if you're on Windows, look for the "Show Kernal Times" option in the Task Manager View menu. That gives you CPU usage in two colours: green for actual CPU burnt by your app, and red for CPU burnt by the OS-- in effect the CPU that you're "wasting" by doing extra context switches, lock management etc.

Hope that helps,

Neil

Reply to Discussion

RSS

© 2024   Created by Neil Coffey.   Powered by

Badges  |  Report an Issue  |  Terms of Service