如何添加线程MVC模式theView以避免JFrame冻结

时间:2014-03-18 13:28:25

标签: java multithreading swing model-view-controller

我想用线程运行我的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);
    }
}

2 个答案:

答案 0 :(得分:2)

简单的答案是大部分工作都是在GUI线程上完成的,只有时间密集型的工作放在单独的线程上(阅读有关创建新线程,Runnable接口,ThreadPoolExecutor和Callable接口的信息)。

一般来说,有很多模式如何工作。对于初学者来说,它可能是生产者/消费者模式。

与线程一样,不要忘记保护共享数据(同步,锁定)甚至更好 - 避免在线程之间共享数据:)

答案 1 :(得分:1)

避免冻结使用SwingWorker进行长时间运行的任务。另请阅读Concurrency in Swing