This week's book giveaway is in the Testing forum.
We're giving away four copies of Data Structures the Fun Way: An Amusing Adventure with Coffee-Filled Examples and have Jeremy Kubica on-line!
See this thread for details.
  • 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Question on Threads

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For the below code, given answer as:
"other.join attempts to join two Threads" ... Please explain.

public class ThreadTest {

class InnerRun implements Runnable {
public void run() {
// do slow stuff
System.out.println ("Ren");
}
}
class InnerRunTwo implements Runnable {
Thread other;
InnerRunTwo(Thread t) {other = t;}
public void run() {
other.join();
System.out.println ("Stimpy");
}
}
void start() {
InnerRun ir = new InnerRun();
Thread t = new Thread(ir);
InnerRunTwo irr = new InnerRunTwo(t);
Thread h = new Thread(irr);
t.start();
h.start();
}
public static void main(String[] args) {
ThreadTest tt = new ThreadTest();
tt.start();

}

}
 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Subha,

Welcome to JavaRanch!



class InnerRunTwo implements Runnable {
Thread other;
InnerRunTwo(Thread t) {other = t;}
public void run() {
other.join(); //LINE 1
System.out.println ("Stimpy");
}
}



You should read Line #1 in the way: the current thread joins other thread.
It means current thread can't complete until the thread it joins to
completes.

[EDIT]
Note:
join() throws InterruptedException, so handle that exception.


Thanks,
[ June 13, 2007: Message edited by: Chandra Bhatt ]
 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Note: join() throws IllegalStateException, so handle that exception.



it actually throws InterruptedException and not IllegalStateException
and you need to handle in the run method.
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ooops!!!

It is InterruptedException!

"Previous post edited"

Thanks,
 
Shubha Kirani
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Chandra and Pravin.
Regarding the question, this is from Kathy & Bert, Master Exam. If I enclose 'other.join' in try-catch block (handle InturreptedException), code would compile fine. As explained in the answer key, I did not understand the meaning of 'attempts to join two threads' as there are only two threads. Other two similar program solution say, one thread join after other. SO wanted to know if I'm missing something here.

I wonder the # of posts answered everyday in this website. I'm very new to Java and Ranch. Like to start my career in Java, starting with SCJP certification. Hope one day I get to answer others quiries.
 
Amateurs built google. Professionals built the titanic. We can't find the guy that built this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic