wood burning stoves 2.0*
The moose likes Threads and Synchronization and the fly likes Return value for thread Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Return value for thread" Watch "Return value for thread" New topic
Author

Return value for thread

john price
Ranch Hand

Joined: Feb 24, 2011
Posts: 495

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)
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18712
    
    8

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

Joined: Feb 24, 2011
Posts: 495

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

Joined: Oct 16, 2003
Posts: 1085
    
  10

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.


Ranga.
SCJP 1.4, OCMJEA/SCEA 5.0.
john price
Ranch Hand

Joined: Feb 24, 2011
Posts: 495

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
Bartender

Joined: Oct 14, 2005
Posts: 18712
    
    8

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

Joined: Oct 16, 2003
Posts: 1085
    
  10

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

Joined: Jan 30, 2014
Posts: 1
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Return value for thread