java - How to start and stop a threads in netbeans from a jButton click -


i write java desktop app fetch , post data online rails backend app. app have call request every 5 second update relay state(example arduino). here 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 didn't work expected, question how can fix code , how put thread correctly?

you can use thread object in class's member , can start , stop in button click action events. here sample 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 wait 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();     }  } 

Comments

Popular posts from this blog

javascript - Bootstrap Popover: iOS Safari strange behaviour -

Magento/PHP - Get phones on all members in a customer group -

session - Logging Out Using PHP -