在java中显示线程的繁忙状态

发布于 2024-10-30 17:46:32 字数 178 浏览 0 评论 0原文

我正在编写一个 Java 应用程序,它将大量数据写入 Excel 工作表,并且需要一段时间才能完成。

我想创建一些类似在 Linux 上安装某些东西时在屏幕上写出点的东西。

在java中这可能吗?打印点,而其他线程实际上正在写入excel,然后在完成后显示点的线程也退出?

我想打印点到控制台。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

乱了心跳 2024-11-06 17:46:32

@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();

修改通知程序对象以满足您的个人需求。

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.

橙味迷妹 2024-11-06 17:46:32

很有可能。使用 newSingleThreadExecutor 打印点,而另一个线程进行解析。例如

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();

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();
各自安好 2024-11-06 17:46:32

是的,有可能,您需要让工作线程设置一个变量来指示它正在工作以及何时完成。然后通过扩展 Thread 类或实现 Runnable 接口来创建线程。该线程应该无限循环,并且在该循环内它应该执行您想要执行的任何打印操作,然后检查变量以查看工作是否完成。当变量值发生变化时,中断循环并结束线程。

一张纸条。注意你的处理速度。如果您的处理器使用率很高,请在循环内使用 Thread.sleep() 。该线程不应该是劳动密集型的。 System.gc() 是另一种让线程等待的流行方法。

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.

So尛奶瓶 2024-11-06 17:46:32

这不是一个优雅的解决方案,但可以完成工作。它循环打印 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");
                }
            }
        }

    }

}

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

    }

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文