Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java ScheduledExecutorService BAD Precision

Hi there i've written a simple program to test the precision of the ScduledExecutorService.schedule() function.

The test sets a delay and check the effective distance between the required delay and the effective result.

The test have been performed on a i7 machine running Linux 3.8 x86_64, with both OpenJDK 1.7 and Oracle JDK 1.7

The result of the test is really bad, here there is a list to show you the average delta between presumed and effective delay:


Legend:

  • Sleep(ms): the delay required in milliseconds
  • deltaAVG(ms): the average difference between the required and effective delay obtained in milliseconds
  • deltaAVG_PERC: the error percentage required/effective
  • delta MIN/MAX: the minimum / maximum difference between required and effective delay obtained

Sleep(ms): 0.010 deltaAVG(ms): 0.029 deltaAVG_PERC: 0289.6 % delta MIN/MAX (ms): 0.029/0.029
Sleep(ms): 0.020 deltaAVG(ms): 0.061 deltaAVG_PERC: 0299.3 % delta MIN/MAX (ms): 0.006/4.578
Sleep(ms): 0.030 deltaAVG(ms): 0.066 deltaAVG_PERC: 0221.1 % delta MIN/MAX (ms): 0.009/1.639
Sleep(ms): 0.040 deltaAVG(ms): 0.066 deltaAVG_PERC: 0165.3 % delta MIN/MAX (ms): 0.010/0.445
Sleep(ms): 0.050 deltaAVG(ms): 0.069 deltaAVG_PERC: 0138.0 % delta MIN/MAX (ms): 0.006/0.370
Sleep(ms): 0.060 deltaAVG(ms): 0.067 deltaAVG_PERC: 0111.8 % delta MIN/MAX (ms): 0.052/0.700
Sleep(ms): 0.070 deltaAVG(ms): 0.067 deltaAVG_PERC: 0096.0 % delta MIN/MAX (ms): 0.053/5.597
Sleep(ms): 0.080 deltaAVG(ms): 0.067 deltaAVG_PERC: 0083.6 % delta MIN/MAX (ms): 0.054/1.978
Sleep(ms): 0.090 deltaAVG(ms): 0.065 deltaAVG_PERC: 0072.8 % delta MIN/MAX (ms): 0.034/1.548
Sleep(ms): 0.100 deltaAVG(ms): 0.066 deltaAVG_PERC: 0066.3 % delta MIN/MAX (ms): 0.026/1.969
Sleep(ms): 0.110 deltaAVG(ms): 0.065 deltaAVG_PERC: 0058.7 % delta MIN/MAX (ms): 0.036/1.075
Sleep(ms): 0.120 deltaAVG(ms): 0.064 deltaAVG_PERC: 0053.5 % delta MIN/MAX (ms): 0.012/0.454
Sleep(ms): 0.130 deltaAVG(ms): 0.067 deltaAVG_PERC: 0051.6 % delta MIN/MAX (ms): 0.038/1.523
Sleep(ms): 0.140 deltaAVG(ms): 0.066 deltaAVG_PERC: 0047.0 % delta MIN/MAX (ms): 0.038/0.544
Sleep(ms): 0.150 deltaAVG(ms): 0.066 deltaAVG_PERC: 0044.0 % delta MIN/MAX (ms): 0.031/0.498
Sleep(ms): 0.160 deltaAVG(ms): 0.065 deltaAVG_PERC: 0040.4 % delta MIN/MAX (ms): 0.022/0.432
Sleep(ms): 0.170 deltaAVG(ms): 0.067 deltaAVG_PERC: 0039.6 % delta MIN/MAX (ms): 0.031/0.229
Sleep(ms): 0.180 deltaAVG(ms): 0.071 deltaAVG_PERC: 0039.3 % delta MIN/MAX (ms): 0.046/0.291
Sleep(ms): 0.190 deltaAVG(ms): 0.074 deltaAVG_PERC: 0039.1 % delta MIN/MAX (ms): 0.042/1.069
Sleep(ms): 0.200 deltaAVG(ms): 0.071 deltaAVG_PERC: 0035.5 % delta MIN/MAX (ms): 0.031/0.293
Sleep(ms): 0.210 deltaAVG(ms): 0.072 deltaAVG_PERC: 0034.3 % delta MIN/MAX (ms): 0.028/1.058
Sleep(ms): 0.220 deltaAVG(ms): 0.075 deltaAVG_PERC: 0034.0 % delta MIN/MAX (ms): 0.055/1.879
Sleep(ms): 0.230 deltaAVG(ms): 0.075 deltaAVG_PERC: 0032.5 % delta MIN/MAX (ms): 0.040/0.514
Sleep(ms): 0.240 deltaAVG(ms): 0.075 deltaAVG_PERC: 0031.4 % delta MIN/MAX (ms): 0.055/1.715
Sleep(ms): 0.250 deltaAVG(ms): 0.075 deltaAVG_PERC: 0030.2 % delta MIN/MAX (ms): 0.044/1.025
Sleep(ms): 0.260 deltaAVG(ms): 0.076 deltaAVG_PERC: 0029.2 % delta MIN/MAX (ms): 0.038/1.561
Sleep(ms): 0.270 deltaAVG(ms): 0.076 deltaAVG_PERC: 0028.1 % delta MIN/MAX (ms): 0.050/0.697
Sleep(ms): 0.280 deltaAVG(ms): 0.075 deltaAVG_PERC: 0026.8 % delta MIN/MAX (ms): 0.039/0.996
Sleep(ms): 0.290 deltaAVG(ms): 0.076 deltaAVG_PERC: 0026.3 % delta MIN/MAX (ms): 0.032/0.475
Sleep(ms): 0.300 deltaAVG(ms): 0.077 deltaAVG_PERC: 0025.6 % delta MIN/MAX (ms): 0.055/2.136
Sleep(ms): 0.310 deltaAVG(ms): 0.077 deltaAVG_PERC: 0024.9 % delta MIN/MAX (ms): 0.042/0.373
Sleep(ms): 0.320 deltaAVG(ms): 0.079 deltaAVG_PERC: 0024.6 % delta MIN/MAX (ms): 0.036/2.622
Sleep(ms): 0.330 deltaAVG(ms): 0.080 deltaAVG_PERC: 0024.3 % delta MIN/MAX (ms): 0.038/1.367
Sleep(ms): 0.340 deltaAVG(ms): 0.080 deltaAVG_PERC: 0023.5 % delta MIN/MAX (ms): 0.028/0.308
Sleep(ms): 0.350 deltaAVG(ms): 0.079 deltaAVG_PERC: 0022.7 % delta MIN/MAX (ms): 0.055/1.885
Sleep(ms): 0.360 deltaAVG(ms): 0.076 deltaAVG_PERC: 0021.1 % delta MIN/MAX (ms): 0.053/0.403
Sleep(ms): 0.370 deltaAVG(ms): 0.079 deltaAVG_PERC: 0021.3 % delta MIN/MAX (ms): 0.056/0.390
Sleep(ms): 0.380 deltaAVG(ms): 0.079 deltaAVG_PERC: 0020.9 % delta MIN/MAX (ms): 0.055/3.777
Sleep(ms): 0.390 deltaAVG(ms): 0.081 deltaAVG_PERC: 0020.9 % delta MIN/MAX (ms): 0.058/0.320
Sleep(ms): 0.400 deltaAVG(ms): 0.080 deltaAVG_PERC: 0019.9 % delta MIN/MAX (ms): 0.056/0.203
Sleep(ms): 0.410 deltaAVG(ms): 0.082 deltaAVG_PERC: 0019.9 % delta MIN/MAX (ms): 0.051/0.562
Sleep(ms): 0.420 deltaAVG(ms): 0.082 deltaAVG_PERC: 0019.6 % delta MIN/MAX (ms): 0.056/0.913
Sleep(ms): 0.430 deltaAVG(ms): 0.080 deltaAVG_PERC: 0018.6 % delta MIN/MAX (ms): 0.053/0.938
Sleep(ms): 0.440 deltaAVG(ms): 0.085 deltaAVG_PERC: 0019.4 % delta MIN/MAX (ms): 0.055/0.582
Sleep(ms): 0.450 deltaAVG(ms): 0.086 deltaAVG_PERC: 0019.1 % delta MIN/MAX (ms): 0.041/0.179
Sleep(ms): 0.460 deltaAVG(ms): 0.083 deltaAVG_PERC: 0018.0 % delta MIN/MAX (ms): 0.032/0.235
Sleep(ms): 0.470 deltaAVG(ms): 0.088 deltaAVG_PERC: 0018.6 % delta MIN/MAX (ms): 0.042/0.581
Sleep(ms): 0.480 deltaAVG(ms): 0.088 deltaAVG_PERC: 0018.3 % delta MIN/MAX (ms): 0.040/0.477
Sleep(ms): 0.490 deltaAVG(ms): 0.086 deltaAVG_PERC: 0017.5 % delta MIN/MAX (ms): 0.032/0.931
Sleep(ms): 0.500 deltaAVG(ms): 0.088 deltaAVG_PERC: 0017.5 % delta MIN/MAX (ms): 0.055/0.521
Sleep(ms): 0.510 deltaAVG(ms): 0.081 deltaAVG_PERC: 0016.0 % delta MIN/MAX (ms): 0.056/0.225
Sleep(ms): 0.520 deltaAVG(ms): 0.088 deltaAVG_PERC: 0016.9 % delta MIN/MAX (ms): 0.055/0.344
Sleep(ms): 0.530 deltaAVG(ms): 0.085 deltaAVG_PERC: 0016.0 % delta MIN/MAX (ms): 0.035/0.819
Sleep(ms): 0.540 deltaAVG(ms): 0.084 deltaAVG_PERC: 0015.6 % delta MIN/MAX (ms): 0.026/0.961
Sleep(ms): 0.550 deltaAVG(ms): 0.093 deltaAVG_PERC: 0016.9 % delta MIN/MAX (ms): 0.058/0.570
Sleep(ms): 0.560 deltaAVG(ms): 0.085 deltaAVG_PERC: 0015.3 % delta MIN/MAX (ms): 0.033/0.176
Sleep(ms): 0.570 deltaAVG(ms): 0.090 deltaAVG_PERC: 0015.8 % delta MIN/MAX (ms): 0.043/0.289
Sleep(ms): 0.580 deltaAVG(ms): 0.087 deltaAVG_PERC: 0014.9 % delta MIN/MAX (ms): 0.041/0.258
Sleep(ms): 0.590 deltaAVG(ms): 0.082 deltaAVG_PERC: 0013.9 % delta MIN/MAX (ms): 0.057/0.352
Sleep(ms): 0.600 deltaAVG(ms): 0.083 deltaAVG_PERC: 0013.9 % delta MIN/MAX (ms): 0.060/0.393
Sleep(ms): 0.610 deltaAVG(ms): 0.084 deltaAVG_PERC: 0013.8 % delta MIN/MAX (ms): 0.059/0.177
Sleep(ms): 0.620 deltaAVG(ms): 0.095 deltaAVG_PERC: 0015.3 % delta MIN/MAX (ms): 0.041/0.273
Sleep(ms): 0.630 deltaAVG(ms): 0.080 deltaAVG_PERC: 0012.6 % delta MIN/MAX (ms): 0.059/0.253
Sleep(ms): 0.640 deltaAVG(ms): 0.085 deltaAVG_PERC: 0013.3 % delta MIN/MAX (ms): 0.060/0.422
Sleep(ms): 0.650 deltaAVG(ms): 0.100 deltaAVG_PERC: 0015.4 % delta MIN/MAX (ms): 0.050/0.641
Sleep(ms): 0.660 deltaAVG(ms): 0.090 deltaAVG_PERC: 0013.7 % delta MIN/MAX (ms): 0.058/0.170
Sleep(ms): 0.670 deltaAVG(ms): 0.097 deltaAVG_PERC: 0014.5 % delta MIN/MAX (ms): 0.055/0.578
Sleep(ms): 0.680 deltaAVG(ms): 0.094 deltaAVG_PERC: 0013.8 % delta MIN/MAX (ms): 0.060/3.560
Sleep(ms): 0.690 deltaAVG(ms): 0.092 deltaAVG_PERC: 0013.3 % delta MIN/MAX (ms): 0.059/0.178
Sleep(ms): 0.700 deltaAVG(ms): 0.094 deltaAVG_PERC: 0013.4 % delta MIN/MAX (ms): 0.060/0.202
Sleep(ms): 0.710 deltaAVG(ms): 0.102 deltaAVG_PERC: 0014.3 % delta MIN/MAX (ms): 0.056/0.227
Sleep(ms): 0.720 deltaAVG(ms): 0.084 deltaAVG_PERC: 0011.7 % delta MIN/MAX (ms): 0.060/0.177
Sleep(ms): 0.730 deltaAVG(ms): 0.099 deltaAVG_PERC: 0013.5 % delta MIN/MAX (ms): 0.046/0.723
Sleep(ms): 0.740 deltaAVG(ms): 0.098 deltaAVG_PERC: 0013.2 % delta MIN/MAX (ms): 0.058/0.203
Sleep(ms): 0.750 deltaAVG(ms): 0.104 deltaAVG_PERC: 0013.9 % delta MIN/MAX (ms): 0.059/0.274
Sleep(ms): 0.760 deltaAVG(ms): 0.105 deltaAVG_PERC: 0013.8 % delta MIN/MAX (ms): 0.056/0.274
Sleep(ms): 0.770 deltaAVG(ms): 0.104 deltaAVG_PERC: 0013.5 % delta MIN/MAX (ms): 0.056/0.631
Sleep(ms): 0.780 deltaAVG(ms): 0.099 deltaAVG_PERC: 0012.7 % delta MIN/MAX (ms): 0.044/0.191
Sleep(ms): 0.790 deltaAVG(ms): 0.099 deltaAVG_PERC: 0012.5 % delta MIN/MAX (ms): 0.041/0.167
Sleep(ms): 0.800 deltaAVG(ms): 0.104 deltaAVG_PERC: 0013.0 % delta MIN/MAX (ms): 0.044/0.223
Sleep(ms): 0.810 deltaAVG(ms): 0.095 deltaAVG_PERC: 0011.7 % delta MIN/MAX (ms): 0.060/0.761
Sleep(ms): 0.820 deltaAVG(ms): 0.101 deltaAVG_PERC: 0012.3 % delta MIN/MAX (ms): 0.058/0.231
Sleep(ms): 0.830 deltaAVG(ms): 0.102 deltaAVG_PERC: 0012.3 % delta MIN/MAX (ms): 0.060/0.552
Sleep(ms): 0.840 deltaAVG(ms): 0.106 deltaAVG_PERC: 0012.6 % delta MIN/MAX (ms): 0.060/0.517
Sleep(ms): 0.850 deltaAVG(ms): 0.109 deltaAVG_PERC: 0012.9 % delta MIN/MAX (ms): 0.061/0.204
Sleep(ms): 0.860 deltaAVG(ms): 0.107 deltaAVG_PERC: 0012.5 % delta MIN/MAX (ms): 0.062/0.532
Sleep(ms): 0.870 deltaAVG(ms): 0.109 deltaAVG_PERC: 0012.5 % delta MIN/MAX (ms): 0.061/0.266
Sleep(ms): 0.880 deltaAVG(ms): 0.108 deltaAVG_PERC: 0012.3 % delta MIN/MAX (ms): 0.057/0.753
Sleep(ms): 0.890 deltaAVG(ms): 0.108 deltaAVG_PERC: 0012.2 % delta MIN/MAX (ms): 0.060/0.553
Sleep(ms): 0.900 deltaAVG(ms): 0.108 deltaAVG_PERC: 0011.9 % delta MIN/MAX (ms): 0.056/0.369
Sleep(ms): 0.910 deltaAVG(ms): 0.106 deltaAVG_PERC: 0011.6 % delta MIN/MAX (ms): 0.057/0.213
Sleep(ms): 0.920 deltaAVG(ms): 0.107 deltaAVG_PERC: 0011.6 % delta MIN/MAX (ms): 0.057/0.185
Sleep(ms): 0.930 deltaAVG(ms): 0.107 deltaAVG_PERC: 0011.5 % delta MIN/MAX (ms): 0.044/0.842
Sleep(ms): 0.940 deltaAVG(ms): 0.111 deltaAVG_PERC: 0011.8 % delta MIN/MAX (ms): 0.064/0.395
Sleep(ms): 0.950 deltaAVG(ms): 0.108 deltaAVG_PERC: 0011.4 % delta MIN/MAX (ms): 0.061/0.207
Sleep(ms): 0.960 deltaAVG(ms): 0.110 deltaAVG_PERC: 0011.5 % delta MIN/MAX (ms): 0.042/0.215
Sleep(ms): 0.970 deltaAVG(ms): 0.107 deltaAVG_PERC: 0011.0 % delta MIN/MAX (ms): 0.049/0.646
Sleep(ms): 0.980 deltaAVG(ms): 0.110 deltaAVG_PERC: 0011.2 % delta MIN/MAX (ms): 0.059/0.317
Sleep(ms): 0.990 deltaAVG(ms): 0.109 deltaAVG_PERC: 0011.0 % delta MIN/MAX (ms): 0.061/0.205
Sleep(ms): 1.000 deltaAVG(ms): 0.103 deltaAVG_PERC: 0010.3 % delta MIN/MAX (ms): 0.052/0.283
Sleep(ms): 1.010 deltaAVG(ms): 0.109 deltaAVG_PERC: 0010.8 % delta MIN/MAX (ms): 0.058/0.295
Sleep(ms): 1.020 deltaAVG(ms): 0.107 deltaAVG_PERC: 0010.5 % delta MIN/MAX (ms): 0.063/0.562
Sleep(ms): 1.030 deltaAVG(ms): 0.105 deltaAVG_PERC: 0010.2 % delta MIN/MAX (ms): 0.060/0.256
Sleep(ms): 1.040 deltaAVG(ms): 0.110 deltaAVG_PERC: 0010.6 % delta MIN/MAX (ms): 0.059/0.231
Sleep(ms): 1.050 deltaAVG(ms): 0.110 deltaAVG_PERC: 0010.5 % delta MIN/MAX (ms): 0.059/0.570
Sleep(ms): 1.060 deltaAVG(ms): 0.109 deltaAVG_PERC: 0010.2 % delta MIN/MAX (ms): 0.059/0.210
Sleep(ms): 1.070 deltaAVG(ms): 0.110 deltaAVG_PERC: 0010.3 % delta MIN/MAX (ms): 0.035/0.460
Sleep(ms): 1.080 deltaAVG(ms): 0.110 deltaAVG_PERC: 0010.2 % delta MIN/MAX (ms): 0.062/0.189
Sleep(ms): 1.090 deltaAVG(ms): 0.110 deltaAVG_PERC: 0010.1 % delta MIN/MAX (ms): 0.058/0.228
Sleep(ms): 1.100 deltaAVG(ms): 0.111 deltaAVG_PERC: 0010.0 % delta MIN/MAX (ms): 0.061/0.513
Sleep(ms): 1.110 deltaAVG(ms): 0.110 deltaAVG_PERC: 0009.9 % delta MIN/MAX (ms): 0.052/0.200
Sleep(ms): 1.120 deltaAVG(ms): 0.110 deltaAVG_PERC: 0009.9 % delta MIN/MAX (ms): 0.048/0.248
Sleep(ms): 1.130 deltaAVG(ms): 0.108 deltaAVG_PERC: 0009.6 % delta MIN/MAX (ms): 0.061/0.570
Sleep(ms): 1.140 deltaAVG(ms): 0.111 deltaAVG_PERC: 0009.7 % delta MIN/MAX (ms): 0.065/0.184
Sleep(ms): 1.150 deltaAVG(ms): 0.112 deltaAVG_PERC: 0009.7 % delta MIN/MAX (ms): 0.063/0.449
Sleep(ms): 1.160 deltaAVG(ms): 0.109 deltaAVG_PERC: 0009.4 % delta MIN/MAX (ms): 0.049/0.298
Sleep(ms): 1.170 deltaAVG(ms): 0.107 deltaAVG_PERC: 0009.1 % delta MIN/MAX (ms): 0.059/0.212
Sleep(ms): 1.180 deltaAVG(ms): 0.107 deltaAVG_PERC: 0009.1 % delta MIN/MAX (ms): 0.060/0.224
Sleep(ms): 1.190 deltaAVG(ms): 0.114 deltaAVG_PERC: 0009.6 % delta MIN/MAX (ms): 0.061/0.217
Sleep(ms): 1.200 deltaAVG(ms): 0.109 deltaAVG_PERC: 0009.1 % delta MIN/MAX (ms): 0.058/0.231
Sleep(ms): 1.210 deltaAVG(ms): 0.115 deltaAVG_PERC: 0009.5 % delta MIN/MAX (ms): 0.061/0.237
Sleep(ms): 1.220 deltaAVG(ms): 0.108 deltaAVG_PERC: 0008.8 % delta MIN/MAX (ms): 0.063/0.207
Sleep(ms): 1.230 deltaAVG(ms): 0.107 deltaAVG_PERC: 0008.7 % delta MIN/MAX (ms): 0.059/0.355
Sleep(ms): 1.240 deltaAVG(ms): 0.113 deltaAVG_PERC: 0009.1 % delta MIN/MAX (ms): 0.059/0.197
Sleep(ms): 1.250 deltaAVG(ms): 0.114 deltaAVG_PERC: 0009.1 % delta MIN/MAX (ms): 0.059/0.235
Sleep(ms): 1.260 deltaAVG(ms): 0.113 deltaAVG_PERC: 0009.0 % delta MIN/MAX (ms): 0.061/0.219
Sleep(ms): 1.270 deltaAVG(ms): 0.113 deltaAVG_PERC: 0008.9 % delta MIN/MAX (ms): 0.060/0.284
Sleep(ms): 1.280 deltaAVG(ms): 0.112 deltaAVG_PERC: 0008.8 % delta MIN/MAX (ms): 0.060/0.222
Sleep(ms): 1.290 deltaAVG(ms): 0.114 deltaAVG_PERC: 0008.9 % delta MIN/MAX (ms): 0.063/0.182
Sleep(ms): 1.300 deltaAVG(ms): 0.112 deltaAVG_PERC: 0008.6 % delta MIN/MAX (ms): 0.058/0.209

