*
The moose likes Threads and Synchronization and the fly likes throw checked exception from inner thread Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "throw checked exception from inner thread" Watch "throw checked exception from inner thread" New topic
Author

throw checked exception from inner thread

dhoni Gibson
Ranch Hand

Joined: Feb 10, 2011
Posts: 30
Hi all,

i have to throw checked exception from child thread to its parent thread. since run() method doesnt allow us to throw exception out of it, i am stuck here, please help me how to handle this situation.

i want to know whether the logic inside the child thread has been completed successfully or not.


Thanks in advance
dhoni
Edward Harned
Ranch Hand

Joined: Sep 19, 2005
Posts: 291

There is no parent/child relationship with threads; each is an independent entity.

You can pass a shared Class to the second thread. When the second thread completes, it can update the shared Class as to its status. The first thread can then get that status.


Ed's latest article: A Java Parallel Calamity http://coopsoft.com/ar/Calamity2Article.html
dhoni Gibson
Ranch Hand

Joined: Feb 10, 2011
Posts: 30
hi ed,

thanks for you reply. can you please explain me with an example. i believe that you want me to pass the shared class to the thread ( being invoked ) through its costructor.


Thanks
dhoni
Edward Harned
Ranch Hand

Joined: Sep 19, 2005
Posts: 291

MySharedClass msc = new MySharedClass();
OtherThread ot = new OtherThread(msc);
ot.start();
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12769
    
    5
You don't pass data to Threads, you pass data to objects, presumably Runnable object attached to a Thread which is executing methods in the object.

Bill
dhoni Gibson
Ranch Hand

Joined: Feb 10, 2011
Posts: 30
hi ed/william,

Thanks for your reply . i have few questions for you

i have runnable class to which i can pass my shared class through constructor. just for my knowledge , william please let me know why cant we use thread instead of runnable class. do you have any specific reason to use runnable object.

in addition i have one more question to all.. instead of passing whole class to the another thread; can i pass simple variable so that i get the status say 'success' or 'failure'.



Thanks
dhoni
Edward Harned
Ranch Hand

Joined: Sep 19, 2005
Posts: 291

You don't pass a whole class, just a reference to the class.

Primitives are passed by copy, so you can't update one and have it reflected in the calling thread. Therefore, you need to pass a reference to something that both threads can see. An array is a form of object so you can pass say:
int[] pass = new int[1];
pass[0] = 0;
Now the other thread can set the first int to 1 when it is done.
Naveen Madarapu
Ranch Hand

Joined: May 24, 2011
Posts: 64

@dhoni Gibson

i have runnable class to which i can pass my shared class through constructor. just for my knowledge , william please let me know why cant we use thread instead of runnable class. do you have any specific reason to use runnable object.


Remember that runnable is not a class it is an interface.


OCPJP
dhoni Gibson
Ranch Hand

Joined: Feb 10, 2011
Posts: 30
thanks for all your reply. naveen i meant to say class that implements runnable interface. i apoligize for not making it clear.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: throw checked exception from inner thread