I’m writing a small application, composed by a gui and a couple of buttons. When the user clicks one of them, the program must download a webpage, do a couple of matching and return a value to the gui.
The question is, I must start a new thread every time the user clicks on that button, or there’s an alternative to threads for those kind of small tasks ( mainly, download contents from the web)?
I’m writing a small application, composed by a gui and a couple of buttons.
Share
You always have to use a different threads. Now how you use those different threads can be up to you. Look at the
producer/consumermethod where there is just an idle thread sitting in the background waiting for work to be queued. This seems to fit your job well. You should also useSwingWorkerwhen possible because it helps with a lot GUI updates and refreshes. Finally, look at thejava.concurrencypackage as it is very useful. Now days, I don’t think there is any reason to start a Thread manually without using some library that does all the locking and threading for you.