how to call a for loop into new Thread?
hello there
in the following code i'm trying to make that if the user presses a button
a string is printed continusely and the infinite loop prevented the program from closing so i want to put the loop in a new thread to handle that bug,how could i do that?
import java.awt.*;
import java.awt.event.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import java.awt.Container;
publicclass threadexampleextends JFrame{
publicstaticvoid main(String[] args){
JButton btn=new JButton("Press Me");
JFrame f=new JFrame("Infinite Loop");
ActionListener action=new ActionListener(){
publicvoid actionPerformed(ActionEvent e)
{
if(e.getActionCommand().equals("Press Me"))
{
for( ; ; )
{
try{
Thread.sleep(2000);
}
catch(InterruptedException io){}
System.out.println("Hello World!");
}
}
}
};
f.add(btn);
f.setSize(200,200);
f.setVisible(true);
btn.addActionListener(action);
f.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}
For a more general understanding of basic threading, I would go here:
http://java.sun.com/docs/books/tutorial/essential/concurrency/runthread.html
On another note, another popular way to create threads for basic tasks like the one you are talking about is to use an anonymous class:
Thread t = new Thread() {
public void run() {
/* do stuff in a new thread */
}
};
t.start();
/* Do stuff while t is running */
Executing time consuming code (like an infinite loop) should never be done on the GUI thread (called the Event Dispatching Thread or EDT) since then it can't handle other events like e.g. painting.
Instead spawn worker threads to do the long running task, e.g. SwingWorker or javax.swing.Timer. Here's an example using Timerpublic class TimerDemo {
private static void initAndShowGUI() {
JFrame frame = new JFrame("Timer demo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel contentPane = new JPanel(new BorderLayout());
final JTextArea textArea = new JTextArea(5, 20);
final JButton start = new JButton("Start");
final JButton stop = new JButton("Stop");
stop.setEnabled(false);
JPanel buttonPane = new JPanel();
buttonPane.add(start);
buttonPane.add(stop);
contentPane.add(new JScrollPane(textArea), BorderLayout.CENTER);
contentPane.add(buttonPane, BorderLayout.SOUTH);
final Timer timer = new Timer(2000, new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
textArea.append("Hello world!\n");
System.out.println("Hello world!");
}
});
start.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
start.setEnabled(false);
stop.setEnabled(true);
timer.start();
}
});
stop.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
start.setEnabled(true);
stop.setEnabled(false);
timer.stop();
}
});
frame.setContentPane(contentPane);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
initAndShowGUI();
}
});
}
}
dwga at 2007-7-29 11:22:00 >
