W
wanwan
I have a thread that records mouse positions at 100Hz, but I can't get
the desired sampling rate. The simplified code is:
static int mousepos_x, mousepos_y; // updated by another class
int[][] samples = new int [15000][2];
int count = 0;
.....
public class SampleMouse implements Runnable {
.....
public void run () {
while (running) {
samples[count][0] = mousepos_x;
samples[count++][1] = mousepos_y;
Thread.sleep(10);
}
}
}
I also tried in the run method with another algorithm:
long starttime; // (class scope variable) in ns
while (running) {
if (System.nanoTime() > starttime + count*10*1000000) {
samples[count][0] = mousepos_x;
samples[count++][1] = mousepos_y;
}
}
I also tried setting the thread priority to max.
With any of the approaches, I get a discrepancy of over 20%
Does anyone have a good way to get the desired sampling rate?
the desired sampling rate. The simplified code is:
static int mousepos_x, mousepos_y; // updated by another class
int[][] samples = new int [15000][2];
int count = 0;
.....
public class SampleMouse implements Runnable {
.....
public void run () {
while (running) {
samples[count][0] = mousepos_x;
samples[count++][1] = mousepos_y;
Thread.sleep(10);
}
}
}
I also tried in the run method with another algorithm:
long starttime; // (class scope variable) in ns
while (running) {
if (System.nanoTime() > starttime + count*10*1000000) {
samples[count][0] = mousepos_x;
samples[count++][1] = mousepos_y;
}
}
I also tried setting the thread priority to max.
With any of the approaches, I get a discrepancy of over 20%
Does anyone have a good way to get the desired sampling rate?