I am creating a Swing application. It contains a JFrame, inside JFrame, I added a JButton to start and stop some tasks. I am using the same JButton to start and stop the tasks which are threads and executed by ExecutorService.
While clicking on the Start button, threads will be executed and the button label will be changed to Stop, while clicking on the Stop button will stop all threads (I have done this using ExecutorService shutdownNow() method) and the button label will be again changed to Start, but the applicaiton will not be closed. Now, if I click Start button again, applicaion gets hanged, the threads are not restarted from the beginning.
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class MultipleThreads {
public static ExecutorService executor = Executors.newFixedThreadPool(4);
public static void main(String[] args) {
JFrame frame = new JFrame("Stop Thread");
frame.setSize(200,200);
frame.setLocationRelativeTo(null);
frame.setLayout(null);
JPanel panel = new JPanel();
panel.setBounds(5,5,150,150);
panel.setLayout(null);
JButton btn = new JButton("Start");
btn.setBounds(10,10,80,25);
btn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) {
if (btn.getText().equals("Start")) {
btn.setText("Stop");
MultipleThreads2 runThreads = new MultipleThreads2();
runThreads.runThreadMethod();
} else if (btn.getText().equals("Stop")) {
try {
if (!executor.awaitTermination(800, TimeUnit.MILLISECONDS)) {
executor.shutdownNow();
}
} catch (InterruptedException e) {
executor.shutdownNow();
}
btn.setText("Start");
}
}
});
panel.add(btn);
frame.add(panel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
class MultipleThreads2 {
public volatile boolean flag = true;
public void stopRunning() {
flag = false;
}
public MultipleThreads2() {
while (flag) {
try {
MultipleThreads.executor.submit(t1);
MultipleThreads.executor.submit(t2);
flag = false;
System.out.println(t1.isAlive());
} catch (Exception e) {
}
}
}
public void runThreadMethod() {
flag = true;
while (flag) {
try {
MultipleThreads.executor.submit(t3);
MultipleThreads.executor.submit(t4);
flag = false;
} catch (Exception e) {
}
}
}
Thread t1 = new Thread(new Runnable() {
@Override
public void run() {
try {
for (int i = 0; i < 10; i++) {
System.out.println("From t1 Thread");
Thread.sleep(1000);
}
} catch (Exception e) {
}
}
});
Thread t2 = new Thread(new Runnable() {
@Override
public void run() {
try {
for (int i = 0; i < 10; i++) {
System.out.println("From t2 Thread");
Thread.sleep(500);
}
} catch (Exception e) {
}
}
});
Thread t3 = new Thread(new Runnable() {
@Override
public void run() {
try {
for (int i = 0; i < 10; i++) {
System.out.println("From t3 Thread");
Thread.sleep(500);
}
} catch (Exception e) {
}
}
});
Thread t4 = new Thread(new Runnable() {
@Override
public void run() {
try {
for (int i = 0; i < 10; i++) {
System.out.println("From t4 Thread");
Thread.sleep(500);
}
} catch (Exception e) {
}
}
});
}
Expected: While clicking on Start button for the second time should restart all the threads from the beginning.