我想用线程运行我的GUI以避免冻结问题,如何添加线程来查看?或者你有任何一般的想法,以避免GUI冻结问题。
public class Vark {
public static void main(String[] args) {
// Object Creation For the View
VarkView theView = new VarkView();
// Object Creation For the Model
VarkModel theModel = new VarkModel();
// Object Creation For the Controller passing theModel and theView
VarkController theController = new VarkController(theView, theModel);
theView.setVisible(true);
}
}
答案 0 :(得分:2)
简单的答案是大部分工作都是在GUI线程上完成的,只有时间密集型的工作放在单独的线程上(阅读有关创建新线程,Runnable接口,ThreadPoolExecutor和Callable接口的信息)。
一般来说,有很多模式如何工作。对于初学者来说,它可能是生产者/消费者模式。
与线程一样,不要忘记保护共享数据(同步,锁定)甚至更好 - 避免在线程之间共享数据:)
答案 1 :(得分:1)
避免冻结使用SwingWorker
进行长时间运行的任务。另请阅读Concurrency in Swing