This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
I have changed from a local method to a thread. The local method included a return value, which I need to keep in the thread. Is there any way to set a return value for a thread?
Here is the thread with the return value (doesn't compile because of the return statement at the end) :
EDIT : Please note that this is NOT a homework assignment. This is a personal project and has NOTHING to do with school. This is a project to get the homework assignments from chosen teachers. It will be a free service for students and I will eventually sell it to the school's server managers for them. Thank you!
EDIT 2 : I found something that might help : Callable. Is this what I should be looking for?
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” (Mosher's Law of Software Engineering)
“If debugging is the process of removing bugs, then programming must be the process of putting them in.” (Edsger Dijkstra)
No. It doesn't make any sense for a thread to have a "return value" because there is nowhere for a thread to return a value to.
However the java.util.concurrent package has the concept of a "Future" , which -- as the documentation says -- "represents the result of an asynchronous computation". Many of the classes in that package support creating and using Future objects.
I made this into a thread so that my swing components wouldn't be laggy. It is still acting laggy (same amount of "lagginess" as before). What happens is you click on a teacher. It spits out a link to the teacher's homework (more to be added later on this) if available. If not, it prints a "could not find" error message. This is the same for the thread or the method. I need to fix it to where someone can check the box and you will not wait to see the check until the thread or method is done, but immediately while the thread or whatever I need to do to make this run smoothely runs in the background. To be clear, I did successfully figure it out and answer the original question. Should I open a new topic in GUI or here or keep this one going?
EDIT : I have an idea. I should make the above code (and the rest of the edited out code) into a thread. That way, everything runs through a thread and doesn't have to wait to execute before it is ready to check and run the next box. Would this be acceptable?
EDIT 2 : Regular thread hosting the old method seems to do the trick. Thanks for your help Paul Chapman!
There is a SwingWorker class that is specially written to handle long-running tasks in a Swing application. Its very handy and can return the 'progress' of the task as well as the end result. You should consider using it.
I read this right after you posted and couldn't decide on what to say, sorry. The thread that the method runs is not long. It just downloads an HTML file, parses through it for some information, and returns the value. Should I use thread (and method inside) or SwingWorker. The thread works fine for me, although I do want efficiency. The thread will not be running long term, although a few steps may be added, for a grand total of 3-10 seconds for a running thread to do all of it's work. What do you suggest?
There's no penalty for choosing a SwingWorker to execute a task which runs very quickly. So if you have a task whose run time is going to be unpredictable -- like your task which connects to a URL and downloads a file, for example -- you might as well use a SwingWorker.