1

I write Java desktop app to fetch and post some data from my online rails backend app. The App have to call a get request every 5 second to update the relay state(example Arduino). here is my code:

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);

        }
    });
}

}

but it didn't work as expected, my question is how can i fix my code and how to put the thread correctly?

Catchwa
  • 5,845
  • 4
  • 31
  • 57

2 Answers2

2

You can use a Thread object in class's member and you can start and stop in button click action events. Here is the sample to start/stop 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();
    }

}
Pasupathi Rajamanickam
  • 1,982
  • 1
  • 24
  • 48
0

You can possibly use thread.join();

Grobbed
  • 317
  • 2
  • 12