简体   繁体   中英

Displaying busy status with thread in java

I'm writing a Java app which writes to excel sheet bunch of data, and it takes a while to do so.

I'd like to create something like writing out dots to screen like on Linux when you're installing something.

Is that possible in java?printing dots, while other thread actually does the writing to excel, then after its finished the one displaying dots also quits?

I'd like to print dots to console.

A variation to @John V. answer would be to use a ScheduledExecutorService:

// SETUP
Runnable notifier = new Runnable() {
    public void run() {
        System.out.print(".");
    }
};

ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);

// IN YOUR WORK THREAD
scheduler.scheduleAtFixedRate(notifier, 1, 1, TimeUnit.SECONDS);
// DO YOUR WORK
schedule.shutdownNow();

Modify the notifier object to suit your individual needs.

Its very possible. Use a newSingleThreadExecutor to print the dots while the other thread does the parsing. For example

ExecutorService e = Executors.newSingleThreadExecutor();
Future f = e.submit(new Runnable(){
   public void run(){
       while(!Thread.currentThread().isInterrupted()){
          Thread.sleep(1000); //exclude try/catch for brevity
          System.out.print(".");
       }
   }
});
//do excel work
f.cancel(true);
e.shutdownNow();

Yes, it is possible, you will want to have your working thread set a variable to indicate that it is working and when it is finished. Then create a thread by either extending the Thread class or implementing the Runnable interface. This thread should just infinitely loop and inside this loop it should do whatever printing you want it to do, and then check the variable to see if the work is done. When the variable value changes, break the loop and end the thread.

One note. Watch your processing speed. Use Thread.sleep() inside your loop if your processor usage goes way high. This thread should not be labour intensive. System.gc() is another popular way to make threads wait.

Not an elegant solution, but get's the job done. It prints 1, 2, 3, 1, 2... dots in a loop and terminates everything after 5 seconds.

public class Busy {

    public Busy() {
        Indicator i = new Indicator();
        ExecutorService ex = Executors.newSingleThreadExecutor();
        ex.submit(i);
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        i.finished = true;
        ex.shutdown();
    }

    public static void main(String[] args) {
        new Busy();
    }

    private class Indicator implements Runnable {

        private static final int DOTS_NO = 3;
        private volatile boolean finished = false;

        @Override
        public void run() {
            for (int i = 0; !finished; i = (i + 1) % (DOTS_NO + 1)) {
                for (int j = 0; j < i; j++) {
                    System.out.print('.');
                }
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                for (int j = 0; j < i; j++) {
                    System.out.print("\b \b");
                }
            }
        }

    }

}

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