I’m trying to make so that when I press a button the program starts a method in a new thread.
The problem is that when I press the button the program freezes until the method is done running like i didn’t use a thread at all. Does anyone know how to solve this problem?
Thanks Morgan.
public Listeners()
{
Calendar.ButtonAddReminder.addActionListener(new ButtonAddListener());
}
private class ButtonAddListener implements ActionListener
{
public void actionPerformed(ActionEvent e) {
new Thread(Calendar.reminder.Reminderchecker(Calendar.reminder.addReminder(date, str))) .start();
}
}
From that, it looks like something in
Calendar.reminder.addReminder()orCalendar.reminder.Reminderchecker()is taking some time and locking up the UI since that’s what’s happening in the EDT.Edit: Oh, I see. You’re not doing what you think you’re doing. You’re executing
Remindercheckerin the current thread. TheRunnablereturned by that method is what’s getting executed in the new thread. To runRemindercheckerin a thread, do something like:Better: Don’t spawn your own random threads like that. Use an organized concurrency strategy such as provided by an
ExecutorService. TheExecutorsclass lets you easily create several that cover common uses.Still better: Check out the SwingWorker and it API docs.