I was trying to restart a thread after I stop it manually in my Project written in java. When I try to restart it I was give an IllegalThreadStateException error.
I simplified my code as the following sample code. My questions are:
How can I restart the logtest thread, do I have to start a new thread instead of restarting it?
As you can see from the following code, in the run method of the class LogThread, I used Thread.Sleep(3) to output these numbers row by row. If I delete this line, the thread will either die in the middle or output all these 10000 rows all at once. Do I have to force the thread to stop 3 milliseconds to sacrifice its performance? Are there any better way to solve this?
The Code:
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.text.StyledDocument;
public class LogTest {
private LogThread logtest;
public LogTest() {
JFrame frame = new JFrame("LogTest");
Container con = frame.getContentPane();
final JTextPane pane = new JTextPane();
final JScrollPane scollPane = new JScrollPane(pane);
JButton button = new JButton("Start Test Log");
JButton button1 = new JButton("Stop Test Log");
logtest = new LogThread(pane);
con.add(scollPane);
con.add(button, BorderLayout.NORTH);
con.add(button1, BorderLayout.SOUTH);
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
logtest.start();
}
});
button1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
logtest.stop();
}
});
frame.setSize(600, 500);
frame.setVisible(true);
}
public static void main(String[] args) {
new LogTest();
}
}
class LogThread implements Runnable {
private final Thread t;
private String newline;
private volatile boolean shouldStop = false;
private StyledDocument doc;
private JTextPane pane;
private static int counter = 0;
public LogThread(JTextPane pane) {
this.doc = pane.getStyledDocument();
this.pane = pane;
t = new Thread(this, "Log Thread");
newline = System.getProperty("line.separator");
}
public void start() {
t.start();
}
public void run() {
try {
if (doc.getLength() > 0) {
doc.remove(0, doc.getLength());
}
} catch (Exception e) {
e.printStackTrace();
}
if(shouldStop) shouldStop=false;
for (int i = counter; i <= 1000 && !shouldStop; i++) {
try {
doc.insertString(doc.getLength(),
i + "-------------------------------------------------------------"+ newline,
null);
pane.setCaretPosition(doc.getLength());
Thread.sleep(3);
} catch (Exception e) {
e.printStackTrace();
}
}
}
public void stop() {
shouldStop = true;
}
}