在java中显示线程的繁忙状态
我正在编写一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
@John V. 答案的一个变体是使用 ScheduledExecutorService:
修改通知程序对象以满足您的个人需求。
A variation to @John V. answer would be to use a ScheduledExecutorService:
Modify the notifier object to suit your individual needs.
很有可能。使用 newSingleThreadExecutor 打印点,而另一个线程进行解析。例如
Its very possible. Use a newSingleThreadExecutor to print the dots while the other thread does the parsing. For example
是的,有可能,您需要让工作线程设置一个变量来指示它正在工作以及何时完成。然后通过扩展 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 theRunnable
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.这不是一个优雅的解决方案,但可以完成工作。它循环打印 1, 2, 3, 1, 2... 点,并在 5 秒后终止所有操作。
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.