The Artima Developer Community
Sponsored Link

Java Answers Forum
threads..

3 replies on 1 page. Most recent reply: Feb 10, 2007 7:05 AM by Vijay Nathani

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 3 replies on 1 page
Alex Tanil

Posts: 10
Nickname: alextanil
Registered: May, 2006

threads.. Posted: Jan 15, 2007 3:11 AM
Reply to this message Reply
Advertisement
how can, a thread return something?
i want when a thread ends to return something, like a value for a table...


Alex Tanil

Posts: 10
Nickname: alextanil
Registered: May, 2006

Re: threads.. Posted: Jan 15, 2007 3:41 AM
Reply to this message Reply
but i dont want the thread to appear..
I mean i dont want the thread to be printed out but instead to return a value.
Can anyone help me?

Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: threads.. Posted: Jan 15, 2007 6:45 AM
Reply to this message Reply
A Thread is not a method. It does not return anything (except error codes and such stuff).
A Thread is however a class and a class can contain methods which can return values.
So you have the option to make the run method and the method which returns the value syncronized, which means that one can not be called while the other one is still running.
After starting your thread, call the returnValue method. Your program will wait until the Thread has stopped and then get the value from this method.
But if you work like that, using a Thread does not make any sense at all.


btw: how do you print out a thread?

Vijay Nathani

Posts: 29
Nickname: vijaynath
Registered: Jan, 2006

Re: threads.. Posted: Feb 10, 2007 7:05 AM
Reply to this message Reply
In Java 5.0 and higher, you can use FutureTask. It has a function "get" that returns the necessary value.

Obviously a thread cannot return a value, but this class alongwith Callable class simulates the return value from a function executed in a different thread.

Regards.......Vijay
www.geocities.com/vijay_nathani

Flat View: This topic has 3 replies on 1 page
Topic: threads.. Previous Topic   Next Topic Topic: loop statement

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use