按下按钮运行线程

时间:2017-04-30 11:18:56

标签: java multithreading swing thread-safety jbutton

我有一个带有以下按钮的GUI。当它被按下时,它应该运行一个新线程(我有一个实现Runnable的类)。但是,当我这样做时,GUI会冻结。我做错了什么,如何解决?

        //button
        addKitchenStaff.addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent e) {
                //create object of a class which implements runnable
                KitchenStaff kitchenStaff = new KitchenStaff(allDishes, ingredientsModel, dishesModel,communication.getBap());
                //arraylist of all such objects
                allKitchenStaff.add(kitchenStaff);

                Thread thread = new Thread(kitchenStaff);
                thread.run();
            }
        });

2 个答案:

答案 0 :(得分:5)

要运行线程,您应该调用start()方法。

尝试thread.start()而不是thread.run()

答案 1 :(得分:2)

run()适用于调用Thread。请改用start()