简体   繁体   中英

System.nanotime giving negative numbers

I writing an android timer app that uses System.nanotime. The issue is that it's giving me underpredicted results as well negative numbers. redVal, blueVal, and greenVal are updated on each frame of the camera.

results

504455566
-95947265
9063721
61035
-99487305
-98937988
12664795
-75317382

code

        for (testAmount = 0; testAmount < 80; testAmount++) {
            runOnUiThread(new Runnable() {
                public void run() {
                    lagSquare.setBackgroundColor(Color.rgb(255, 255, 255));
                    lagStartTime = System.nanoTime(); //start lagTimer start
                }
            });
            while (redVal <= 100.0 && blueVal <= 100.0 && greenVal <= 100.0) {
                x=0;
            }
            runOnUiThread(new Runnable() {
                public void run() {
                    lagEndTime = System.nanoTime(); //start lagTimer end
                    lagSquare.setBackgroundColor(Color.rgb(000, 000, 000));//set lagSquare black
                }
            });
            lagTimeResult = (lagEndTime - lagStartTime);
            timeArray[testAmount] = lagTimeResult;
            Log.i("LTR", String.valueOf(lagTimeResult));
            try {
                Thread.sleep(60);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

You are trying to output a time difference which is relying upon values being set in different threads, without any synchronization. This will almost always end up with the wrong value:

for (testAmount = 0; testAmount < 80; testAmount++) {

            // this will schedule the Runnable to run *sometime* in
            // the future - but not necessarily right now
            runOnUiThread(new Runnable() {
                public void run() {
                    lagStartTime = System.nanoTime(); //start lagTimer start
                }
            });

            // this will also schedule this Runnable to run *sometime* in
            // the future - but not necessarily after the first one
            runOnUiThread(new Runnable() {
                public void run() {
                    lagEndTime = System.nanoTime(); //start lagTimer end
                }
            });

            // this will probably execute first (before either of
            // the other Runnables have completed)
            lagTimeResult = (lagEndTime - lagStartTime);
        }

You simply can't rely on the order of threads executing in the order you've coded them - and definitely not in a loop. I can't understand from your question what you are trying to time, but the take-home rule is that whenever you have multiple threads, you can never rely on the order of execution without using some form of synchronization.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM