简体   繁体   中英

Alternative to thread for small tasks without freezing the GUI

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)?

You always have to use a different threads. Now how you use those different threads can be up to you. Look at the producer/consumer method 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 use SwingWorker when possible because it helps with a lot GUI updates and refreshes. Finally, look at the java.concurrency package 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.

If you want that call to be asynchronous, there's no alternative.

However, if you can afford to let your user wait until the download is completed and processed, there's no need for threads.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM