Java AtomicLong printProgress(final AtomicLong value)

Here you can find the source of printProgress(final AtomicLong value)

Description

print Progress

License

Apache License

Declaration

public static void printProgress(final AtomicLong value) 

Method Source Code

//package com.java2s;
/*//from   w  w  w. j  a v a  2  s  .co  m
 *  Copyright (c) 2012 Jan Kotek
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.locks.LockSupport;

public class Main {
    /** @deprecated */
    public static void printProgress(final AtomicLong value) {
        new Thread("printProgress") {
            {
                setDaemon(true);
            }

            @Override
            public void run() {
                long startValue = value.get();
                long startTime, time = System.currentTimeMillis();
                startTime = time;
                long old = value.get();
                while (true) {
                    time += 1000;
                    while (time > System.currentTimeMillis()) {
                        LockSupport.parkNanos(1000 * 1000); //1ms
                    }

                    long current = value.get();
                    if (current < 0) {
                        System.out.println("Finished, total time: " + (time - startTime) + ", aprox items: " + old);
                        return;
                    }
                    long totalSpeed = 1000 * (current - startValue) / (time - startTime);
                    System.out.print("total: " + current + " - items per last second: " + (current - old)
                            + " - avg items per second: " + totalSpeed + "\r");
                    old = current;
                }

            }
        }.start();
    }
}

Related

  1. isAtomicLong(final N number)
  2. joinThreads()
  3. lock()
  4. max(AtomicLong x, long y)
  5. nanoTime()
  6. randomSeed()
  7. removeCustomTime()
  8. resetURICounter()
  9. runThread(Runnable r)