Last time we met said:
Yes, but, the JVM will take a lot of the memory and the CPU, that's in
addition to the disk IO every second or something?
No. If written in any reasonable manner, it should take ~1 MB of RAM
and have no noticeable CPU or disk hit; all you're doing is getting the
timestamp of 1 directory once a second. Nothing resource intensive about
that.
Let's test this...
To prove it to myself, I wrote the program below which does what the
user would like, except that it only lists the dir contents instead
of running a program on a file. When I run it on my machine, which
is slow by today's standards (400Mhz PII, 384 MB of RAM, 5400 rpm ide
hd), the program takes up 1.6 MB of RAM and the CPU for its process
never goes above 0.0. Even when I up the polling to 100 times a
second, the CPU goes to 0.19% every now and then, and then back to 0.0.
So, actually, Java seems like a good language for writing programs
like this. It took me ~2 minutes to code this up and, while the
RAM used is probably more than it would be in other languages,
1.6 MB is very reasonable.
---
import java.io.*;
public class TestPoller extends Thread {
public void run() {
File directory = new File("/home/drew/stuff/javatemp/testpoller");
long currentLastModified = 0;
while (true) {
if (directory.lastModified() > currentLastModified) {
currentLastModified = directory.lastModified();
String[] files = directory.list();
System.out.println("Directory contents changed. Now:");
for (int i=0; i < files.length; i++) {
System.out.println(" " + files
);
}
}
try {
this.sleep(1000);
}
catch (InterruptedException e) {
System.out.println("Interrupted!");
}
}
}
public static void main(String[] args) throws Exception {
TestPoller tp = new TestPoller();
tp.setPriority(Thread.MIN_PRIORITY);
tp.start();
}
}
---
--
--------------------------------------------------------------------------
The geographical center of Boston is in Roxbury. Due north of the
center we find the South End. This is not to be confused with South
Boston which lies directly east from the South End. North of the South
End is East Boston and southwest of East Boston is the North End.
Drew Volpe, mylastname at hcs o harvard o edu