Thread.join冻结进度条 [英] Thread.join freezes progressbar

查看:123
本文介绍了Thread.join冻结进度条的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用线程运行多个进度条的代码.

I have a code which uses a thread to run multiple progress bar.

Thread thread1 = new Thread(progressbar1);
thread1.start(); 
thread2.start()

以此类推.

thread1.join(); 
thread2.join(); 
thread3.join();

但是,这冻结了所有三个进度条动画,并且仅在完成所有线程后才显示完成. 如何解决这个问题呢? 谢谢.

But that freezes all three progress bar animation and shows the completion only when all threads are done. How to solve this problem? Thanks.

推荐答案

Doing any blocking operation on the Event Dispatch Thread will cause your GUI to become unresponsive and stop being repainted. That's why long-running operations should be done in a background thread.

似乎您知道您需要在后台线程中执行操作,但仍然希望阻止EDT.你不应该那样做.相反,您需要回调-一些代码将在后台线程完成运行后运行.

It seems like you know you need to do things in a background thread but still want to block the EDT. You shouldn't do that. Instead, you want a callback--some code that will be run once your background threads have finished running.

Java 6+中已经提供的回调机制是 SwingWorker .实施一个工作程序,将join()(和start() s)放入doInBackground()方法中.然后,您以后要执行的任何Swing操作都可以进入done().

An already provided callback mechanism available in Java 6+ is SwingWorker. Implement a worker, putting the join()s (and start()s) in the doInBackground() method. Then any Swing operations that you want to do afterward can go in done().

这篇关于Thread.join冻结进度条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