如何通过jButton单击启动和停止netbeans中的线程

时间:2015-06-04 03:40:24

标签: java multithreading jbutton

我编写Java桌面应用程序来从我的在线rails后端应用程序中获取和发布一些数据。应用程序必须每5秒调用一次get请求来更新中继状态(例如Arduino)。这是我的代码:

public class GUI extends javax.swing.JFrame {

private Serial serial = null;
private Service service = null;
private volatile boolean connected = false;
private Thread updateThread;

public GUI() {
    initComponents();
    init_serial();
    service = new Service();
    updateThread = new Thread() {
        public void run() {
            while (connected) {
                updateJob();
            }
        }
    };
    updateThread.start();
}

private void init_serial() {
    serial = new Serial();
    serial.searchForPorts();
    serial.connect();
    serial.initIOStream();
    serial.initListener();
}

private void updateJob() {
    ActionListener actListner = new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent event) {
            updateState();
        }

    };
    Timer timer = new Timer(5000, actListner);
    timer.start();
}

private void updateState() {
    String portState = service.get_port_state();
    serial.write(portState);
    System.out.println(portState);
}


private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    connected = true;
    logger.setText(null);
    logger.setText("connected");
}                                        

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    logger.setText(null);
    logger.setText("disconnected");
}                                        


public static void main(String args[]) {
    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            new GUI().setVisible(true);

        }
    });
}

}

但它没有按预期工作,我的问题是如何修复我的代码以及如何正确地放置线程?

2 个答案:

答案 0 :(得分:1)

您可以在班级成员中使用Thread对象,您可以在按钮点击操作事件中启动和停止。以下是启动/停止线程的示例。

public class GUI extends javax.swing.JFrame {
    Thread updateThread = null;
    public GUI() {
        JButton btnStart = new JButton("Start");
        JButton btnStop = new JButton("Stop");
        JPanel jPanel = new JPanel();
        jPanel.setBounds(0, 0, 100, 200);
        jPanel.add(btnStart);
        jPanel.add(btnStop);
        add(jPanel);
        btnStart.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                updateThread = new Thread(new Runnable() {
                    @Override
                    public void run() {
                        while (true) {
                            System.out.println("Work updated");
                            try {
                                Thread.sleep(1000);//Time to wait for next routine
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                });
                updateThread.start();
            }
        });
        btnStop.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                updateThread.stop();

            }
        });
        setVisible(true);
        setBounds(0, 0, 100, 200);
    }

    public static void main(String[] args) {
        new GUI();
    }

}

答案 1 :(得分:0)

您可以使用thread.join();