简体   繁体   English

在java中用线程显示忙状态

[英]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. 我正在编写一个Java应用程序,它写入excel表数据集,这需要一段时间才能完成。

I'd like to create something like writing out dots to screen like on Linux when you're installing something. 我想在你安装东西的时候创建类似于在Linux上编写点到屏幕的东西。

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? 这有可能在java?打印点,而其他线程实际上写入excel,然后在它完成后,一个显示点也退出?

I'd like to print dots to console. 我想打印点到控制台。

A variation to @John V. answer would be to use a ScheduledExecutorService: @John V.答案的变体是使用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. 使用newSingleThreadExecutor打印点,而另一个线程进行解析。 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. 然后通过扩展Thread类或实现Runnable接口来创建一个线程。 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. 如果处理器使用率很高,请在循环内使用Thread.sleep() This thread should not be labour intensive. 这个帖子不应该是劳动密集型的。 System.gc() is another popular way to make threads wait. System.gc()是另一种使线程等待的流行方法。

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. 它在循环中打印1,2,3,1,2 ......点,并在5秒后终止所有内容。

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");
                }
            }
        }

    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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