As you can see the average Error called deltaAVG is increasing relatively to the delay.

How is possible to get a better result in delay? I mean 300% error rate on 10 microsecond on i7 machine is too much.

here is the code i used for testing:

package threadexecutor_perftest;

import java.text.DecimalFormat;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;
import statistica.timedValuesAverage;

/**
 *
 * @author salvatore novelli salvatore.novelli  domain   gmail.com
 */
public class ThreadExecutor_PerfTest implements Runnable, ThreadFactory {

    private final ScheduledExecutorService executor;
    private long start;
    private long stop;
    private long delay_nano = 10000;
    private final int averageTimeLen_ms = 2000;
    private TimedValuesAverage<Double> deltaAVG = new TimedValuesAverage<>(averageTimeLen_ms);
    DecimalFormat int3 = new DecimalFormat("0.000");
    DecimalFormat int4 = new DecimalFormat("0000.0");

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        ThreadExecutor_PerfTest test = new ThreadExecutor_PerfTest();

        test.start();

    }

    public ThreadExecutor_PerfTest() {
        executor = Executors.newSingleThreadScheduledExecutor(this);

    }

    public boolean start() {
        executor.schedule(this, 0L, TimeUnit.NANOSECONDS);
        return true;
    }

    private long DBG_lastReport;

    @Override
    public void run() {

        stop = System.nanoTime();

        if (start > 0) {

            long deltaT = (stop - start) - delay_nano;
            deltaAVG.put((double) deltaT);

            //report status every averageTimeLen_ms
            if ((System.currentTimeMillis() - DBG_lastReport) > averageTimeLen_ms) {

                System.out.println("    Sleep(ms): " + int3.format(delay_nano / 1000000.0)
                        + " deltaAVG(ms): " + int3.format(deltaAVG.getAverage() / 1000000.0)
                        + " deltaAVG_PERC: " + int4.format((deltaAVG.getAverage() /     delay_nano) * 100)+" %"
                        + " delta MIN/MAX (ms): " +     int3.format(deltaAVG.getSmallestEver() /     1000000.0) + "/" + int3.format(deltaAVG.getGreatestEver() / 1000000.0));

                //increase delay by 10 micro seconds (1000 nano seconds)
                delay_nano += 10000;
                deltaAVG = new TimedValuesAverage<>(averageTimeLen_ms);
                DBG_lastReport = System.currentTimeMillis();
            }
        }

        start = System.nanoTime();
        executor.schedule(this, delay_nano, TimeUnit.NANOSECONDS);
    }

    @Override
    public Thread newThread(Runnable r) {
        Thread t = new Thread(r, "Exec-test");
        t.setPriority(Thread.MAX_PRIORITY);
        return t;
    }

}
like image 635
snovelli Avatar asked Nov 27 '13 11:11

snovelli


2 Answers

Your OS's scheduler quantum is probably no greater than 10ms anyway, so trying to rely on a user-space thread-based timer is going to be futile. See this answer which basically says the same.

You best shot in Java-land is java.util.concurrent.locks.LockSupport and the parkNanos() methods. But below 10ms you're still likely to be out of luck -- because of the OS not Java.

If you read nothing else, read this. This answer is correct.

like image 137
brettw Avatar answered Sep 19 '22 12:09

brettw


Executors.newSingleThreadScheduledExecutor(this); uses a ScheduledThreadPoolExecutor under the hood. In the JavaDocs for that class it states:

Delayed tasks execute no sooner than they are enabled, but without any real-time guarantees about when, after they are enabled, they will commence. Tasks scheduled for exactly the same execution time are enabled in first-in-first-out (FIFO) order of submission.

As n1ckolas has pointed out, you are going to have difficulty trying to get this kind of precision in pure Java.

However, there are some things you can try which might be more accurate than a ScheduledThreadPoolExecutor although their accuracy will be dependent on the OS, the hardware, etc.

like image 45
Pace Avatar answered Sep 22 '22 12:09

Pace