File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Return value for thread

 
john price
Ranch Hand
Posts: 495
Chrome Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Paul Clapham
Sheriff
Pie
Posts: 20166
24
MySQL Database
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
john price
Ranch Hand
Posts: 495
Chrome Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Easy enough :


I only had to slightly change the actual thread, "GetHWLink". This website was a great tutorial : http://kamleshkr.wordpress.com/2009/10/02/java-threads-callable-and-future/

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?

Thanks,
John Price

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!
 
Ranganathan Kaliyur Mannar
Bartender
Posts: 1101
10
Java Netbeans IDE Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
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.
 
john price
Ranch Hand
Posts: 495
Chrome Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?

Thanks,
John Price
 
Paul Clapham
Sheriff
Pie
Posts: 20166
24
MySQL Database
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ranganathan Kaliyur Mannar
Bartender
Posts: 1101
10
Java Netbeans IDE Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul is 100% right. Also, SwingWorker can help to post the progress of the task. This can come in handy if and when you decide to show the progress to the user.
 
Sai Sunder
Greenhorn
Posts: 1
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Implement the callable interface. The call method in that interface works just like run method, but can return a value. Refer http://preciselyconcise.com/java/concurrency/d_return_value_from_thread.php for a demo
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic