title: Stop current Thread and then create a new thread that performs the same operation?
body: <p>My problem is that I cannot figure out a way of having a thread that "on the click of a button starts, and stops on the click of another button", and then if I click the same start button a NEW thread starts that does exactly the same operation as the first. So basically just a new instance.</p>
  
  <p>In my program I have a Server app that has 2 buttons and 2 text fields. After the user has entered the correct username and password the Server app opens a new ServerSocket that listens for clients that want to connect. This is done in a separate Thread to prevent the GUI from freezing. After the stop button is pressed the Thread is stopped.</p>
  
  <p>How can I get my program to start a new Thread, that does the same as the first one, when I press the start button again in the GUI? Must I perhaps make use of a loop?</p>
  
  <p>Server App Code:</p>
  
  <pre><code>public class Server extends JFrame implements ActionListener {
  
      JLabel instructionsLabel;
      JLabel passwordLabel;
      JPasswordField passwordTF;
      JButton shutdownButton;
      JButton startupButton;
      JLabel usernameLabel;
      JTextField usernameTF;
      Thread MyThread = new Thread(new ServerRunnable());
  
      public Server() {
          super("Server");
          initComponents();
      }
                                 // My problem is here
      public void starterMeth() {
          MyThread.start();
      }
  
      public void stopMeth() {
          MyThread.interrupt();
      }
                                  // in these 2 methods
      public void actionPerformed(ActionEvent e) {
          Object source = e.getSource();
  
          String f = "n";
          ConnectionBean cb = new ConnectionBean();
  
          char[] a = passwordTF.getPassword();
          String b = new String(a);
          String inputDetails = usernameTF.getText() + b;
  
          Iterator it = cb.getDetails().iterator();
          while (it.hasNext()) {
              Object next = it.next();
              if (inputDetails.equals(next)) {
                  f = "y";
                  if (source == startupButton) {
                      if (!MyThread.isInterrupted()) {
                          starterMeth();
                          JOptionPane.showMessageDialog(null,
                              "Congratulations! Server started.",
                              "Start-up Message",
                              JOptionPane.INFORMATION_MESSAGE);
                      } else {
                          JOptionPane.showMessageDialog(null,
                              "Please restart the server application.",
                              "Start-up Message",
                              JOptionPane.INFORMATION_MESSAGE);
                      }
                  } else if (source == shutdownButton) {
                      stopMeth();
                      JOptionPane.showMessageDialog(null,
                          "Server shut-down successfully!",
                          "Shut-down Message",
                          JOptionPane.INFORMATION_MESSAGE);
                  }
                  // only resets the text fields when the correct details are entered
                  passwordTF.setText("");
                  usernameTF.setText("");
              }
          }
          if (f.equals("n")) {
              JOptionPane.showMessageDialog(null, "Invalid username or password.", "Alert", JOptionPane.WARNING_MESSAGE);
          }
          cb.setCloseConnection(true);
      }
  
      private void initComponents() {
      }
  }
  </code></pre>
  
  <p>My Runnable Code:</p>
  
  <pre><code>public class ServerRunnable implements Runnable {
  
      @Override
      public void run() {
  
          try {
              ServerSocket ss = new ServerSocket(7777);
  
              while(true) {
                  Socket cs = ss.accept();
                  new ClientThread(cs).start();
          }
          } catch (IOException ex) {
          }
      }
  }
  </code></pre>