• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
  • Junilu Lacar
Sheriffs:
  • Tim Cooke
  • Jeanne Boyarsky
  • Knute Snortum
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • salvin francis
  • fred rosenberger
  • Frits Walraven

throw checked exception from inner thread

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 291
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
dhoni Gibson
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 291
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
MySharedClass msc = new MySharedClass();
OtherThread ot = new OtherThread(msc);
ot.start();
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 291
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 64
Eclipse IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@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.
 
dhoni Gibson
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for all your reply. naveen i meant to say class that implements runnable interface. i apoligize for not making it clear.
 
Popeye has his spinach. I have this tiny ad:
ScroogeXHTML 8.7 - RTF to HTML5 and XHTML converter
https://coderanch.com/t/730700/ScroogeXHTML-RTF-HTML-XHTML-converter
    Bookmark Topic Watch Topic
  • New Topic